Skip to content

Commit 66e69e3

Browse files
committed
Merged cli.go and main.go
1 parent db8a846 commit 66e69e3

File tree

2 files changed

+87
-100
lines changed

2 files changed

+87
-100
lines changed

cli.go

Lines changed: 0 additions & 100 deletions
This file was deleted.

main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io/ioutil"
67
"os"
78

89
log "github.com/Sirupsen/logrus"
910
flag "github.com/docker/docker/pkg/mflag"
1011

12+
"github.com/scaleway/scaleway-cli/api"
1113
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")
1249
)
1350

1451
func main() {
@@ -76,6 +113,56 @@ func main() {
76113
log.Fatalf("scw: unknown subcommand %s\nRun 'scw help' for usage.", name)
77114
}
78115

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+
79166
func initLogging(debug bool) {
80167
log.SetOutput(os.Stderr)
81168
if debug {

0 commit comments

Comments
 (0)