1
0
Эх сурвалжийг харах

refactor(cli): put a return if the execution will be stopped

Mustafa Arici 8 жил өмнө
parent
commit
998bdac106
3 өөрчлөгдсөн 28 нэмэгдсэн , 3 устгасан
  1. 5 0
      cmd/ovpm/net.go
  2. 17 2
      cmd/ovpm/user.go
  3. 6 1
      cmd/ovpm/vpn.go

+ 5 - 0
cmd/ovpm/net.go

@@ -45,6 +45,7 @@ var netDefineCommand = cli.Command{
 		if name == "" || cidr == "" || typ == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("name, cidr, type are required")
 		}
 
 		switch ovpm.NetworkTypeFromString(typ) {
@@ -54,6 +55,7 @@ var netDefineCommand = cli.Command{
 				fmt.Println()
 				fmt.Println(cli.ShowSubcommandHelp(c))
 				exit(1)
+				return fmt.Errorf("via is a IPv4 addr")
 			}
 
 		case ovpm.SERVERNET:
@@ -62,6 +64,7 @@ var netDefineCommand = cli.Command{
 				fmt.Println()
 				fmt.Println(cli.ShowSubcommandHelp(c))
 				exit(1)
+				return fmt.Errorf("via can only be used when type is ROUTE")
 			}
 		default: // Means UNDEFINEDNET
 			fmt.Printf("undefined network type %s", typ)
@@ -71,6 +74,7 @@ var netDefineCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("undefined network type")
 		}
 
 		conn := getConn(c.GlobalString("daemon-port"))
@@ -190,6 +194,7 @@ var netUndefineCommand = cli.Command{
 		if name == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("name is required")
 		}
 
 		conn := getConn(c.GlobalString("daemon-port"))

+ 17 - 2
cmd/ovpm/user.go

@@ -96,14 +96,18 @@ var userCreateCommand = cli.Command{
 		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("--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)
 		if static != "" {
 			h := ovpm.IP2HostID(net.ParseIP(static).To4())
 			if h == 0 {
@@ -111,6 +115,7 @@ var userCreateCommand = cli.Command{
 				fmt.Println()
 				fmt.Println(cli.ShowSubcommandHelp(c))
 				exit(1)
+				return fmt.Errorf("can't parse ipv4")
 			}
 
 			hostid = h
@@ -186,6 +191,7 @@ var userUpdateCommand = cli.Command{
 		if username == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("username is required")
 		}
 
 		// Check whether if all flags are are empty.
@@ -194,14 +200,16 @@ var userUpdateCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("nothing is supplied")
 		}
 
 		// Given that static is set, check whether it's IPv4.
 		if static != "" && !govalidator.IsIPv4(static) {
-			fmt.Println("--static flag takes a valid ipv4 address")
+			fmt.Println("--static flag takes a valid IPv4 address")
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("static can either be a valid IPv4 addr or empty string")
 		}
 		var staticPref pb.UserUpdateRequest_StaticPref
 		staticPref = pb.UserUpdateRequest_NOPREFSTATIC
@@ -217,6 +225,7 @@ var userUpdateCommand = cli.Command{
 					fmt.Println()
 					fmt.Println(cli.ShowSubcommandHelp(c))
 					exit(1)
+					return fmt.Errorf("can't parse IP addr")
 				}
 
 				hostid = h
@@ -233,6 +242,7 @@ var userUpdateCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("static and no-static are mutually exclusive")
 		case static == "" && !noStatic:
 		default:
 			// means no pref
@@ -253,6 +263,7 @@ var userUpdateCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("gw and no-gw are mutually exclusive")
 		default:
 			gwPref = pb.UserUpdateRequest_NOPREF
 
@@ -273,6 +284,7 @@ var userUpdateCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("admin and no-admin are mutually exclusive")
 		}
 
 		//conn := getConn(c.String("port"))
@@ -316,6 +328,7 @@ var userDeleteCommand = cli.Command{
 		if username == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("username is required")
 		}
 
 		//conn := getConn(c.String("port"))
@@ -351,6 +364,7 @@ var userRenewCommand = cli.Command{
 		if username == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("username is required")
 		}
 
 		//conn := getConn(c.String("port"))
@@ -392,6 +406,7 @@ var userGenconfigCommand = cli.Command{
 		if username == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("username is required")
 		}
 		if output == "" {
 			output = username + ".ovpn"

+ 6 - 1
cmd/ovpm/vpn.go

@@ -78,7 +78,7 @@ var vpnInitCommand = cli.Command{
 			logrus.Errorf("'hostname' is required")
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
-
+			return fmt.Errorf("hostname is required")
 		}
 
 		port := c.String("port")
@@ -99,6 +99,7 @@ var vpnInitCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("net should be CIDR")
 		}
 
 		dns := c.String("dns")
@@ -107,6 +108,7 @@ var vpnInitCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("dns IPv4 address")
 		}
 
 		conn := getConn(c.GlobalString("daemon-port"))
@@ -167,6 +169,7 @@ var vpnUpdateCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("hostname is required")
 		}
 
 		if ipblock != "" {
@@ -199,12 +202,14 @@ var vpnUpdateCommand = cli.Command{
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("dns should be IPv4")
 		}
 
 		if !(ipblock != "" || dns != "") {
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			exit(1)
+			return fmt.Errorf("ipblock and dns should be provided")
 		}
 
 		conn := getConn(c.GlobalString("daemon-port"))