/
update.go
114 lines (105 loc) · 2.71 KB
/
update.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cmd
import (
"errors"
"fmt"
"os"
cs "github.com/gwleclerc/adr/constants"
"github.com/gwleclerc/adr/records"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var (
update_author string
update_status records.AdrStatus
update_tags []string
update_superseders []string
)
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update [flags] <record ID>",
Short: "Update an ADR",
Long: `
Update an existing architecture decision record.
It will keep the content and only modify the metadata.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) <= 0 {
fmt.Printf("%s %s %s\n", cs.Red("invalid argument: please specify a"), cs.RedUnderline("record ID"), cs.Red("in arguments"))
return ErrSilent
}
if len(args) > 1 {
fmt.Println(cs.Yellow("too many argument: keeping only the first record ID"))
}
recordID := args[0]
service, err := records.NewService()
if err != nil {
fmt.Println(cs.Red("unable to initialize records service: %v", err))
return ErrSilent
}
if err := updateRecord(service, recordID); err != nil {
fmt.Println(cs.Red("unable to update ADR %q: %v", recordID, err))
return ErrSilent
}
return nil
},
}
func init() {
updateCmd.Flags().StringVarP(
&update_author,
"author",
"a",
"",
"author of the record",
)
updateCmd.Flags().VarP(
&update_status,
"status",
"s",
`status of the record, allowed: "unknown", "proposed", "accepted", "deprecated", "superseded" or "observed"`,
)
_ = updateCmd.RegisterFlagCompletionFunc("status", records.AdrStatusCompletion)
updateCmd.Flags().StringSliceVarP(
&update_tags,
"tags",
"t",
nil, // must be nil to allow '--tags=' to remove all tags on record
`tags of the record`,
)
updateCmd.Flags().StringSliceVarP(
&update_superseders,
"superseders",
"r",
nil, // must be nil to allow '--superseders=' to remove all superseders on record
`superseders of the record`,
)
rootCmd.AddCommand(updateCmd)
}
func updateRecord(service *records.Service, recordID string) error {
record, ok := service.GetRecord(recordID)
if !ok {
return errors.New("record not found")
}
if update_author != "" {
record.Author = update_author
}
if update_status != "" {
record.Status = update_status
}
if update_tags != nil {
record.Tags.Set(update_tags...)
}
if update_superseders != nil {
record.Superseders.Set(update_superseders...)
}
err := service.UpdateRecord(record)
if err != nil {
return err
}
fmt.Println()
fmt.Println(cs.Green("Record %q has been successfully updated:", record.ID))
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(cs.TableHeader)
table.Append(record.ToRow())
table.Render()
fmt.Println()
return nil
}