Skip to content

Commit cb601b5

Browse files
authored
feat: add config management commands (#153)
* feat: add config commands * chore: fix error message
1 parent 2c7aea2 commit cb601b5

19 files changed

Lines changed: 207 additions & 128 deletions

cmd/check.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"google.golang.org/grpc/status"
1515
)
1616

17-
func checkSchemaCmd() *cobra.Command {
18-
var host, comp, file, namespaceID string
17+
func checkSchemaCmd(cdk *CDK) *cobra.Command {
18+
var comp, file, namespaceID string
1919
var req stencilv1beta1.CheckCompatibilityRequest
2020

2121
cmd := &cobra.Command{
@@ -37,7 +37,7 @@ func checkSchemaCmd() *cobra.Command {
3737
return err
3838
}
3939

40-
client, cancel, err := createClient(cmd)
40+
client, cancel, err := createClient(cmd, cdk)
4141
if err != nil {
4242
return err
4343
}
@@ -62,9 +62,6 @@ func checkSchemaCmd() *cobra.Command {
6262
},
6363
}
6464

65-
cmd.Flags().StringVar(&host, "host", "", "Server host address eg: localhost:8000")
66-
cmd.MarkFlagRequired("host")
67-
6865
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Parent namespace ID")
6966
cmd.MarkFlagRequired("namespace")
7067

cmd/client.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1420
func 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+
}

cmd/config.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func configCmd(cdk *CDK) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "config <command>",
13+
Short: "Manage stencil CLI configuration",
14+
}
15+
cmd.AddCommand(configInitCommand(cdk))
16+
cmd.AddCommand(configListCommand(cdk))
17+
return cmd
18+
}
19+
20+
func configInitCommand(cdk *CDK) *cobra.Command {
21+
return &cobra.Command{
22+
Use: "init",
23+
Short: "Initialize CLI configuration",
24+
Example: heredoc.Doc(`
25+
$ stencil config init
26+
`),
27+
Annotations: map[string]string{
28+
"group": "core",
29+
},
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
if err := cdk.Config.Init(&ClientConfig{}); err != nil {
32+
return err
33+
}
34+
35+
fmt.Printf("Config created: %v\n", cdk.Config.File())
36+
return nil
37+
},
38+
}
39+
}
40+
41+
func configListCommand(cdk *CDK) *cobra.Command {
42+
var cmd = &cobra.Command{
43+
Use: "list",
44+
Short: "List client configuration settings",
45+
Example: heredoc.Doc(`
46+
$ stencil config list
47+
`),
48+
Annotations: map[string]string{
49+
"group": "core",
50+
},
51+
RunE: func(cmd *cobra.Command, args []string) error {
52+
data, err := cdk.Config.Read()
53+
if err != nil {
54+
return ErrClientConfigNotFound
55+
}
56+
57+
fmt.Println(data)
58+
return nil
59+
},
60+
}
61+
return cmd
62+
}

cmd/create.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"google.golang.org/grpc/status"
1616
)
1717

