Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iss#6: implemented logic to determine how skywire-updater finds config file. #7

Merged
merged 12 commits into from
Apr 19, 2019
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
*.out
.idea/
coverage/
db.json
db.json
.vscode/
31 changes: 21 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
.DEFAULT_GOAL := help
.PHONY : check lint install-linters dep test

OPTS=GO111MODULE=on

check: lint test ## Run linters and tests

lint: ## Run linters. Use make install-linters first.
GO111MODULE=off vendorcheck ./...
GO111MODULE=on golangci-lint run -c .golangci.yml ./...
${OPTS} golangci-lint run -c .golangci.yml ./...
# The govet version in golangci-lint is out of date and has spurious warnings, run it separately
GO111MODULE=on go vet -all ./...
${OPTS} go vet -all ./...

install:
GO111MODULE=on go build -o ~/.skycoin/bin/skywire-updater ./cmd/skywire-updater
install: dep
${OPTS} go build -o ~/.skycoin/bin/skywire-updater ./cmd/skywire-updater
export PATH=$PATH:$HOME/.skycoin/bin
skywire-updater init-config
cp -R ./scripts ~/.skycoin/skywire-updater/scripts

install-linters: ## Install linters
GO111MODULE=on go get -u github.com/FiloSottile/vendorcheck
${OPTS} go get -u github.com/FiloSottile/vendorcheck
# For some reason this install method is not recommended, see https://github.com/golangci/golangci-lint#install
# However, they suggest `curl ... | bash` which we should not do
GO111MODULE=on go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
GO111MODULE=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
${OPTS} go get -u golang.org/x/tools/cmd/goimports

format: ## Formats the code. Must have goimports installed (use make install-linters).
GO111MODULE=on goimports -w -local github.com/watercompany/skywire-updater ./pkg
GO111MODULE=on goimports -w -local github.com/watercompany/skywire-updater ./cmd
${OPTS} goimports -w -local github.com/skycoin/skywire-updater ./pkg
${OPTS} goimports -w -local github.com/skycoin/skywire-updater ./cmd

dep: ## sorts dependencies
GO111MODULE=on go mod vendor -v
${OPTS} go mod vendor -v

test: ## Run tests for net
@mkdir -p coverage/
GO111MODULE=on go test -race -tags no_ci -cover -timeout=5m ./pkg/...
${OPTS} go test -race -tags no_ci -cover -timeout=5m ./pkg/...

help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
$ skywire-updater -h

skywire-updater is responsible for checking for updates, and updating services
associated with skywire.
associated with skywire.

It takes one optional argument [config-path] which specifies the path to the
configuration file to use. If no [config-path] is specified, the following
directories are searched in order:

1. /Users/anonymous/.skycoin/skywire-updater/config.yml
2. /usr/local/skycoin/skywire-updater/config.yml
1. /your_working_directory/config.yml
2. /home/anonymous/.skycoin/skywire-updater/config.yml
3. /usr/local/skycoin/skywire-updater/config.yml

Usage:
skywire-updater [config-path] [flags]
Expand Down
5 changes: 3 additions & 2 deletions cmd/skywire-updater/commands/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
evanlinjin marked this conversation as resolved.
Show resolved Hide resolved

"github.com/skycoin/skywire-updater/internal/pathutil"
"github.com/skycoin/skywire/pkg/util/pathutil"

"github.com/skycoin/skywire-updater/pkg/update"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ var initConfigCmd = &cobra.Command{
}

func init() {
initConfigCmd.Flags().StringVarP(&output, "output", "o", defaultConfigPaths[0], "path of output config file.")
initConfigCmd.Flags().StringVarP(&output, "output", "o", defaultPaths[pathutil.HomeLoc], "path of output config file.")
initConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.")
initConfigCmd.Flags().StringVarP(&mode, "mode", "m", homeMode, fmt.Sprintf("config generation mode. Valid values: %v", initConfigModes))
}
56 changes: 26 additions & 30 deletions cmd/skywire-updater/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -11,55 +10,50 @@ import (
"github.com/skycoin/skycoin/src/util/logging"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-updater/internal/pathutil"
"github.com/skycoin/skywire/pkg/util/pathutil"

"github.com/skycoin/skywire-updater/pkg/api"
"github.com/skycoin/skywire-updater/pkg/store"
"github.com/skycoin/skywire-updater/pkg/update"
)

