Skip to content

Commit

Permalink
Fixed #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Oct 11, 2019
1 parent fa02788 commit 5a5249b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ var argsCmd = &cobra.Command{
Aliases: []string{"arguments"},
Short: "View arguments",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()

config.CheckLogin(nodeName, "", "")
user.CheckLogin(nodeName, "", "")

node, err := config.User.GetNode(nodeName)
node, err := user.GetNode(nodeName)
if err != nil {
logger.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var loginCmd = &cobra.Command{
Use: "login [--node default]",
Short: "Login with a node",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()

if config.CheckLogin(nodeName, username, password) != nil {
if user.CheckLogin(nodeName, username, password) != nil {
logger.Info("Logged in")
}
},
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ var logoutCmd = &cobra.Command{
Use: "logout [--node default]",
Short: "Logout of a node",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()

node, err := config.User.GetNode(nodeName)
node, err := user.GetNode(nodeName)
if err != nil {
logger.Error(err)
}

node.Token = ""
config.User.UpdateNode(nodeName, *node)
user.UpdateNode(nodeName, *node)

logger.Info("Logged out")
},
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ var nodeCmd = &cobra.Command{
Use: "node",
Short: "Manage processing nodes",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()

for k, n := range config.User.Nodes {
for k, n := range user.Nodes {
if logger.VerboseFlag {
logger.Info(k + " - " + n.String())
} else {
Expand All @@ -44,9 +44,9 @@ var addCmd = &cobra.Command{
Short: "Add a new processing node",
Args: cobra.ExactValidArgs(2),
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()

if err := config.User.AddNode(args[0], args[1]); err != nil {
if err := user.AddNode(args[0], args[1]); err != nil {
logger.Error(err)
}
},
Expand All @@ -58,9 +58,9 @@ var removeCmd = &cobra.Command{
Aliases: []string{"delete", "rm", "del"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()

if !config.User.RemoveNode(args[0]) {
if !user.RemoveNode(args[0]) {
logger.Error("Cannot remove node " + args[0] + " (does it exist?)")
}
},
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ var force bool
var rootCmd = &cobra.Command{
Use: "odm [flags] <images> [<gcp>] [args]",
Short: "A command line tool to process aerial imagery in the cloud",
Version: "1.0.0",
Version: "1.1.0",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()
user := config.Initialize()
if len(args) == 0 {
cmd.Help()
os.Exit(0)
Expand All @@ -64,7 +64,7 @@ var rootCmd = &cobra.Command{

logger.Debug("Options: " + strings.Join(options, " "))

info := config.CheckLogin(nodeName, "", "")
info := user.CheckLogin(nodeName, "", "")

// Check max images
if len(inputFiles) > info.MaxImages {
Expand All @@ -73,7 +73,7 @@ var rootCmd = &cobra.Command{

logger.Debug("NodeODM version: " + info.Version)

node, err := config.User.GetNode(nodeName)
node, err := user.GetNode(nodeName)
if err != nil {
logger.Error(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
// if it does, it attempts to login
// it it doesn't, returns node.Info()
// on error, it prints a message and exits
func CheckLogin(nodeName string, username string, password string) *odm.InfoResponse {
node, err := User.GetNode(nodeName)
func (c Configuration) CheckLogin(nodeName string, username string, password string) *odm.InfoResponse {
node, err := c.GetNode(nodeName)
if err != nil {
logger.Error(err)
}
Expand All @@ -47,7 +47,7 @@ func CheckLogin(nodeName string, username string, password string) *odm.InfoResp
logger.Error(err)
}

User.UpdateNode(nodeName, *node)
c.UpdateNode(nodeName, *node)
} else {
logger.Error(err)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ import (
homedir "github.com/mitchellh/go-homedir"
)

// User contains the user's configuration
var User Configuration

// NewConfiguration creates a new configuration from a specified file path
func NewConfiguration(filePath string) Configuration {
conf := Configuration{}
Expand All @@ -57,7 +54,7 @@ type Configuration struct {
}

// Initialize the configuration
func Initialize() {
func Initialize() Configuration {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
Expand All @@ -66,14 +63,15 @@ func Initialize() {
}

cfgPath := filepath.Join(home, ".odm.json")
user := Configuration{}

if exists, _ := fs.FileExists(cfgPath); exists {
// Read existing config
User = loadFromFile(cfgPath)
user = loadFromFile(cfgPath)
} else {
// Download public nodes, choose a default
nodes := GetPublicNodes()
User := NewConfiguration(cfgPath)
user = NewConfiguration(cfgPath)

logger.Info("Found " + strconv.Itoa(len(nodes)) + " public nodes")

Expand All @@ -83,11 +81,13 @@ func Initialize() {
randomNode := nodes[rand.Intn(len(nodes))]

logger.Info("Setting default node to " + randomNode.String())
User.AddNode("default", randomNode.Url)
user.AddNode("default", randomNode.Url)

logger.Info("Initialized configuration at " + cfgPath)
}
}

return user
}

func saveToFile(conf Configuration, filePath string) {
Expand Down

0 comments on commit 5a5249b

Please sign in to comment.