Reduce code duplication in client actions
This commit is contained in:
		| @@ -28,9 +28,15 @@ import ( | |||||||
| 	"google.golang.org/protobuf/types/known/timestamppb" | 	"google.golang.org/protobuf/types/known/timestamppb" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | type contextKey string | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	contextKeyConfig         contextKey = "config" | ||||||
|  | 	contextKeyClientConnFunc contextKey = "clientConnFunc" | ||||||
|  | ) | ||||||
|  |  | ||||||
| func ActionClientGet(c *cli.Context) error { | func ActionClientGet(c *cli.Context) error { | ||||||
| 	addr := c.String("addr") | 	conn, err := connFromContext(c) | ||||||
| 	conn, err := grpc.DialContext(c.Context, addr, grpc.WithInsecure()) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -65,22 +71,7 @@ func ActionClientGet(c *cli.Context) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func ActionClientUpload(c *cli.Context) error { | func ActionClientUpload(c *cli.Context) error { | ||||||
| 	cfg, err := getConfig(c) | 	conn, err := connFromContext(c) | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	addr := cfg.Client.DefaultServer |  | ||||||
| 	if c.IsSet("addr") { |  | ||||||
| 		addr = c.String("addr") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	clientCreds, err := cfg.Client.Creds() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) |  | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -118,22 +109,7 @@ func ActionClientUpload(c *cli.Context) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func ActionClientList(c *cli.Context) error { | func ActionClientList(c *cli.Context) error { | ||||||
| 	cfg, err := getConfig(c) | 	conn, err := connFromContext(c) | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	addr := cfg.Client.DefaultServer |  | ||||||
| 	if c.IsSet("addr") { |  | ||||||
| 		addr = c.String("addr") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	clientCreds, err := cfg.Client.Creds() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) |  | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -153,22 +129,7 @@ func ActionClientList(c *cli.Context) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func ActionClientDelete(c *cli.Context) error { | func ActionClientDelete(c *cli.Context) error { | ||||||
| 	cfg, err := getConfig(c) | 	conn, err := connFromContext(c) | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	addr := cfg.Client.DefaultServer |  | ||||||
| 	if c.IsSet("addr") { |  | ||||||
| 		addr = c.String("addr") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	clientCreds, err := cfg.Client.Creds() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) |  | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -362,22 +323,7 @@ func ActionClientLogin(c *cli.Context) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func ActionClientPassword(c *cli.Context) error { | func ActionClientPassword(c *cli.Context) error { | ||||||
| 	cfg, err := getConfig(c) | 	conn, err := connFromContext(c) | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	addr := cfg.Client.DefaultServer |  | ||||||
| 	if c.IsSet("addr") { |  | ||||||
| 		addr = c.String("addr") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	clientCreds, err := cfg.Client.Creds() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) |  | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -406,21 +352,7 @@ func ActionClientPassword(c *cli.Context) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func ActionClientUpdate(c *cli.Context) error { | func ActionClientUpdate(c *cli.Context) error { | ||||||
| 	cfg, err := getConfig(c) | 	conn, err := connFromContext(c) | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	addr := cfg.Client.DefaultServer |  | ||||||
| 	if c.IsSet("addr") { |  | ||||||
| 		addr = c.String("addr") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	clientCreds, err := cfg.Client.Creds() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) |  | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| @@ -467,3 +399,40 @@ func ActionClientUpdate(c *cli.Context) error { | |||||||
| 	fmt.Printf("Wrote latest binary to %s", outputPath) | 	fmt.Printf("Wrote latest binary to %s", outputPath) | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func BeforeClient(c *cli.Context) error { | ||||||
|  | 	cfg, err := getConfig(c) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	c.Context = context.WithValue(c.Context, contextKeyConfig, cfg) | ||||||
|  |  | ||||||
|  | 	addr := cfg.Client.DefaultServer | ||||||
|  | 	if c.IsSet("addr") { | ||||||
|  | 		addr = c.String("addr") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	clientCreds, err := cfg.Client.Creds() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	connFunc := func() (*grpc.ClientConn, error) { | ||||||
|  | 		return grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) | ||||||
|  | 	} | ||||||
|  | 	c.Context = context.WithValue(c.Context, contextKeyClientConnFunc, connFunc) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func connFromContext(c *cli.Context) (*grpc.ClientConn, error) { | ||||||
|  | 	connFunc := c.Context.Value(contextKeyClientConnFunc).(func() (*grpc.ClientConn, error)) | ||||||
|  | 	return connFunc() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func configFromContext(c *cli.Context) (*config.Config, error) { | ||||||
|  | 	cfg, ok := c.Context.Value(contextKeyConfig).(*config.Config) | ||||||
|  | 	if !ok { | ||||||
|  | 		return getConfig(c) | ||||||
|  | 	} | ||||||
|  | 	return cfg, nil | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user