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 <john@gravitl.com>
This commit is contained in:
john s
2022-03-20 23:43:17 -05:00
committed by GitHub
parent eb974dbd63
commit 24f292c934
6 changed files with 21 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,7 +9,7 @@ import (
) )
// InstallDaemon - Calls the correct function to install the netclient as a daemon service on the given operating system. // 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 os := runtime.GOOS
var err error var err error

View File

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