Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit 7d36af0

Browse files
committed
little cmd/grpc_helper improvements
1 parent 2b406c4 commit 7d36af0

File tree

7 files changed

+35
-22
lines changed

7 files changed

+35
-22
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ OPEN_BROWSER =
22
SUPPORTED_VERSIONS = 1.9 1.10 1.11 latest
33

44

5+
include env/.env.example
56
include env/makefiles/env.mk
67
include env/makefiles/docker.mk
78
include env/makefiles/local.mk

cmd/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var (
2424
"idle_timeout": time.Duration(0),
2525
"base_url": "http://localhost/",
2626
"template_dir": "static/templates",
27-
"forma_token": "",
2827
"dsn": "postgres://postgres:postgres@127.0.0.1:5432/postgres?connect_timeout=1&sslmode=disable",
2928
"open_conn": 1,
3029
"idle_conn": 1,

cmd/ctl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package cmd
1+
package cmd_test

cmd/grpc_helper.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,30 @@ import (
2222
"gopkg.in/yaml.v2"
2323
)
2424

25+
const (
26+
schemaKind kind = "Schema"
27+
templateKind kind = "Template"
28+
)
29+
2530
var entities factory
2631

2732
func init() {
2833
entities = factory{
2934
createCmd: {
30-
"Schema": func() interface{} { return &pb.CreateSchemaRequest{} },
31-
"Template": func() interface{} { return &pb.CreateTemplateRequest{} },
35+
schemaKind: func() interface{} { return &pb.CreateSchemaRequest{} },
36+
templateKind: func() interface{} { return &pb.CreateTemplateRequest{} },
3237
},
3338
readCmd: {
34-
"Schema": func() interface{} { return &pb.ReadSchemaRequest{} },
35-
"Template": func() interface{} { return &pb.ReadTemplateRequest{} },
39+
schemaKind: func() interface{} { return &pb.ReadSchemaRequest{} },
40+
templateKind: func() interface{} { return &pb.ReadTemplateRequest{} },
3641
},
3742
updateCmd: {
38-
"Schema": func() interface{} { return &pb.UpdateSchemaRequest{} },
39-
"Template": func() interface{} { return &pb.UpdateTemplateRequest{} },
43+
schemaKind: func() interface{} { return &pb.UpdateSchemaRequest{} },
44+
templateKind: func() interface{} { return &pb.UpdateTemplateRequest{} },
4045
},
4146
deleteCmd: {
42-
"Schema": func() interface{} { return &pb.DeleteSchemaRequest{} },
43-
"Template": func() interface{} { return &pb.DeleteTemplateRequest{} },
47+
schemaKind: func() interface{} { return &pb.DeleteSchemaRequest{} },
48+
templateKind: func() interface{} { return &pb.DeleteTemplateRequest{} },
4449
},
4550
}
4651
}
@@ -52,11 +57,15 @@ func communicate(cmd *cobra.Command, _ []string) error {
5257
}
5358
if dry, _ := cmd.Flags().GetBool("dry-run"); dry {
5459
cmd.Printf("%T would be sent with data: ", entity)
60+
if cmd.Flag("output").Value.String() == jsonFormat {
61+
return json.NewEncoder(cmd.OutOrStdout()).Encode(entity)
62+
}
5563
return json.NewEncoder(cmd.OutOrStdout()).Encode(entity)
5664
}
5765
response, err := call(cnf.Union.GRPCConfig, entity)
5866
if err != nil {
59-
return err
67+
cmd.Println(err)
68+
return nil
6069
}
6170
if cmd.Flag("output").Value.String() == jsonFormat {
6271
return json.NewEncoder(cmd.OutOrStdout()).Encode(response)
@@ -67,7 +76,7 @@ func communicate(cmd *cobra.Command, _ []string) error {
6776
func printSchemas(cmd *cobra.Command, _ []string) error {
6877
var (
6978
target *cobra.Command
70-
builders map[string]func() interface{}
79+
builders map[kind]builder
7180
found bool
7281
)
7382
use := cmd.Flag("for").Value.String()
@@ -80,30 +89,34 @@ func printSchemas(cmd *cobra.Command, _ []string) error {
8089
if !found {
8190
return errors.Errorf("unknown control command %q", use)
8291
}
83-
for kind, builder := range builders {
84-
yaml.NewEncoder(cmd.OutOrStdout()).Encode(schema{Kind: kind, Payload: convert(builder())})
92+
for k, b := range builders {
93+
_ = yaml.NewEncoder(cmd.OutOrStdout()).Encode(schema{Kind: k, Payload: convert(b())})
8594
cmd.Println()
8695
}
8796
return nil
8897
}
8998

99+
type builder func() interface{}
100+
101+
type kind string
102+
90103
type schema struct {
91-
Kind string `yaml:"kind"`
104+
Kind kind `yaml:"kind"`
92105
Payload map[string]interface{} `yaml:"payload"`
93106
}
94107

95-
type factory map[*cobra.Command]map[string]func() interface{}
108+
type factory map[*cobra.Command]map[kind]builder
96109

97110
func (f factory) new(cmd *cobra.Command) (interface{}, error) {
98111
data, err := f.data(cmd.Flag("filename").Value.String())
99112
if err != nil {
100113
return nil, err
101114
}
102-
builder, ok := f[cmd][data.Kind]
103-
if !ok {
115+
build, found := f[cmd][data.Kind]
116+
if !found {
104117
return nil, errors.Errorf("unknown payload type %q", data.Kind)
105118
}
106-
entity := builder()
119+
entity := build()
107120
if err = mapstructure.Decode(data.Payload, &entity); err != nil {
108121
return nil, errors.Wrapf(err, "trying to decode payload to %#v", entity)
109122
}

cmd/migrate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package cmd
1+
package cmd_test

cmd/root_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package cmd
1+
package cmd_test

cmd/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package cmd
1+
package cmd_test

0 commit comments

Comments
 (0)