@@ -5,12 +5,18 @@ import (
55 "errors"
66 "time"
77
8+ "github.com/odpf/salt/cmdx"
9+ "github.com/odpf/salt/config"
810 stencilv1beta1 "github.com/odpf/stencil/proto/odpf/stencil/v1beta1"
911 "github.com/spf13/cobra"
1012 "google.golang.org/grpc"
1113 "google.golang.org/grpc/credentials/insecure"
1214)
1315
16+ type ClientConfig struct {
17+ Host string `yaml:"host" cmdx:"host"`
18+ }
19+
1420func createConnection (ctx context.Context , host string ) (* grpc.ClientConn , error ) {
1521 opts := []grpc.DialOption {
1622 grpc .WithTransportCredentials (insecure .NewCredentials ()),
@@ -20,13 +26,16 @@ func createConnection(ctx context.Context, host string) (*grpc.ClientConn, error
2026 return grpc .DialContext (ctx , host , opts ... )
2127}
2228
23- func createClient (cmd * cobra.Command ) (stencilv1beta1.StencilServiceClient , func (), error ) {
24- host , err := cmd . Flags (). GetString ( "host" )
29+ func createClient (cmd * cobra.Command , cdk * CDK ) (stencilv1beta1.StencilServiceClient , func (), error ) {
30+ c , err := loadClientConfig ( cmd , cdk . Config )
2531 if err != nil {
2632 return nil , nil , err
2733 }
34+
35+ host := c .Host
36+
2837 if host == "" {
29- return nil , nil , errors . New ( " \" host \" not set" )
38+ return nil , nil , ErrClientConfigHostNotFound
3039 }
3140
3241 dialTimeoutCtx , dialCancel := context .WithTimeout (cmd .Context (), time .Second * 2 )
@@ -44,3 +53,18 @@ func createClient(cmd *cobra.Command) (stencilv1beta1.StencilServiceClient, func
4453 client := stencilv1beta1 .NewStencilServiceClient (conn )
4554 return client , cancel , nil
4655}
56+
57+ func loadClientConfig (cmd * cobra.Command , cmdxConfig * cmdx.Config ) (* ClientConfig , error ) {
58+ var clientConfig ClientConfig
59+
60+ if err := cmdxConfig .Load (
61+ & clientConfig ,
62+ cmdx .WithFlags (cmd .Flags ()),
63+ ); err != nil {
64+ if ! errors .Is (err , new (config.ConfigFileNotFoundError )) {
65+ return nil , ErrClientConfigNotFound
66+ }
67+ }
68+
69+ return & clientConfig , nil
70+ }
0 commit comments