Browse Source

netclient: pass by value -> reference (#919)

* netclient: pass by value -> reference

Updates various function arguments to accept config.ClientConfig
as a reference to avoid deep copying the struct.

Signed-off-by: John Sahhar <[email protected]>
john s 3 years ago
parent
commit
24f292c934

+ 1 - 3
config/config.go

@@ -6,7 +6,6 @@ package config
 
 import (
 	"fmt"
-	"log"
 	"os"
 
 	"gopkg.in/yaml.v3"
@@ -110,7 +109,6 @@ func readConfig() (*EnvironmentConfig, error) {
 
 func init() {
 	if Config, SetupErr = readConfig(); SetupErr != nil {
-		log.Fatal(SetupErr)
-		os.Exit(2)
+		Config = &EnvironmentConfig{}
 	}
 }

+ 3 - 3
netclient/cli_options/cmds.go

@@ -28,7 +28,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 					err = errors.New("no server address provided")
 					return err
 				}
-				err = command.Join(cfg, pvtKey)
+				err = command.Join(&cfg, pvtKey)
 				return err
 			},
 		},
@@ -43,7 +43,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 				if err != nil {
 					return err
 				}
-				err = command.Leave(cfg, c.String("force") == "yes")
+				err = command.Leave(&cfg, c.String("force") == "yes")
 				return err
 			},
 		},
@@ -58,7 +58,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 				if err != nil {
 					return err
 				}
-				err = command.Pull(cfg)
+				err = command.Pull(&cfg)
 				return err
 			},
 		},

+ 12 - 9
netclient/command/commands.go

@@ -1,6 +1,7 @@
 package command
 
 import (
+	"errors"
 	"strings"
 
 	"github.com/gravitl/netmaker/netclient/config"
@@ -12,22 +13,24 @@ import (
 // JoinComms -- Join the message queue comms network if it doesn't have it
 // tries to ping if already found locally, if fail ping pull for best effort for communication
 func JoinComms(cfg *config.ClientConfig) error {
-	var commsCfg config.ClientConfig
+	commsCfg := &config.ClientConfig{}
 	commsCfg.Network = cfg.Server.CommsNetwork
 	commsCfg.Node.Network = cfg.Server.CommsNetwork
 	commsCfg.Server.AccessKey = cfg.Server.AccessKey
 	commsCfg.Server.GRPCAddress = cfg.Server.GRPCAddress
 	commsCfg.Server.GRPCSSL = cfg.Server.GRPCSSL
 	commsCfg.Server.CoreDNSAddr = cfg.Server.CoreDNSAddr
-	if commsCfg.ConfigFileExists() {
-		commsCfg.ReadConfig()
+	if !commsCfg.ConfigFileExists() {
+		return errors.New("no configuration file exists")
 	}
-	if commsCfg.Node.Name == "" {
+	commsCfg.ReadConfig()
+
+	if len(commsCfg.Node.Name) == 0 {
 		if err := functions.JoinNetwork(commsCfg, "", true); err != nil {
 			return err
 		}
 	} else { // check if comms is currently reachable
-		if err := functions.PingServer(&commsCfg); err != nil {
+		if err := functions.PingServer(commsCfg); err != nil {
 			if err = Pull(commsCfg); err != nil {
 				return err
 			}
@@ -37,10 +40,10 @@ func JoinComms(cfg *config.ClientConfig) error {
 }
 
 // Join - join command to run from cli
-func Join(cfg config.ClientConfig, privateKey string) error {
+func Join(cfg *config.ClientConfig, privateKey string) error {
 	var err error
 	//check if comms network exists
-	if err = JoinComms(&cfg); err != nil {
+	if err = JoinComms(cfg); err != nil {
 		return err
 	}
 
@@ -88,7 +91,7 @@ func Join(cfg config.ClientConfig, privateKey string) error {
 }
 
 // Leave - runs the leave command from cli
-func Leave(cfg config.ClientConfig, force bool) error {
+func Leave(cfg *config.ClientConfig, force bool) error {
 	err := functions.LeaveNetwork(cfg.Network, force)
 	if err != nil {
 		ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
@@ -106,7 +109,7 @@ func Leave(cfg config.ClientConfig, force bool) error {
 }
 
 // Pull - runs pull command from cli
-func Pull(cfg config.ClientConfig) error {
+func Pull(cfg *config.ClientConfig) error {
 	var err error
 	if cfg.Network == "all" {
 		ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)

+ 1 - 0
netclient/config/config.go

@@ -24,6 +24,7 @@ type ClientConfig struct {
 	Daemon          string         `yaml:"daemon"`
 	OperatingSystem string         `yaml:"operatingsystem"`
 	DebugOn         bool           `yaml:"debugon"`
+	
 }
 
 // ServerConfig - struct for dealing with the server information for a netclient

+ 1 - 1
netclient/daemon/common.go

@@ -9,7 +9,7 @@ import (
 )
 
 // InstallDaemon - Calls the correct function to install the netclient as a daemon service on the given operating system.
-func InstallDaemon(cfg config.ClientConfig) error {
+func InstallDaemon(cfg *config.ClientConfig) error {
 	os := runtime.GOOS
 	var err error
 

+ 3 - 3
netclient/functions/join.go

@@ -24,7 +24,7 @@ import (
 )
 
 // JoinNetwork - helps a client join a network
-func JoinNetwork(cfg config.ClientConfig, privateKey string, iscomms bool) error {
+func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) error {
 	if cfg.Node.Network == "" {
 		return errors.New("no network provided")
 	}
@@ -35,7 +35,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string, iscomms bool) error
 		return err
 	}
 
-	err = config.Write(&cfg, cfg.Network)
+	err = config.Write(cfg, cfg.Network)
 	if err != nil {
 		return err
 	}
@@ -211,7 +211,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string, iscomms bool) error
 
 	cfg.Node = node
 
-	setListenPort(oldListenPort, &cfg)
+	setListenPort(oldListenPort, cfg)
 
 	err = config.ModConfig(&cfg.Node)
 	if err != nil {