18-
func createSchemaCmd() *cobra.Command {
19-
var host, format, comp, file, namespaceID string
18+
func createSchemaCmd(cdk *CDK) *cobra.Command {
19+
var format, comp, file, namespaceID string
2020
var req stencilv1beta1.CreateSchemaRequest
2121

2222
cmd := &cobra.Command{
@@ -36,7 +36,7 @@ func createSchemaCmd() *cobra.Command {
3636

3737
spinner := printer.Spin("")
3838
defer spinner.Stop()
39-
client, cancel, err := createClient(cmd)
39+
client, cancel, err := createClient(cmd, cdk)
4040
if err != nil {
4141
return err
4242
}
@@ -66,9 +66,6 @@ func createSchemaCmd() *cobra.Command {
6666
},
6767
}
6868

69-
cmd.Flags().StringVar(&host, "host", "", "Stencil host address eg: localhost:8000")
70-
cmd.MarkFlagRequired("host")
71-
7269
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Namespace ID")
7370
cmd.MarkFlagRequired("namespace")
7471

cmd/delete.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
func deleteSchemaCmd() *cobra.Command {
14-
var host, namespaceID string
13+
func deleteSchemaCmd(cdk *CDK) *cobra.Command {
14+
var namespaceID string
1515
var req stencilv1beta1.DeleteSchemaRequest
1616
var reqVer stencilv1beta1.DeleteVersionRequest
1717
var version int32
@@ -27,7 +27,7 @@ func deleteSchemaCmd() *cobra.Command {
2727
spinner := printer.Spin("")
2828
defer spinner.Stop()
2929

30-
client, cancel, err := createClient(cmd)
30+
client, cancel, err := createClient(cmd, cdk)
3131
if err != nil {
3232
return err
3333
}
@@ -60,9 +60,6 @@ func deleteSchemaCmd() *cobra.Command {
6060
},
6161
}
6262

63-
cmd.Flags().StringVar(&host, "host", "", "Stencil host address eg: localhost:8000")
64-
cmd.MarkFlagRequired("host")
65-
6663
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Parent namespace ID")
6764
cmd.MarkFlagRequired("namespace")
6865

cmd/diff.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import (
1818
"google.golang.org/protobuf/types/descriptorpb"
1919
)
2020

21-
func diffSchemaCmd() *cobra.Command {
21+
func diffSchemaCmd(cdk *CDK) *cobra.Command {
2222
var fullname string
23-
var host string
2423
var namespace string
2524
var earlierVersion int32
2625
var laterVersion int32
@@ -91,7 +90,7 @@ func diffSchemaCmd() *cobra.Command {
9190
VersionId: laterVersion,
9291
}
9392

94-
client, cancel, err := createClient(cmd)
93+
client, cancel, err := createClient(cmd, cdk)
9594
if err != nil {
9695
return err
9796
}
@@ -146,8 +145,6 @@ func diffSchemaCmd() *cobra.Command {
146145
},
147146
}
148147

149-
cmd.Flags().StringVar(&host, "host", "", "Stencil host address eg: localhost:8000")
150-
cmd.MarkFlagRequired("host")
151148
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Parent namespace ID")
152149
cmd.MarkFlagRequired("namespace")
153150
cmd.Flags().Int32Var(&earlierVersion, "earlier-version", 0, "Earlier version of the schema")

cmd/download.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
func downloadSchemaCmd() *cobra.Command {
14-
var host, output, namespaceID string
13+
func downloadSchemaCmd(cdk *CDK) *cobra.Command {
14+
var output, namespaceID string
1515
var version int32
1616
var data []byte
1717

@@ -25,7 +25,7 @@ func downloadSchemaCmd() *cobra.Command {
2525
RunE: func(cmd *cobra.Command, args []string) error {
2626
spinner := printer.Spin("")
2727
defer spinner.Stop()
28-
client, cancel, err := createClient(cmd)
28+
client, cancel, err := createClient(cmd, cdk)
2929
if err != nil {
3030
return err
3131
}
@@ -46,8 +46,6 @@ func downloadSchemaCmd() *cobra.Command {
4646
return nil
4747
},
4848
}
49-
cmd.Flags().StringVar(&host, "host", "", "Stencil host address eg: localhost:8000")
50-
cmd.MarkFlagRequired("host")
5149

5250
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Parent namespace ID")
5351
cmd.MarkFlagRequired("namespace")

cmd/edit.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
func editSchemaCmd() *cobra.Command {
14-
var host, comp, namespaceID string
13+
func editSchemaCmd(cdk *CDK) *cobra.Command {
14+
var comp, namespaceID string
1515
var req stencilv1beta1.UpdateSchemaMetadataRequest
1616

1717
cmd := &cobra.Command{
@@ -25,7 +25,7 @@ func editSchemaCmd() *cobra.Command {
2525
spinner := printer.Spin("")
2626
defer spinner.Stop()
2727

28-
client, cancel, err := createClient(cmd)
28+
client, cancel, err := createClient(cmd, cdk)
2929
if err != nil {
3030
return err
3131
}
@@ -48,9 +48,6 @@ func editSchemaCmd() *cobra.Command {
4848
},
4949
}
5050

51-
cmd.Flags().StringVar(&host, "host", "", "Server host address eg: localhost:8000")
52-
cmd.MarkFlagRequired("host")
53-
5451
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Parent namespace ID")
5552
cmd.MarkFlagRequired("namespace")
5653

cmd/errors.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
)
8+
9+
var (
10+
ErrClientConfigNotFound = errors.New(heredoc.Doc(`
11+
Stencil client config not found.
12+
Run "stencil config init" to initialize a new client config or
13+
Run "stencil help environment" for more information.
14+
`))
15+
ErrClientConfigHostNotFound = errors.New(heredoc.Doc(`
16+
Stencil client config "host" not found.
17+
Pass stencil server host with "--host" flag or
18+
set host in stencil config.
19+
Run "stencil config <subcommand>" or
20+
"stencil help environment" for more information.
21+
`))
22+
ErrClientNotAuthorized = errors.New(heredoc.Doc(`
23+
Stencil auth error. Stencil requires an auth header.
24+
25+
Run "stencil help auth" for more information.
26+
`))
27+
)

cmd/graph.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"google.golang.org/protobuf/types/descriptorpb"
1313
)
1414

15-
func graphSchemaCmd() *cobra.Command {
16-
var host, output, namespaceID string
15+
func graphSchemaCmd(cdk *CDK) *cobra.Command {
16+
var output, namespaceID string
1717
var version int32
1818

1919
cmd := &cobra.Command{
@@ -25,7 +25,7 @@ func graphSchemaCmd() *cobra.Command {
2525
$ stencil schema graph booking -n odpf -v 1 -o ./vis.dot
2626
`),
2727
RunE: func(cmd *cobra.Command, args []string) error {
28-
client, cancel, err := createClient(cmd)
28+
client, cancel, err := createClient(cmd, cdk)
2929
if err != nil {
3030
return err
3131
}
@@ -63,9 +63,6 @@ func graphSchemaCmd() *cobra.Command {
6363
},
6464
}
6565

66-
cmd.Flags().StringVar(&host, "host", "", "stencil host address eg: localhost:8000")
67-
cmd.MarkFlagRequired("host")
68-
6966
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "provide namespace/group or entity name")
7067
cmd.MarkFlagRequired("namespace")
7168

0 commit comments

Comments
 (0)