Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ env:
- PATH=$HOME/gopath/bin:$PATH


go:
- 1.3
- 1.4
- 1.5
- tip

matrix:
include:
- go: 1.3
- go: 1.4
- go: 1.5 TEST_WITH_REAL_API=1
- go: tip
allow_failures:
- go: tip

Expand All @@ -25,15 +24,21 @@ before_install:
- go get -u github.com/axw/gocov/gocov
- go get -u github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover


install:
- make travis_install
- go get github.com/moul/anonuuid/cmd/anonuuid


script:
- make build
- ./scw version
- make travis_run
- make cover
- goveralls -service=travis-ci -v -coverprofile=profile.out
- env | anonuuid
- make build show_version
- if [ -z "${TEST_WITH_REAL_API}" -o -z "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make test; fi
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_login; fi
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup || true; fi
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make cover; fi
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_coveralls; fi
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup; fi

after_success:
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup; fi

after_failure:
- if [ "${TEST_WITH_REAL_API}" -a "${TRAVIS_SCALEWAY_ORGANIZATION}" ]; then make travis_cleanup; fi
68 changes: 48 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ GOCLEAN ?= $(GOCMD) clean
GOINSTALL ?= $(GOCMD) install
GOTEST ?= $(GOCMD) test
GOFMT ?= gofmt -w
GOCOVER ?= $(GOTEST) -covermode=count -v

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

NAME = scw
SRC = cmd/scw
PACKAGES = pkg/api pkg/commands pkg/utils pkg/cli pkg/sshcommand
PACKAGES = pkg/api pkg/commands pkg/utils pkg/cli pkg/sshcommand pkg/config pkg/scwversion
REV = $(shell git rev-parse HEAD || echo "nogit")
TAG = $(shell git describe --tags --always || echo "nogit")
BUILDER = scaleway-cli-builder

ALL_GO_FILES = $(shell find . -type f -name "*.go")

BUILD_LIST = $(foreach int, $(SRC), $(int)_build)
CLEAN_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_clean)
INSTALL_LIST = $(foreach int, $(SRC), $(int)_install)
IREF_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_iref)
TEST_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_test)
FMT_LIST = $(foreach int, $(SRC) $(PACKAGES), $(int)_fmt)
COVER_LIST = $(foreach int, $(PACKAGES), $(int)_cover)
COVERPROFILE_LIST = $(foreach int, $(PACKAGES), $(int)/profile.out)


.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST) $(COVER_LIST)
.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST)


all: build
Expand All @@ -49,10 +50,6 @@ install: $(INSTALL_LIST)
test: $(TEST_LIST)
iref: $(IREF_LIST)
fmt: $(FMT_LIST)
cover:
rm -f profile.out
$(MAKE) $(COVER_LIST)
echo "mode: set" | cat - profile.out > profile.out.tmp && mv profile.out.tmp profile.out


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

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


travis_install:
go get golang.org/x/tools/cmd/cover


travis_run: build
go test -v -covermode=count $(foreach int, $(SRC) $(PACKAGES), ./$(int))


golint:
@go get github.com/golang/lint/golint
@for dir in */; do golint $$dir; done
Expand All @@ -152,3 +137,46 @@ party:
convey:
go get github.com/smartystreets/goconvey
goconvey -cover -port=9042 -workDir="$(realpath .)/pkg" -depth=-1


.PHONY: travis_login
travis_login:
@if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \
echo '{"api_endpoint":"https://api.scaleway.com/","account_endpoint":"https://account.scaleway.com/","organization":"$(TRAVIS_SCALEWAY_ORGANIZATION)","token":"$(TRAVIS_SCALEWAY_TOKEN)"}' > ~/.scwrc && \
chmod 600 ~/.scwrc; \
else \
echo "Cannot login, credentials are missing"; \
fi


.PHONY: cover
cover: profile.out

$(COVERPROFILE_LIST): $(ALL_GO_FILES)
rm -f $@
$(GOCOVER) -coverpkg=./pkg/... -coverprofile=$@ ./$(dir $@)

