|
6 | 6 | package main |
7 | 7 |
|
8 | 8 | import ( |
9 | | - "encoding/json" |
10 | | - "fmt" |
11 | | - "io/ioutil" |
12 | 9 | "os" |
13 | 10 |
|
14 | | - log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" |
15 | | - flag "github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/mflag" |
16 | | - |
17 | | - "github.com/scaleway/scaleway-cli/pkg/api" |
18 | | - cmds "github.com/scaleway/scaleway-cli/pkg/cli" |
19 | | - "github.com/scaleway/scaleway-cli/pkg/scwversion" |
20 | | - "github.com/scaleway/scaleway-cli/pkg/utils" |
21 | | -) |
22 | | - |
23 | | -// CommandListOpts holds a list of parameters |
24 | | -type CommandListOpts struct { |
25 | | - Values *[]string |
26 | | -} |
27 | | - |
28 | | -// NewListOpts create an empty CommandListOpts |
29 | | -func NewListOpts() CommandListOpts { |
30 | | - var values []string |
31 | | - return CommandListOpts{ |
32 | | - Values: &values, |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -// String returns a string representation of a CommandListOpts object |
37 | | -func (opts *CommandListOpts) String() string { |
38 | | - return fmt.Sprintf("%v", []string((*opts.Values))) |
39 | | -} |
40 | | - |
41 | | -// Set appends a new value to a CommandListOpts |
42 | | -func (opts *CommandListOpts) Set(value string) error { |
43 | | - (*opts.Values) = append((*opts.Values), value) |
44 | | - return nil |
45 | | -} |
46 | | - |
47 | | -func commandUsage(name string) { |
48 | | -} |
49 | | - |
50 | | -var ( |
51 | | - flAPIEndPoint *string |
52 | | - flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode") |
53 | | - flVerbose = flag.Bool([]string{"V", "-verbose"}, false, "Enable verbose mode") |
54 | | - flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit") |
55 | | - flQuiet = flag.Bool([]string{"q", "-quiet"}, false, "Enable quiet mode") |
56 | | - flSensitive = flag.Bool([]string{"-sensitive"}, false, "Show sensitive data in outputs, i.e. API Token/Organization") |
| 11 | + "github.com/scaleway/scaleway-cli/pkg/cli" |
| 12 | + "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" |
57 | 13 | ) |
58 | 14 |
|
59 | 15 | func main() { |
60 | | - config, cfgErr := getConfig() |
61 | | - if cfgErr != nil && !os.IsNotExist(cfgErr) { |
62 | | - log.Fatalf("Unable to open .scwrc config file: %v", cfgErr) |
63 | | - } |
64 | | - |
65 | | - if config != nil { |
66 | | - defaultComputeAPI := os.Getenv("scaleway_api_endpoint") |
67 | | - if defaultComputeAPI == "" { |
68 | | - defaultComputeAPI = config.ComputeAPI |
69 | | - } |
70 | | - flAPIEndPoint = flag.String([]string{"-api-endpoint"}, defaultComputeAPI, "Set the API endpoint") |
71 | | - } |
72 | | - flag.Parse() |
73 | | - |
74 | | - if *flVersion { |
75 | | - showVersion() |
76 | | - return |
77 | | - } |
78 | | - |
79 | | - if flAPIEndPoint != nil { |
80 | | - os.Setenv("scaleway_api_endpoint", *flAPIEndPoint) |
81 | | - } |
82 | | - |
83 | | - if *flSensitive { |
84 | | - os.Setenv("SCW_SENSITIVE", "1") |
85 | | - } |
86 | | - |
87 | | - if *flDebug { |
88 | | - os.Setenv("DEBUG", "1") |
89 | | - } |
90 | | - |
91 | | - utils.Quiet(*flQuiet) |
92 | | - initLogging(os.Getenv("DEBUG") != "", *flVerbose) |
93 | | - |
94 | | - args := flag.Args() |
95 | | - if len(args) < 1 { |
96 | | - usage() |
97 | | - } |
98 | | - name := args[0] |
99 | | - |
100 | | - args = args[1:] |
101 | | - |
102 | | - for _, cmd := range cmds.Commands { |
103 | | - if cmd.Name() == name { |
104 | | - cmd.Flag.SetOutput(ioutil.Discard) |
105 | | - err := cmd.Flag.Parse(args) |
106 | | - if err != nil { |
107 | | - log.Fatalf("usage: scw %s", cmd.UsageLine) |
108 | | - } |
109 | | - if cmd.Name() != "login" && cmd.Name() != "help" && cmd.Name() != "version" { |
110 | | - if cfgErr != nil { |
111 | | - if name != "login" && config == nil { |
112 | | - log.Debugf("cfgErr: %v", cfgErr) |
113 | | - fmt.Fprintf(os.Stderr, "You need to login first: 'scw login'\n") |
114 | | - os.Exit(1) |
115 | | - } |
116 | | - } |
117 | | - api, err := getScalewayAPI() |
118 | | - if err != nil { |
119 | | - log.Fatalf("unable to initialize scw api: %s", err) |
120 | | - } |
121 | | - cmd.API = api |
122 | | - } |
123 | | - err = cmd.Exec(cmd, cmd.Flag.Args()) |
124 | | - if err != nil { |
125 | | - log.Fatalf("Cannot execute '%s': %v", cmd.Name(), err) |
126 | | - } |
127 | | - if cmd.API != nil { |
128 | | - cmd.API.Sync() |
129 | | - } |
130 | | - os.Exit(0) |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - log.Fatalf("scw: unknown subcommand %s\nRun 'scw help' for usage.", name) |
135 | | -} |
136 | | - |
137 | | -func usage() { |
138 | | - cmds.CmdHelp.Exec(cmds.CmdHelp, []string{}) |
139 | | - os.Exit(1) |
140 | | -} |
141 | | - |
142 | | -// getConfig returns the Scaleway CLI config file for the current user |
143 | | -func getConfig() (*api.Config, error) { |
144 | | - scwrcPath, err := utils.GetConfigFilePath() |
145 | | - if err != nil { |
146 | | - return nil, err |
147 | | - } |
148 | | - |
149 | | - stat, err := os.Stat(scwrcPath) |
150 | | - // we don't care if it fails, the user just won't see the warning |
151 | | - if err == nil { |
152 | | - mode := stat.Mode() |
153 | | - if mode&0066 != 0 { |
154 | | - log.Fatalf("Permissions %#o for .scwrc are too open.", mode) |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - file, err := ioutil.ReadFile(scwrcPath) |
| 16 | + ec, err := cli.Start(os.Args[1:], nil) |
159 | 17 | if err != nil { |
160 | | - return nil, err |
161 | | - } |
162 | | - var config api.Config |
163 | | - err = json.Unmarshal(file, &config) |
164 | | - if err != nil { |
165 | | - return nil, err |
166 | | - } |
167 | | - // check if he has an old scwrc version |
168 | | - if config.AccountAPI == "" { |
169 | | - config.AccountAPI = "https://account.scaleway.com" |
170 | | - config.Save() |
171 | | - } |
172 | | - if os.Getenv("scaleway_api_endpoint") == "" { |
173 | | - os.Setenv("scaleway_api_endpoint", config.ComputeAPI) |
174 | | - } |
175 | | - return &config, nil |
176 | | -} |
177 | | - |
178 | | -// getScalewayAPI returns a ScalewayAPI using the user config file |
179 | | -func getScalewayAPI() (*api.ScalewayAPI, error) { |
180 | | - // We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object |
181 | | - config, err := getConfig() |
182 | | - if err != nil { |
183 | | - return nil, err |
184 | | - } |
185 | | - return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.AccountAPI, config.Organization, config.Token) |
186 | | -} |
187 | | - |
188 | | -func showVersion() { |
189 | | - fmt.Printf("scw version %s, build %s\n", scwversion.VERSION, scwversion.GITCOMMIT) |
190 | | -} |
191 | | - |
192 | | -func initLogging(debug bool, verbose bool) { |
193 | | - log.SetOutput(os.Stderr) |
194 | | - if debug { |
195 | | - log.SetLevel(log.DebugLevel) |
196 | | - } else if verbose { |
197 | | - log.SetLevel(log.InfoLevel) |
198 | | - } else { |
199 | | - log.SetLevel(log.WarnLevel) |
| 18 | + logrus.Fatalf("%s", err) |
200 | 19 | } |
| 20 | + os.Exit(ec) |
201 | 21 | } |
0 commit comments