diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index f69b4cd0..1b48393a 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -43,7 +43,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { if err != nil { return err } - err = command.Leave(cfg) + err = command.Leave(cfg, c.String("force") == "yes") return err }, }, diff --git a/netclient/cli_options/flags.go b/netclient/cli_options/flags.go index 1a42b99b..5ac99ab9 100644 --- a/netclient/cli_options/flags.go +++ b/netclient/cli_options/flags.go @@ -198,5 +198,11 @@ func GetFlags(hostname string) []cli.Flag { Value: "yes", Usage: "Checks for IP changes if 'yes'. Ignores if 'no'. Yes by default.", }, + &cli.StringFlag{ + Name: "force", + EnvVars: []string{"NETCLIENT_FORCE"}, + Value: "no", + Usage: "Allows to run the command with force, if otherwise prevented.", + }, } } diff --git a/netclient/command/commands.go b/netclient/command/commands.go index e469e479..f3251646 100644 --- a/netclient/command/commands.go +++ b/netclient/command/commands.go @@ -49,7 +49,7 @@ func Join(cfg config.ClientConfig, privateKey string) error { if err != nil && !cfg.DebugOn { if !strings.Contains(err.Error(), "ALREADY_INSTALLED") { ncutils.PrintLog("error installing: "+err.Error(), 1) - err = functions.LeaveNetwork(cfg.Network) + err = functions.LeaveNetwork(cfg.Network, true) if err != nil { err = functions.WipeLocal(cfg.Network) if err != nil { @@ -88,8 +88,8 @@ func Join(cfg config.ClientConfig, privateKey string) error { } // Leave - runs the leave command from cli -func Leave(cfg config.ClientConfig) error { - err := functions.LeaveNetwork(cfg.Network) +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) } else { diff --git a/netclient/functions/common.go b/netclient/functions/common.go index 946969f8..345e277d 100644 --- a/netclient/functions/common.go +++ b/netclient/functions/common.go @@ -124,7 +124,7 @@ func Uninstall() error { ncutils.PrintLog("continuing uninstall without leaving networks", 1) } else { for _, network := range networks { - err = LeaveNetwork(network) + err = LeaveNetwork(network, true) if err != nil { ncutils.PrintLog("Encounter issue leaving network "+network+": "+err.Error(), 1) } @@ -147,13 +147,16 @@ func Uninstall() error { } // LeaveNetwork - client exits a network -func LeaveNetwork(network string) error { +func LeaveNetwork(network string, force bool) error { cfg, err := config.ReadConfig(network) if err != nil { return err } servercfg := cfg.Server node := cfg.Node + if node.NetworkSettings.IsComms == "yes" && !force { + return errors.New("COMMS_NET - You are trying to leave the comms network. This will break network updates. Unless you re-join. If you really want to leave, run with --force=yes.") + } if node.IsServer != "yes" { var wcclient nodepb.NodeServiceClient diff --git a/netclient/functions/mqhandlers.go b/netclient/functions/mqhandlers.go index e4a6b236..21893f6d 100644 --- a/netclient/functions/mqhandlers.go +++ b/netclient/functions/mqhandlers.go @@ -61,7 +61,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { case models.NODE_DELETE: ncutils.PrintLog(fmt.Sprintf("received delete request for %s", nodeCfg.Node.Name), 0) unsubscribeNode(client, &nodeCfg) - if err = LeaveNetwork(nodeCfg.Node.Network); err != nil { + if err = LeaveNetwork(nodeCfg.Node.Network, true); err != nil { if !strings.Contains("rpc error", err.Error()) { ncutils.PrintLog(fmt.Sprintf("failed to leave, please check that local files for network %s were removed", nodeCfg.Node.Network), 0) return