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

Commit

Permalink
little cmd/grpc_helper improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Oct 2, 2018
1 parent 2b406c4 commit 7d36af0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ OPEN_BROWSER =
SUPPORTED_VERSIONS = 1.9 1.10 1.11 latest


include env/.env.example
include env/makefiles/env.mk
include env/makefiles/docker.mk
include env/makefiles/local.mk
Expand Down
1 change: 0 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
"idle_timeout": time.Duration(0),
"base_url": "http://localhost/",
"template_dir": "static/templates",
"forma_token": "",
"dsn": "postgres://postgres:postgres@127.0.0.1:5432/postgres?connect_timeout=1&sslmode=disable",
"open_conn": 1,
"idle_conn": 1,
Expand Down
2 changes: 1 addition & 1 deletion cmd/ctl_test.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package cmd
package cmd_test
47 changes: 30 additions & 17 deletions cmd/grpc_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@ import (
"gopkg.in/yaml.v2"
)

const (
schemaKind kind = "Schema"
templateKind kind = "Template"
)

var entities factory

func init() {
entities = factory{
createCmd: {
"Schema": func() interface{} { return &pb.CreateSchemaRequest{} },
"Template": func() interface{} { return &pb.CreateTemplateRequest{} },
schemaKind: func() interface{} { return &pb.CreateSchemaRequest{} },
templateKind: func() interface{} { return &pb.CreateTemplateRequest{} },
},
readCmd: {
"Schema": func() interface{} { return &pb.ReadSchemaRequest{} },
"Template": func() interface{} { return &pb.ReadTemplateRequest{} },
schemaKind: func() interface{} { return &pb.ReadSchemaRequest{} },
templateKind: func() interface{} { return &pb.ReadTemplateRequest{} },
},
updateCmd: {
"Schema": func() interface{} { return &pb.UpdateSchemaRequest{} },
"Template": func() interface{} { return &pb.UpdateTemplateRequest{} },
schemaKind: func() interface{} { return &pb.UpdateSchemaRequest{} },
templateKind: func() interface{} { return &pb.UpdateTemplateRequest{} },
},
deleteCmd: {
"Schema": func() interface{} { return &pb.DeleteSchemaRequest{} },
"Template": func() interface{} { return &pb.DeleteTemplateRequest{} },
schemaKind: func() interface{} { return &pb.DeleteSchemaRequest{} },
templateKind: func() interface{} { return &pb.DeleteTemplateRequest{} },
},
}
}
Expand All @@ -52,11 +57,15 @@ func communicate(cmd *cobra.Command, _ []string) error {
}
if dry, _ := cmd.Flags().GetBool("dry-run"); dry {
cmd.Printf("%T would be sent with data: ", entity)
if cmd.Flag("output").Value.String() == jsonFormat {
return json.NewEncoder(cmd.OutOrStdout()).Encode(entity)
}
return json.NewEncoder(cmd.OutOrStdout()).Encode(entity)
}
response, err := call(cnf.Union.GRPCConfig, entity)
if err != nil {
return err
cmd.Println(err)
return nil
}
if cmd.Flag("output").Value.String() == jsonFormat {
return json.NewEncoder(cmd.OutOrStdout()).Encode(response)
Expand All @@ -67,7 +76,7 @@ func communicate(cmd *cobra.Command, _ []string) error {
func printSchemas(cmd *cobra.Command, _ []string) error {
var (
target *cobra.Command
builders map[string]func() interface{}
builders map[kind]builder
found bool
)
use := cmd.Flag("for").Value.String()
Expand All @@ -80,30 +89,34 @@ func printSchemas(cmd *cobra.Command, _ []string) error {
if !found {
return errors.Errorf("unknown control command %q", use)
}
for kind, builder := range builders {
yaml.NewEncoder(cmd.OutOrStdout()).Encode(schema{Kind: kind, Payload: convert(builder())})
for k, b := range builders {
_ = yaml.NewEncoder(cmd.OutOrStdout()).Encode(schema{Kind: k, Payload: convert(b())})
cmd.Println()
}
return nil
}

type builder func() interface{}

type kind string

type schema struct {
Kind string `yaml:"kind"`
Kind kind `yaml:"kind"`
Payload map[string]interface{} `yaml:"payload"`
}

type factory map[*cobra.Command]map[string]func() interface{}
type factory map[*cobra.Command]map[kind]builder

func (f factory) new(cmd *cobra.Command) (interface{}, error) {
data, err := f.data(cmd.Flag("filename").Value.String())
if err != nil {
return nil, err
}
builder, ok := f[cmd][data.Kind]
if !ok {
build, found := f[cmd][data.Kind]
if !found {
return nil, errors.Errorf("unknown payload type %q", data.Kind)
}
entity := builder()
entity := build()
if err = mapstructure.Decode(data.Payload, &entity); err != nil {
return nil, errors.Wrapf(err, "trying to decode payload to %#v", entity)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package cmd
package cmd_test
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package cmd
package cmd_test
2 changes: 1 addition & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package cmd
package cmd_test

0 comments on commit 7d36af0

Please sign in to comment.