|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "io/ioutil" |
6 | 7 | "os" |
7 | 8 |
|
8 | 9 | log "github.com/Sirupsen/logrus" |
9 | 10 | flag "github.com/docker/docker/pkg/mflag" |
10 | 11 |
|
| 12 | + "github.com/scaleway/scaleway-cli/api" |
11 | 13 | cmds "github.com/scaleway/scaleway-cli/commands" |
| 14 | + "github.com/scaleway/scaleway-cli/scwversion" |
| 15 | + "github.com/scaleway/scaleway-cli/utils" |
| 16 | +) |
| 17 | + |
| 18 | +// CommandListOpts holds a list of parameters |
| 19 | +type CommandListOpts struct { |
| 20 | + Values *[]string |
| 21 | +} |
| 22 | + |
| 23 | +// NewListOpts create an empty CommandListOpts |
| 24 | +func NewListOpts() CommandListOpts { |
| 25 | + var values []string |
| 26 | + return CommandListOpts{ |
| 27 | + Values: &values, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// String returns a string representation of a CommandListOpts object |
| 32 | +func (opts *CommandListOpts) String() string { |
| 33 | + return fmt.Sprintf("%v", []string((*opts.Values))) |
| 34 | +} |
| 35 | + |
| 36 | +// Set appends a new value to a CommandListOpts |
| 37 | +func (opts *CommandListOpts) Set(value string) error { |
| 38 | + (*opts.Values) = append((*opts.Values), value) |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +func commandUsage(name string) { |
| 43 | +} |
| 44 | + |
| 45 | +var ( |
| 46 | + flAPIEndPoint *string |
| 47 | + flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode") |
| 48 | + flVersion = flag.Bool([]string{"v", "--version"}, false, "Print version information and quit") |
12 | 49 | ) |
13 | 50 |
|
14 | 51 | func main() { |
@@ -76,6 +113,56 @@ func main() { |
76 | 113 | log.Fatalf("scw: unknown subcommand %s\nRun 'scw help' for usage.", name) |
77 | 114 | } |
78 | 115 |
|
| 116 | +func usage() { |
| 117 | + cmds.CmdHelp.Exec(cmds.CmdHelp, []string{}) |
| 118 | + os.Exit(1) |
| 119 | +} |
| 120 | + |
| 121 | +// getConfig returns the Scaleway CLI config file for the current user |
| 122 | +func getConfig() (*api.Config, error) { |
| 123 | + scwrcPath, err := utils.GetConfigFilePath() |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + stat, err := os.Stat(scwrcPath) |
| 129 | + // we don't care if it fails, the user just won't see the warning |
| 130 | + if err == nil { |
| 131 | + mode := stat.Mode() |
| 132 | + if mode&0066 != 0 { |
| 133 | + log.Fatalf("Permissions %#o for .scwrc are too open.", mode) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + file, err := ioutil.ReadFile(scwrcPath) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + var config api.Config |
| 142 | + err = json.Unmarshal(file, &config) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + if os.Getenv("scaleway_api_endpoint") == "" { |
| 147 | + os.Setenv("scaleway_api_endpoint", config.APIEndPoint) |
| 148 | + } |
| 149 | + return &config, nil |
| 150 | +} |
| 151 | + |
| 152 | +// getScalewayAPI returns a ScalewayAPI using the user config file |
| 153 | +func getScalewayAPI() (*api.ScalewayAPI, error) { |
| 154 | + // We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object |
| 155 | + config, err := getConfig() |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.Organization, config.Token) |
| 160 | +} |
| 161 | + |
| 162 | +func showVersion() { |
| 163 | + fmt.Printf("scw version %s, build %s\n", scwversion.VERSION, scwversion.GITCOMMIT) |
| 164 | +} |
| 165 | + |
79 | 166 | func initLogging(debug bool) { |
80 | 167 | log.SetOutput(os.Stderr) |
81 | 168 | if debug { |
|
0 commit comments