forked from juju/juju
/
listcommits.go
210 lines (182 loc) · 5.6 KB
/
listcommits.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package model
import (
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
"github.com/gosuri/uitable"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/DavinZhang/juju/api/modelgeneration"
jujucmd "github.com/DavinZhang/juju/cmd"
"github.com/DavinZhang/juju/cmd/juju/common"
"github.com/DavinZhang/juju/cmd/modelcmd"
"github.com/DavinZhang/juju/core/model"
"github.com/DavinZhang/juju/juju/osenv"
)
const (
listCommitsSummary = "Lists commits history"
listCommitsDoc = `
Commits shows the timeline of changes to the model that occurred through branching.
It does not take into account other changes to the model that did not occur through a managed branch.
Lists consists of:
- the commit number
- user who committed the branch
- when the branch was committed
- the branch name
Examples:
juju commits
juju commits --utc
See also:
commits
show-commit
add-branch
track
branch
abort
diff
`
)
// CommitsCommand supplies the "commit" CLI command used to commit changes made
// under a branch, to the model.
type CommitsCommand struct {
modelcmd.ModelCommandBase
api CommitsCommandAPI
out cmd.Output
isoTime bool
}
// CommitsCommandAPI defines an API interface to be used during testing.
//go:generate go run github.com/golang/mock/mockgen -package mocks -destination ./mocks/commits_mock.go github.com/DavinZhang/juju/cmd/juju/model CommitsCommandAPI
type CommitsCommandAPI interface {
Close() error
// ListCommitsBranch commits the branch with the input name to the model,
// effectively completing it and applying
// all branch changes across the model.
// The new generation ID of the model is returned.
ListCommits() (model.GenerationCommits, error)
}
// NewCommitCommand wraps CommitsCommand with sane model settings.
func NewCommitsCommand() cmd.Command {
return modelcmd.Wrap(&CommitsCommand{})
}
// Info implements part of the cmd.Command interface.
func (c *CommitsCommand) Info() *cmd.Info {
info := &cmd.Info{
Name: "commits",
Purpose: listCommitsSummary,
Doc: listCommitsDoc,
Aliases: []string{"list-commits"},
}
return jujucmd.Info(info)
}
// SetFlags implements part of the cmd.Command interface.
func (c *CommitsCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
f.BoolVar(&c.isoTime, "utc", false, "Display time as UTC in RFC3339 format")
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": c.printTabular,
})
}
// Init implements part of the cmd.Command interface.
func (c *CommitsCommand) Init(args []string) error {
lArgs := len(args)
if lArgs > 0 {
return errors.Errorf("expected no arguments, but got %v", lArgs)
}
// If use of ISO time not specified on command line, check env var.
if !c.isoTime {
envVarValue := os.Getenv(osenv.JujuStatusIsoTimeEnvKey)
if envVarValue != "" {
var err error
if c.isoTime, err = strconv.ParseBool(envVarValue); err != nil {
return errors.Annotatef(err, "invalid %s env var, expected true|false", osenv.JujuStatusIsoTimeEnvKey)
}
}
}
return nil
}
// getAPI returns the API. This allows passing in a test CommitCommandAPI
// implementation.
func (c *CommitsCommand) getAPI() (CommitsCommandAPI, error) {
if c.api != nil {
return c.api, nil
}
api, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Annotate(err, "opening API connection")
}
client := modelgeneration.NewClient(api)
return client, nil
}
// Run implements the meaty part of the cmd.Command interface.
func (c *CommitsCommand) Run(ctx *cmd.Context) error {
client, err := c.getAPI()
if err != nil {
return err
}
defer func() { _ = client.Close() }()
commits, err := client.ListCommits()
if err != nil {
return errors.Trace(err)
}
if len(commits) == 0 && c.out.Name() == "tabular" {
ctx.Infof("No commits to list")
return nil
}
tabular := c.constructYamlJson(commits)
return errors.Trace(c.out.Write(ctx, tabular))
}
// printTabular prints the list of actions in tabular format
func (c *CommitsCommand) printTabular(writer io.Writer, value interface{}) error {
list, ok := value.(formattedCommitList)
if !ok {
return errors.New("unexpected value")
}
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
table.AddRow("Commit", "Committed at", "Committed by", "Branch name")
for _, c := range list.Commits {
table.AddRow(c.CommitId, c.CommittedAt, c.CommittedBy, c.BranchName)
}
_, _ = fmt.Fprint(writer, table)
return nil
}
func (c *CommitsCommand) constructYamlJson(generations model.GenerationCommits) formattedCommitList {
sort.Slice(generations, func(i, j int) bool {
return generations[i].GenerationId > generations[j].GenerationId
})
var result formattedCommitList
for _, gen := range generations {
var formattedDate string
if c.out.Name() == "tabular" {
formattedDate = common.UserFriendlyDuration(gen.Completed, time.Now())
} else {
formattedDate = common.FormatTime(&gen.Completed, c.isoTime)
}
fmc := formattedCommit{
CommitId: gen.GenerationId,
BranchName: gen.BranchName,
CommittedAt: formattedDate,
CommittedBy: gen.CompletedBy,
}
result.Commits = append(result.Commits, fmc)
}
return result
}
type formattedCommit struct {
CommitId int `json:"id" yaml:"id"`
BranchName string `json:"branch-name" yaml:"branch-name"`
CommittedAt string `json:"committed-at" yaml:"committed-at"`
CommittedBy string `json:"committed-by" yaml:"committed-by"`
}
type formattedCommitList struct {
Commits []formattedCommit `json:"commits" yaml:"commits"`
}