Selaa lähdekoodia

refactor(cmd/ovpm/user): improve create user command structure and commentation

Static ip flag handling is restructured. Doccoments are improved
semantically and also by the number.
Mustafa Arici 8 vuotta sitten
vanhempi
commit
bb453408b2
1 muutettua tiedostoa jossa 23 lisäystä ja 14 poistoa
  1. 23 14
      cmd/ovpm/user.go

+ 23 - 14
cmd/ovpm/user.go

@@ -66,11 +66,11 @@ var userCreateCommand = cli.Command{
 	Flags: []cli.Flag{
 		cli.StringFlag{
 			Name:  "username, u",
-			Usage: "username for the vpn user",
+			Usage: "username for the vpn user (required)",
 		},
 		cli.StringFlag{
 			Name:  "password, p",
-			Usage: "password for the vpn user",
+			Usage: "password for the vpn user (required)",
 		},
 		cli.BoolFlag{
 			Name:  "no-gw",
@@ -85,47 +85,56 @@ var userCreateCommand = cli.Command{
 			Usage: "this user has admin rights",
 		},
 	},
+	// userCreate action has two modes. Bulk mode
 	Action: func(c *cli.Context) error {
 		action = "user:create"
+
+		// Init subcmd flags.
 		username := c.String("username")
 		password := c.String("password")
 		noGW := c.Bool("no-gw")
 		static := c.String("static")
 		admin := c.Bool("admin")
+		var hostid uint32
 
+		// Check required flags.
 		if username == "" || password == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
 			return fmt.Errorf("username and password is required")
 		}
 
-		if static != "" && !govalidator.IsIPv4(static) {
-			fmt.Println("--static flag takes a valid IPv4 address")
-			fmt.Println()
-			fmt.Println(cli.ShowSubcommandHelp(c))
-			exit(1)
-			return fmt.Errorf("static should be either valid ipv4 addr or emptystring")
-		}
-		var hostid uint32
-		fmt.Println("static:", static)
+		// Should set static ip?
 		if static != "" {
+			// Is ip addr valid?
+			if !govalidator.IsIPv4(static) {
+				fmt.Println("--static flag takes a valid IPv4 address")
+				fmt.Println()
+				fmt.Println(cli.ShowSubcommandHelp(c))
+				exit(1)
+				return fmt.Errorf("static should be either valid ipv4 addr or emptystring")
+			}
+			// Convert ip addr to hostid(uint32).
 			h := ovpm.IP2HostID(net.ParseIP(static).To4())
 			if h == 0 {
+				// hostid being 0 points to dynamic ip addr,
+				// hence ambiguous meaning here.
+				// This is perceived as an error.
 				fmt.Printf("can not parse %s as IPv4", static)
 				fmt.Println()
 				fmt.Println(cli.ShowSubcommandHelp(c))
 				exit(1)
 				return fmt.Errorf("can't parse ipv4")
 			}
-
-			hostid = h
+			hostid = h // set hostid of the user
 		}
 
-		//conn := getConn(c.String("port"))
+		// Prepare a gRPC call for creating the user.
 		conn := getConn(c.GlobalString("daemon-port"))
 		defer conn.Close()
 		userSvc := pb.NewUserServiceClient(conn)
 
+		// Call the gRPC daemon.
 		response, err := userSvc.Create(context.Background(),
 			&pb.UserCreateRequest{Username: username, Password: password, NoGw: noGW, HostId: hostid, IsAdmin: admin},
 		)