profile.out: $(COVERPROFILE_LIST)
rm -f $@
echo "mode: set" > $@
cat ./pkg/*/profile.out | grep -v mode: | sort -r | awk '{if($$1 != last) {print $$0;last=$$1}}' >> $@


.PHONY: travis_coveralls
travis_coveralls:
if [ -f ~/.scwrc ]; then goveralls -covermode=count -service=travis-ci -v -coverprofile=profile.out; fi


.PHONY: travis_cleanup
travis_cleanup:
# FIXME: delete only resources created for this project
@if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \
./scw stop -t $(shell ./scw ps -q) || true; \
./scw rm $(shell ./scw ps -aq) || true; \
./scw rmi $(shell ./scw images -q) || true; \
fi


.PHONY: show_version
show_version:
./scw version
190 changes: 5 additions & 185 deletions cmd/scw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,196 +6,16 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
flag "github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/mflag"

"github.com/scaleway/scaleway-cli/pkg/api"
cmds "github.com/scaleway/scaleway-cli/pkg/cli"
"github.com/scaleway/scaleway-cli/pkg/scwversion"
"github.com/scaleway/scaleway-cli/pkg/utils"
)

// CommandListOpts holds a list of parameters
type CommandListOpts struct {
Values *[]string
}

// NewListOpts create an empty CommandListOpts
func NewListOpts() CommandListOpts {
var values []string
return CommandListOpts{
Values: &values,
}
}

// String returns a string representation of a CommandListOpts object
func (opts *CommandListOpts) String() string {
return fmt.Sprintf("%v", []string((*opts.Values)))
}

// Set appends a new value to a CommandListOpts
func (opts *CommandListOpts) Set(value string) error {
(*opts.Values) = append((*opts.Values), value)
return nil
}

func commandUsage(name string) {
}

var (
flAPIEndPoint *string
flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode")
flVerbose = flag.Bool([]string{"V", "-verbose"}, false, "Enable verbose mode")
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
flQuiet = flag.Bool([]string{"q", "-quiet"}, false, "Enable quiet mode")
flSensitive = flag.Bool([]string{"-sensitive"}, false, "Show sensitive data in outputs, i.e. API Token/Organization")
"github.com/scaleway/scaleway-cli/pkg/cli"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
)

func main() {
config, cfgErr := getConfig()
if cfgErr != nil && !os.IsNotExist(cfgErr) {
log.Fatalf("Unable to open .scwrc config file: %v", cfgErr)
}

if config != nil {
defaultComputeAPI := os.Getenv("scaleway_api_endpoint")
if defaultComputeAPI == "" {
defaultComputeAPI = config.ComputeAPI
}
flAPIEndPoint = flag.String([]string{"-api-endpoint"}, defaultComputeAPI, "Set the API endpoint")
}
flag.Parse()

if *flVersion {
showVersion()
return
}

if flAPIEndPoint != nil {
os.Setenv("scaleway_api_endpoint", *flAPIEndPoint)
}

if *flSensitive {
os.Setenv("SCW_SENSITIVE", "1")
}

if *flDebug {
os.Setenv("DEBUG", "1")
}

utils.Quiet(*flQuiet)
initLogging(os.Getenv("DEBUG") != "", *flVerbose)

args := flag.Args()
if len(args) < 1 {
usage()
}
name := args[0]

args = args[1:]

for _, cmd := range cmds.Commands {
if cmd.Name() == name {
cmd.Flag.SetOutput(ioutil.Discard)
err := cmd.Flag.Parse(args)
if err != nil {
log.Fatalf("usage: scw %s", cmd.UsageLine)
}
if cmd.Name() != "login" && cmd.Name() != "help" && cmd.Name() != "version" {
if cfgErr != nil {
if name != "login" && config == nil {
log.Debugf("cfgErr: %v", cfgErr)
fmt.Fprintf(os.Stderr, "You need to login first: 'scw login'\n")
os.Exit(1)
}
}
api, err := getScalewayAPI()
if err != nil {
log.Fatalf("unable to initialize scw api: %s", err)
}
cmd.API = api
}
err = cmd.Exec(cmd, cmd.Flag.Args())
if err != nil {
log.Fatalf("Cannot execute '%s': %v", cmd.Name(), err)
}
if cmd.API != nil {
cmd.API.Sync()
}
os.Exit(0)
}
}

log.Fatalf("scw: unknown subcommand %s\nRun 'scw help' for usage.", name)
}

func usage() {
cmds.CmdHelp.Exec(cmds.CmdHelp, []string{})
os.Exit(1)
}

// getConfig returns the Scaleway CLI config file for the current user
func getConfig() (*api.Config, error) {
scwrcPath, err := utils.GetConfigFilePath()
if err != nil {
return nil, err
}

stat, err := os.Stat(scwrcPath)
// we don't care if it fails, the user just won't see the warning
if err == nil {
mode := stat.Mode()
if mode&0066 != 0 {
log.Fatalf("Permissions %#o for .scwrc are too open.", mode)
}
}

file, err := ioutil.ReadFile(scwrcPath)
ec, err := cli.Start(os.Args[1:], nil)
if err != nil {
return nil, err
}
var config api.Config
err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
// check if he has an old scwrc version
if config.AccountAPI == "" {
config.AccountAPI = "https://account.scaleway.com"
config.Save()
}
if os.Getenv("scaleway_api_endpoint") == "" {
os.Setenv("scaleway_api_endpoint", config.ComputeAPI)
}
return &config, nil
}

// getScalewayAPI returns a ScalewayAPI using the user config file
func getScalewayAPI() (*api.ScalewayAPI, error) {
// We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object
config, err := getConfig()
if err != nil {
return nil, err
}
return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.AccountAPI, config.Organization, config.Token)
}

func showVersion() {
fmt.Printf("scw version %s, build %s\n", scwversion.VERSION, scwversion.GITCOMMIT)
}

func initLogging(debug bool, verbose bool) {
log.SetOutput(os.Stderr)
if debug {
log.SetLevel(log.DebugLevel)
} else if verbose {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
logrus.Fatalf("%s", err)
}
os.Exit(ec)
}
Loading