Skip to content

Commit 5f5c350

Browse files
committed
Merge pull request #128 from scaleway/test-cli
Test cli with credentials (linked to #126)
2 parents 3fe3d61 + 3a8f19b commit 5f5c350

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2520
-423
lines changed

.travis.yml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ env:
99
- PATH=$HOME/gopath/bin:$PATH
1010

1111

12-
go:
13-
- 1.3
14-
- 1.4
15-
- 1.5
16-
- tip
17-
1812
matrix:
13+
include:
14+
- go: 1.3
15+
- go: 1.4
16+
- go: 1.5 TEST_WITH_REAL_API=1
17+
- go: tip
1918
allow_failures:
2019
- go: tip
2120

@@ -25,15 +24,21 @@ before_install:
2524
- go get -u github.com/axw/gocov/gocov
2625
- go get -u github.com/mattn/goveralls
2726
- go get golang.org/x/tools/cmd/cover
28-
29-
30-
install:
31-
- make travis_install
27+
- go get github.com/moul/anonuuid/cmd/anonuuid
3228

3329

3430
script:
35-
- make build
36-
- ./scw version
37-
- make travis_run
38-
- make cover
39-
- goveralls -service=travis-ci -v -coverprofile=profile.out
31+
- env | anonuuid
32+
- make build show_version
33+
- if [ -z "${TEST_WITH_REAL_API}" -o -z "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make test; fi
34+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_login; fi
35+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup || true; fi
36+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make cover; fi
37+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_coveralls; fi
38+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup; fi
39+
40+
after_success:
41+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup; fi
42+
43+
after_failure:
44+
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup; fi

Makefile

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ GOCLEAN ?= $(GOCMD) clean
55
GOINSTALL ?= $(GOCMD) install
66
GOTEST ?= $(GOCMD) test
77
GOFMT ?= gofmt -w
8+
GOCOVER ?= $(GOTEST) -covermode=count -v
89

910
FPM_VERSION ?= $(shell ./dist/scw-Darwin-i386 --version | sed 's/.*v\([0-9.]*\),.*/\1/g')
1011
FPM_DOCKER ?= \
@@ -24,22 +25,22 @@ FPM_ARGS ?= \
2425

2526
NAME = scw
2627
SRC = cmd/scw
27-
PACKAGES = pkg/api pkg/commands pkg/utils pkg/cli pkg/sshcommand
28+
PACKAGES = pkg/api pkg/commands pkg/utils pkg/cli pkg/sshcommand pkg/config pkg/scwversion
2829
REV = $(shell git rev-parse HEAD || echo "nogit")
2930
TAG = $(shell git describe --tags --always || echo "nogit")
3031
BUILDER = scaleway-cli-builder
31-
32+
ALL_GO_FILES = $(shell find . -type f -name "*.go")
3233

3334
BUILD_LIST = $(foreach int, $(SRC), $(int)_build)
3435
CLEAN_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_clean)
3536
INSTALL_LIST = $(foreach int, $(SRC), $(int)_install)
3637
IREF_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_iref)
3738
TEST_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_test)
3839
FMT_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_fmt)
39-
COVER_LIST = $(foreach int, $(PACKAGES), $(int)_cover)
40+
COVERPROFILE_LIST = $(foreach int, $(PACKAGES), $(int)/profile.out)
4041

4142

42-
.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST) $(COVER_LIST)
43+
.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST)
4344

4445

4546
all: build
@@ -49,10 +50,6 @@ install: $(INSTALL_LIST)
4950
test: $(TEST_LIST)
5051
iref: $(IREF_LIST)
5152
fmt: $(FMT_LIST)
52-
cover:
53-
rm -f profile.out
54-
$(MAKE) $(COVER_LIST)
55-
echo "mode: set" | cat - profile.out > profile.out.tmp && mv profile.out.tmp profile.out
5653

5754

5855
.git:
@@ -76,9 +73,6 @@ $(IREF_LIST): %_iref: pkg/scwversion/version.go
7673
$(GOTEST) -i ./$*
7774
$(TEST_LIST): %_test:
7875
$(GOTEST) ./$*
79-
$(COVER_LIST): %_cover:
80-
$(GOTEST) -coverprofile=file-profile.out ./$*
81-
if [ -f file-profile.out ]; then cat file-profile.out | grep -v "mode: set" >> profile.out || true; rm -f file-profile.out; fi
8276
$(FMT_LIST): %_fmt:
8377
$(GOFMT) ./$*
8478

@@ -130,15 +124,6 @@ packages:
130124
#publish_packages:
131125
# docker run -v $(PWD)/dist moul/dput ppa:moul/scw dist/scw_$(FPM_VERSION)_arm.changes
132126

133-
134-
travis_install:
135-
go get golang.org/x/tools/cmd/cover
136-
137-
138-
travis_run: build
139-
go test -v -covermode=count $(foreach int, $(SRC) $(PACKAGES), ./$(int))
140-
141-
142127
golint:
143128
@go get github.com/golang/lint/golint
144129
@for dir in */; do golint $$dir; done
@@ -152,3 +137,46 @@ party:
152137
convey:
153138
go get github.com/smartystreets/goconvey
154139
goconvey -cover -port=9042 -workDir="$(realpath .)/pkg" -depth=-1
140+
141+
142+
.PHONY: travis_login
143+
travis_login:
144+
@if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \
145+
echo '{"api_endpoint":"https://api.scaleway.com/","account_endpoint":"https://account.scaleway.com/","organization":"$(TRAVIS_SCALEWAY_ORGANIZATION)","token":"$(TRAVIS_SCALEWAY_TOKEN)"}' > ~/.scwrc && \
146+
chmod 600 ~/.scwrc; \
147+
else \
148+
echo "Cannot login, credentials are missing"; \
149+
fi
150+
151+
152+
.PHONY: cover
153+
cover: profile.out
154+
155+
$(COVERPROFILE_LIST): $(ALL_GO_FILES)
156+
rm -f $@
157+
$(GOCOVER) -coverpkg=./pkg/... -coverprofile=$@ ./$(dir $@)
158+
159+
profile.out: $(COVERPROFILE_LIST)
160+
rm -f $@
161+
echo "mode: set" > $@
162+
cat ./pkg/*/profile.out | grep -v mode: | sort -r | awk '{if($$1 != last) {print $$0;last=$$1}}' >> $@
163+
164+
165+
.PHONY: travis_coveralls
166+
travis_coveralls:
167+
if [ -f ~/.scwrc ]; then goveralls -covermode=count -service=travis-ci -v -coverprofile=profile.out; fi
168+
169+
170+
.PHONY: travis_cleanup
171+
travis_cleanup:
172+
# FIXME: delete only resources created for this project
173+
@if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \
174+
./scw stop -t $(shell ./scw ps -q) || true; \
175+
./scw rm $(shell ./scw ps -aq) || true; \
176+
./scw rmi $(shell ./scw images -q) || true; \
177+
fi
178+
179+
180+
.PHONY: show_version
181+
show_version:
182+
./scw version

cmd/scw/main.go

Lines changed: 5 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -6,196 +6,16 @@
66
package main
77

88
import (
9-
"encoding/json"
10-
"fmt"
11-
"io/ioutil"
129
"os"
1310

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"
5713
)
5814

5915
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)
15917
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)
20019
}
20+
os.Exit(ec)
20121
}

0 commit comments

Comments
 (0)