/
command_subcommand.go
172 lines (147 loc) · 4.77 KB
/
command_subcommand.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2018. Akamai Technologies, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/akamai/cli/pkg/git"
"github.com/akamai/cli/pkg/log"
"github.com/akamai/cli/pkg/packages"
"github.com/akamai/cli/pkg/terminal"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func cmdSubcommand(git git.Repository, langManager packages.LangManager) cli.ActionFunc {
return func(c *cli.Context) error {
c.Context = log.WithCommandContext(c.Context, c.Command.Name)
logger := log.WithCommand(c.Context, c.Command.Name)
term := terminal.Get(c.Context)
commandName := strings.ToLower(c.Command.Name)
executable, _, err := findExec(c.Context, langManager, commandName)
if err != nil {
errMsg := color.RedString("Executable \"%s\" not found.", commandName)
logger.Error(errMsg)
return cli.Exit(errMsg, 1)
}
var packageDir string
if len(executable) == 1 {
packageDir = findPackageDir(executable[0])
} else if len(executable) > 1 {
packageDir = findPackageDir(executable[1])
}
cmdPackage, _ := readPackage(packageDir)
if cmdPackage.Requirements.Python != "" {
exec, err := langManager.FindExec(c.Context, cmdPackage.Requirements, packageDir)
if err != nil {
return err
}
if len(executable) == 1 {
executable = append([]string{exec[0]}, executable...)
} else {
if strings.Contains(strings.ToLower(executable[0]), "python") ||
strings.Contains(strings.ToLower(executable[0]), "py.exe") {
executable[0] = exec[0]
}
}
if runtime.GOOS == "linux" {
_, err = os.Stat(filepath.Join(packageDir, ".local"))
} else if runtime.GOOS == "darwin" {
_, err = os.Stat(filepath.Join(packageDir, "Library"))
} else if runtime.GOOS == "windows" {
_, err = os.Stat(filepath.Join(packageDir, "Lib"))
}
if err == nil {
answer, err := term.Confirm("Would you like to reinstall it", true)
logger.Debugf("Would you like to reinstall it? %v", answer)
if err != nil {
return err
}
if !answer {
logger.Error(packages.ErrPackageNeedsReinstall.Error())
return cli.Exit(color.RedString(packages.ErrPackageNeedsReinstall.Error()), -1)
}
if err = uninstallPackage(c.Context, langManager, commandName, logger); err != nil {
return err
}
if _, err = installPackage(c.Context, git, langManager, commandName, false); err != nil {
return err
}
}
if err := os.Setenv("PYTHONUSERBASE", packageDir); err != nil {
return err
}
}
var currentCmd command
for _, cmd := range cmdPackage.Commands {
if strings.EqualFold(cmd.Name, commandName) {
currentCmd = cmd
break
}
for _, alias := range cmd.Aliases {
if strings.EqualFold(alias, commandName) {
currentCmd = cmd
}
}
}
if err := os.Setenv("AKAMAI_CLI_COMMAND", commandName); err != nil {
return err
}
if err := os.Setenv("AKAMAI_CLI_COMMAND_VERSION", currentCmd.Version); err != nil {
return err
}
cmdPackage, err = readPackage(packageDir)
if err != nil {
return err
}
executable = prepareCommand(c, executable, c.Args().Slice(), "edgerc", "section", "accountkey")
subCmd := createCommand(executable[0], executable[1:])
return passthruCommand(c.Context, subCmd, langManager, cmdPackage.Requirements, fmt.Sprintf("cli-%s", cmdPackage.Commands[0].Name))
}
}
func prepareCommand(c *cli.Context, command, args []string, flags ...string) []string {
// dont search for flags is there are no args
if len(args) == 0 {
return command
}
additionalFlags := findFlags(c, args, flags...)
if len(command) > 1 {
// for python or js append flags to the end
command = append(command, args...)
command = append(command, additionalFlags...)
} else {
command = append(command, additionalFlags...)
command = append(command, args...)
}
return command
}
func findFlags(c *cli.Context, target []string, flags ...string) []string {
var ret []string
for _, flagName := range flags {
if flagVal := c.String(flagName); flagVal != "" && !containsString(target, fmt.Sprintf("--%s", flagName)) {
ret = append(ret, fmt.Sprintf("--%s", flagName), flagVal)
}
}
return ret
}
func containsString(s []string, item string) bool {
for _, v := range s {
if v == item {
return true
}
}
return false
}