-
Notifications
You must be signed in to change notification settings - Fork 224
/
legacy_cli.go
97 lines (87 loc) · 2.69 KB
/
legacy_cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"fmt"
"os"
"reflect"
"strings"
)
func translateLegacyOpts(args []string) ([]string, error) {
legacyOptMapping := map[string]string{
"-handler": "--handler",
"-image": "--image",
"-name": "--name",
"-gateway": "--gateway",
"-fprocess": "--fprocess",
"-lang": "--lang",
"-replace": "--replace",
"-no-cache": "--no-cache",
"-yaml": "--yaml",
"-squash": "--squash",
}
validActions := map[string]string{
"build": "build",
"delete": "remove",
"deploy": "deploy",
"push": "push",
}
action := ""
translatedArgs := []string{args[0]}
optsCache := args[1:]
// Replace action
for idx, opt := range optsCache {
if opt == "-version" {
translatedArgs = append(translatedArgs, "version")
optsCache = append(optsCache[:idx], optsCache[idx+1:]...)
action = "version"
}
if opt == "-action" {
if len(optsCache) == idx+1 {
return []string{""}, fmt.Errorf("no action supplied after deprecated -action flag")
}
if translated, ok := validActions[optsCache[idx+1]]; ok {
translatedArgs = append(translatedArgs, translated)
optsCache = append(optsCache[:idx], optsCache[idx+2:]...)
action = translated
} else {
return []string{""}, fmt.Errorf("unknown action supplied to deprecated -action flag: %s", optsCache[idx+1])
}
}
if strings.HasPrefix(opt, "-action"+"=") {
s := strings.SplitN(opt, "=", 2)
if len(s[1]) == 0 {
return []string{""}, fmt.Errorf("no action supplied after deprecated -action= flag")
}
if translated, ok := validActions[s[1]]; ok {
translatedArgs = append(translatedArgs, translated)
optsCache = append(optsCache[:idx], optsCache[idx+1:]...)
action = translated
} else {
return []string{""}, fmt.Errorf("unknown action supplied to deprecated -action= flag: %s", s[1])
}
}
}
for idx, arg := range optsCache {
if action == "remove" {
if arg == "-name" {
optsCache = append(optsCache[:idx], optsCache[idx+1:]...)
continue
}
}
if translated, ok := legacyOptMapping[arg]; ok {
optsCache[idx] = translated
}
for legacyOpt, translated := range legacyOptMapping {
if strings.HasPrefix(arg, legacyOpt) {
optsCache[idx] = strings.Replace(arg, legacyOpt, translated, 1)
}
}
}
translatedArgs = append(translatedArgs, optsCache...)
if !reflect.DeepEqual(args, translatedArgs) {
fmt.Fprintln(os.Stderr, "Found deprecated go-style flags in command, translating to new format:")
fmt.Fprintf(os.Stderr, " %s\n", strings.Join(translatedArgs, " "))
}
return translatedArgs, nil
}