From ef09a54967b275de6e319aa9058f5a1d9cc09015 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@tbnet.works>
Date: Tue, 19 Jul 2022 10:21:12 +0000
Subject: [PATCH] Resolve "Device creation in prompt mode stores some old
 values"

See merge request danet/gosdn!319

Co-authored-by: Malte Bauch <malte.bauch@extern.h-da.de>
---
 cli/cmd/deviceCreate.go | 10 ++++++++--
 cli/cmd/deviceSet.go    |  5 +++++
 cli/cmd/login.go        |  5 +++++
 cli/cmd/logout.go       |  5 +++++
 cli/cmd/pndCreate.go    |  5 +++++
 cli/cmd/userCreate.go   |  5 +++++
 cli/cmd/userDelete.go   |  5 +++++
 cli/cmd/userGet.go      |  5 +++++
 cli/cmd/userUpdate.go   |  5 +++++
 9 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/cli/cmd/deviceCreate.go b/cli/cmd/deviceCreate.go
index efd182a0d..d8380a581 100644
--- a/cli/cmd/deviceCreate.go
+++ b/cli/cmd/deviceCreate.go
@@ -87,8 +87,14 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`,
 		for _, r := range resp.GetResponses() {
 			spinner.Success("Device has been created with ID: ", r.GetId())
 		}
+
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		deviceName, opcode, address, username, password = "", "", "", "", ""
+	},
 }
 
 var deviceName string
@@ -100,6 +106,6 @@ func init() {
 	deviceCreateCmd.Flags().StringVar(&deviceName, "name", "", "add a device name (optional)")
 	deviceCreateCmd.Flags().StringVar(&opcode, "type", "", "generation target (csbi or plugin)")
 	deviceCreateCmd.Flags().StringVarP(&address, "address", "a", "", "address of a gnmi target, e.g. 192.168.1.1:6030")
-	deviceCreateCmd.Flags().StringVarP(&username, "username", "u", "admin", "username for a gnmi resource")
-	deviceCreateCmd.Flags().StringVarP(&password, "password", "p", "arista", "password for a gnmi resource")
+	deviceCreateCmd.Flags().StringVarP(&username, "username", "u", "", "username for a gnmi resource")
+	deviceCreateCmd.Flags().StringVarP(&password, "password", "p", "", "password for a gnmi resource")
 }
diff --git a/cli/cmd/deviceSet.go b/cli/cmd/deviceSet.go
index 15806d356..baaae3419 100644
--- a/cli/cmd/deviceSet.go
+++ b/cli/cmd/deviceSet.go
@@ -105,6 +105,11 @@ To enable replacing behaviour (destructive!), set the --replace flag."`,
 		}
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		replace, forcePush, file = false, false, ""
+	},
 }
 
 func fileContentToString(path string) (string, error) {
diff --git a/cli/cmd/login.go b/cli/cmd/login.go
index 77e655f81..0596b6852 100644
--- a/cli/cmd/login.go
+++ b/cli/cmd/login.go
@@ -88,6 +88,11 @@ var loginCmd = &cobra.Command{
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		nbUserName, nbUserPwd, controllerAPIEndpoint = "", "", viper.GetString("controllerAPIEndpoint")
+	},
 }
 
 func init() {
diff --git a/cli/cmd/logout.go b/cli/cmd/logout.go
index a8268b764..2aeb75e9a 100644
--- a/cli/cmd/logout.go
+++ b/cli/cmd/logout.go
@@ -68,6 +68,11 @@ var logoutCmd = &cobra.Command{
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		nbUserName = ""
+	},
 }
 
 func init() {
diff --git a/cli/cmd/pndCreate.go b/cli/cmd/pndCreate.go
index a2859604d..4ff4f8c37 100644
--- a/cli/cmd/pndCreate.go
+++ b/cli/cmd/pndCreate.go
@@ -66,6 +66,11 @@ passed using parameters.`,
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		pndName, pndDescription, pndDefaultSbi = "", "", "openconfig"
+	},
 }
 
 var pndName string
diff --git a/cli/cmd/userCreate.go b/cli/cmd/userCreate.go
index bd0b87888..8246187e1 100644
--- a/cli/cmd/userCreate.go
+++ b/cli/cmd/userCreate.go
@@ -70,6 +70,11 @@ var userCreateCmd = &cobra.Command{
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		nbUserName, nbUserPwd, nbUserRole = "", "", ""
+	},
 }
 
 var nbUserRole string
diff --git a/cli/cmd/userDelete.go b/cli/cmd/userDelete.go
index c8bfb2151..5efe1f925 100644
--- a/cli/cmd/userDelete.go
+++ b/cli/cmd/userDelete.go
@@ -58,6 +58,11 @@ var userDeleteCmd = &cobra.Command{
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		nbUserName = ""
+	},
 }
 
 func init() {
diff --git a/cli/cmd/userGet.go b/cli/cmd/userGet.go
index 1a487a3a7..5fcacd84e 100644
--- a/cli/cmd/userGet.go
+++ b/cli/cmd/userGet.go
@@ -63,6 +63,11 @@ var userGetCmd = &cobra.Command{
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		nbUserName = ""
+	},
 }
 
 func init() {
diff --git a/cli/cmd/userUpdate.go b/cli/cmd/userUpdate.go
index 7288d9e4d..b9a2bd1ff 100644
--- a/cli/cmd/userUpdate.go
+++ b/cli/cmd/userUpdate.go
@@ -71,6 +71,11 @@ var userUpdateCmd = &cobra.Command{
 
 		return nil
 	},
+	PostRun: func(cmd *cobra.Command, args []string) {
+		// Necessary for prompt mode. The flag variables have to be resetted,
+		// since in prompt mode the program keeps running.
+		nbUserID, nbUserName, nbUserPwd, nbUserRole = "", "", "", ""
+	},
 }
 
 var nbUserID string
-- 
GitLab