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 11 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/
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ install-linters: ## Install linters
# 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=on go get github.com/golangci/golangci-lint/cmd/[email protected]

format: ## Formats the code. Must have goimports installed (use make install-linters).
GO111MODULE=on goimports -w -local github.com/watercompany/skywire-updater ./pkg
Expand Down
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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer it if we read the [config-file] via a CLI flag, instead of a new sub-command.  😜

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I was thinking to use CLI flags originally too; I will go back and review the code again and see if I could make this change. Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Evan. I made a change to the code. It can now reads in [config-file] via CLI flag instead of having to type a new Cobra subcommand. It turns out that by default Cobra only parses local flags on the target command, any local flags on parent commands are ignored. So by enabling this argument Command.TraverseChildren allows Cobra to parse local flags on each command before executing the target command. Please let me know if this logic is okay. Thanks!

Expand Down
7 changes: 3 additions & 4 deletions cmd/skywire-updater/commands/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os"
"path/filepath"

"github.com/skycoin/skywire-updater/pkg/update"
"github.com/skycoin/skywire/pkg/util/pathutil"
"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-updater/pkg/update"
)

const (
Expand Down Expand Up @@ -98,7 +97,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))
}
58 changes: 26 additions & 32 deletions cmd/skywire-updater/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,57 @@
package commands

import (
"errors"
"fmt"
"net"
"net/http"
"os"
"path/filepath"

"github.com/skycoin/skycoin/src/util/logging"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-updater/internal/pathutil"
"github.com/skycoin/skywire-updater/pkg/api"
"github.com/skycoin/skywire-updater/pkg/store"
"github.com/skycoin/skywire-updater/pkg/update"
"github.com/skycoin/skywire/pkg/util/pathutil"
"github.com/spf13/cobra"
)

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, UpdaterDefaults())
evanlinjin marked this conversation as resolved.
Show resolved Hide resolved

log.Infof("config path: '%s'", configPath)
conf := update.NewConfig(".", "./bin")
Expand Down Expand Up @@ -95,11 +87,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)
}

}
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ 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
gopkg.in/yaml.v2 v2.2.2
)
Loading