var log = logging.MustGetLogger("skywire-updater")
const configEnv = "SW_UPDATER_CONFIG"

var defaultConfigPaths = [2]string{
filepath.Join(pathutil.HomeDir(), ".skycoin/skywire-updater/config.yml"),
}
var log = logging.MustGetLogger("skywire-updater")

func findConfigPath() (string, error) {
log.Info("configuration file is not explicitly specified, attempting to find one in default paths ...")
for i, cPath := range defaultConfigPaths {
if _, err := os.Stat(cPath); err != nil {
log.Infof("- [%d/%d] '%s' does not exist", i, len(defaultConfigPaths), cPath)
} else {
log.Infof("- [%d/%d] '%s' exists (using this one)", i, len(defaultConfigPaths), cPath)
return cPath, nil
}
// UpdaterDefaults returns the default config paths for Skywire-Updater.
func UpdaterDefaults() pathutil.ConfigPaths {
paths := make(pathutil.ConfigPaths)
if wd, err := os.Getwd(); err == nil {
paths[pathutil.WorkingDirLoc] = filepath.Join(wd, "config.yml")
}
return "", errors.New("no configuration file found")
paths[pathutil.HomeLoc] = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire-updater/config.yml")
paths[pathutil.LocalLoc] = "/usr/local/skycoin/skywire-updater/config.yml"
return paths
}

var defaultPaths = UpdaterDefaults()

// RootCmd is the command to run when no sub-commands are specified.
// TraverseChildren set to true enables cobra to parse local flags on each command before executing target command
var RootCmd = &cobra.Command{
Use: "skywire-updater [config-path]",
Use: "skywire-updater [config-path]",
Short: "Updates skywire services",
Long: fmt.Sprintf(`
skywire-updater is responsible for checking for updates, and updating services
associated with skywire.

It takes one optional argument [config-path] which specifies the path to the
configuration file to use. If no [config-path] is specified, the following
configuration file to use. If no [config-path] is specified, the following
directories are searched in order:

1. %s
2. %s`, defaultConfigPaths[0], defaultConfigPaths[1]),
Short: "Updates skywire services",
2. %s
3. %s`, defaultPaths[pathutil.WorkingDirLoc], defaultPaths[pathutil.HomeLoc], defaultPaths[pathutil.LocalLoc]),
TraverseChildren: true,
Run: func(_ *cobra.Command, args []string) {
var configPath string
if len(args) == 0 {
var err error
if configPath, err = findConfigPath(); err != nil {
log.WithError(err).Fatal()
}
} else {
configPath = args[0]
}

configPath := pathutil.FindConfigPath(args, 0, configEnv, defaultPaths)

log.Infof("config path: '%s'", configPath)
conf := update.NewConfig(".", "./bin")
Expand Down Expand Up @@ -95,11 +89,13 @@ directories are searched in order:
},
}

// Execute executes root CLI command.
// Execute executes root CLI command and add subcommands.
func Execute() {

RootCmd.AddCommand(initConfigCmd)

if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}

}
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ module github.com/skycoin/skywire-updater

require (
github.com/go-chi/chi v4.0.2+incompatible
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.6 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/sirupsen/logrus v1.3.0 // indirect
github.com/skycoin/skycoin v0.25.1
github.com/skycoin/skywire v0.1.2-0.20190418071242-c3a15be79321
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25 // indirect
golang.org/x/sys v0.0.0-20190306071516-a98ae47d97a5 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480 // indirect
golang.org/x/net v0.0.0-20190419010253-1f3472d942ba // indirect
golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be // indirect
golang.org/x/tools v0.0.0-20190418235243-4796d4bd3df0 // indirect
gopkg.in/yaml.v2 v2.2.2
)
Loading