From 24f292c93459c51ae9576b288fed36a6774cf639 Mon Sep 17 00:00:00 2001 From: john s Date: Sun, 20 Mar 2022 23:43:17 -0500 Subject: [PATCH] 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 --- config/config.go | 4 +--- netclient/cli_options/cmds.go | 6 +++--- netclient/command/commands.go | 21 ++++++++++++--------- netclient/config/config.go | 1 + netclient/daemon/common.go | 2 +- netclient/functions/join.go | 6 +++--- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/config/config.go b/config/config.go index cba6d80b..b634a833 100644 --- a/config/config.go +++ b/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{} } } diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index 1b48393a..26314e2b 100644 --- a/netclient/cli_options/cmds.go +++ b/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 }, }, diff --git a/netclient/command/commands.go b/netclient/command/commands.go index ab0014b5..40d9c397 100644 --- a/netclient/command/commands.go +++ b/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) diff --git a/netclient/config/config.go b/netclient/config/config.go index 110e73a0..19cb68c5 100644 --- a/netclient/config/config.go +++ b/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 diff --git a/netclient/daemon/common.go b/netclient/daemon/common.go index 5d4e9efb..6a7d0982 100644 --- a/netclient/daemon/common.go +++ b/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 diff --git a/netclient/functions/join.go b/netclient/functions/join.go index 8f79175e..226c4ae2 100644 --- a/netclient/functions/join.go +++ b/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 {