/
add.go
72 lines (57 loc) · 1.6 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package command
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/status"
todov1 "github.com/sagikazarmark/todobackend-go-kit/api/todo/v1"
)
type createOptions struct {
title string
client todov1.TodoListServiceClient
}
// NewAddCommand creates a new cobra.Command for adding a new item to the list.
func NewAddCommand(c Context) *cobra.Command {
options := createOptions{}
cmd := &cobra.Command{
Use: "add",
Aliases: []string{"a"},
Short: "Add an item to the list",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
options.title = args[0]
options.client = c.GetTodoClient()
cmd.SilenceErrors = true
cmd.SilenceUsage = true
return runCreate(options)
},
}
return cmd
}
func runCreate(options createOptions) error {
req := &todov1.AddItemRequest{
Title: options.title,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := options.client.AddItem(ctx, req)
if err != nil {
st := status.Convert(err)
for _, detail := range st.Details() {
// nolint: gocritic
switch t := detail.(type) {
case *errdetails.BadRequest:
fmt.Println("Oops! Your request was rejected by the server.")
for _, violation := range t.GetFieldViolations() {
fmt.Printf("The %q field was wrong:\n", violation.GetField())
fmt.Printf("\t%s\n", violation.GetDescription())
}
}
}
return err
}
fmt.Printf("Todo item %q with ID %s has been created.", options.title, resp.GetItem().GetId())
return nil
}