Skip to content

Commit c838b0b

Browse files
committed
Code cleaning
Apply some of the advice of 'revive'.
1 parent 4c166f1 commit c838b0b

6 files changed

Lines changed: 91 additions & 46 deletions

File tree

pkg/vendir/cmd/baseline.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 The Carvel Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package cmd
25

36
import (
@@ -14,7 +17,7 @@ import (
1417
ctlcache "carvel.dev/vendir/pkg/vendir/fetch/cache"
1518
)
1619

17-
func NewBaselineOptions(ui ui.UI) *BaselineOptions {
20+
func NewBaselineOptions(ui ui.UI) *BaselineOptions { //nolint:revive
1821
return &BaselineOptions{ui: ui}
1922
}
2023

@@ -29,10 +32,15 @@ func NewBaselineCmd(o *BaselineOptions) *cobra.Command {
2932
&o.Yes, "yes", "y", false,
3033
"If true, automatically answer 'yes' to all the questions")
3134

32-
cmd.Flags().StringSliceVarP(&o.Files, "file", "f", []string{defaultConfigName}, "Set configuration file")
33-
cmd.Flags().StringVar(&o.LockFile, "lock-file", defaultLockName, "Set lock file")
34-
cmd.Flags().StringVar(&o.Chdir, "chdir", "", "Set current directory for process")
35-
cmd.Flags().BoolVar(&o.PreferSHA, "prefer-sha", false, "Prefer sha instead of tags")
35+
cmd.Flags().StringSliceVarP(
36+
&o.Files, "file", "f", []string{defaultConfigName},
37+
"Set configuration file")
38+
cmd.Flags().StringVar(
39+
&o.LockFile, "lock-file", defaultLockName, "Set lock file")
40+
cmd.Flags().StringVar(
41+
&o.Chdir, "chdir", "", "Set current directory for process")
42+
cmd.Flags().BoolVar(
43+
&o.PreferSHA, "prefer-sha", false, "Prefer sha instead of tags")
3644
cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "List what would be done")
3745

3846
return &cmd
@@ -53,7 +61,7 @@ type BaselineOptions struct {
5361
}
5462

5563
func (o *BaselineOptions) Run() error {
56-
if len(o.Chdir) > 0 {
64+
if len(o.Chdir) > 0 { //nolint:revive
5765
err := os.Chdir(o.Chdir)
5866
if err != nil {
5967
return fmt.Errorf("Running chdir: %s", err)
@@ -91,19 +99,23 @@ func (o *BaselineOptions) Run() error {
9199
newRefs := make(map[string]string)
92100

93101
for _, status := range statusMap {
94-
if !status.MatchTarget() || !o.PreferSHA && !status.MatchTargetTag() && len(status.Ref.Tags) != 0 {
102+
if !status.MatchTarget() ||
103+
!o.PreferSHA &&
104+
!status.MatchTargetTag() &&
105+
len(status.Ref.Tags) != 0 { //nolint:revive
95106
var newRef string
96-
if o.PreferSHA || len(status.Ref.Tags) == 0 {
107+
if o.PreferSHA || len(status.Ref.Tags) == 0 { //nolint:revive
97108
newRef = status.Ref.SHA
98109
} else {
99-
newRef = status.Ref.Tags[0]
110+
newRef = status.Ref.Tags[0] //nolint:revive
100111
}
101112
newRefs[status.Path()] = newRef
102113
}
103114
}
104115

105-
if len(newRefs) == 0 {
106-
o.ui.PrintLinef("All references already match current state, no update needed")
116+
if len(newRefs) == 0 { //nolint:revive
117+
o.ui.PrintLinef(
118+
"All references already match current state, no update needed")
107119

108120
return nil
109121
}
@@ -114,7 +126,9 @@ func (o *BaselineOptions) Run() error {
114126
if newRef != "" {
115127
newRef = " -> " + newRef
116128
}
117-
o.ui.PrintLinef("%s/%s: %s%s", status.DirectoryPath, status.ContentPath, status.TargetRef, newRef)
129+
o.ui.PrintLinef(
130+
"%s/%s: %s%s",
131+
status.DirectoryPath, status.ContentPath, status.TargetRef, newRef)
118132
}
119133

120134
if !o.DryRun {
@@ -152,7 +166,7 @@ func saveFile(fname string, doc *yaml.Node) error {
152166
}
153167

154168
enc := yaml.NewEncoder(f)
155-
enc.SetIndent(2)
169+
enc.SetIndent(2) //nolint:revive
156170
if err := enc.Encode(doc); err != nil {
157171
_ = f.Close()
158172

@@ -172,7 +186,7 @@ func updateRefs(fname string, newRefs map[string]string) error {
172186
panic("expects the root node")
173187
}
174188

175-
top := doc.Content[0]
189+
top := doc.Content[0] //nolint:revive
176190
if top.Kind != yaml.MappingNode {
177191
panic("top content must be a mapping")
178192
}
@@ -193,14 +207,16 @@ func updateRefs(fname string, newRefs map[string]string) error {
193207
if hg := getMappingNodeChild(content, "hg"); hg != nil {
194208
ref := getMappingNodeChild(hg, "ref")
195209
if ref == nil {
196-
return fmt.Errorf("could not find 'ref' for '%s'", fullPath)
210+
return fmt.Errorf(
211+
"could not find 'ref' for '%s'", fullPath)
197212
}
198213
ref.Value = newRef
199214
}
200215
if git := getMappingNodeChild(content, "git"); git != nil {
201216
ref := getMappingNodeChild(git, "ref")
202217
if ref == nil {
203-
return fmt.Errorf("could not find 'ref' for '%s'", fullPath)
218+
return fmt.Errorf(
219+
"could not find 'ref' for '%s'", fullPath)
204220
}
205221
ref.Value = newRef
206222
}
@@ -212,9 +228,9 @@ func updateRefs(fname string, newRefs map[string]string) error {
212228
}
213229

214230
func getMappingNodeChild(node *yaml.Node, name string) *yaml.Node {
215-
for i := 0; i < len(node.Content); i += 2 {
231+
for i := 0; i < len(node.Content); i += 2 { //nolint:revive
216232
if node.Content[i].Value == name {
217-
return node.Content[i+1]
233+
return node.Content[i+1] //nolint:revive
218234
}
219235
}
220236

pkg/vendir/cmd/status.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 The Carvel Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package cmd
25

36
import (
@@ -23,7 +26,7 @@ type StatusOptions struct {
2326
ExitCode bool
2427
}
2528

26-
func NewStatusOptions(ui ui.UI) *StatusOptions {
29+
func NewStatusOptions(ui ui.UI) *StatusOptions { //nolint:revive
2730
return &StatusOptions{ui: ui}
2831
}
2932

@@ -34,16 +37,23 @@ func NewStatusCmd(o *StatusOptions) *cobra.Command {
3437
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
3538
}
3639

37-
cmd.Flags().StringSliceVarP(&o.Files, "file", "f", []string{defaultConfigName}, "Set configuration file")
38-
cmd.Flags().StringVar(&o.LockFile, "lock-file", defaultLockName, "Set lock file")
39-
cmd.Flags().StringVar(&o.Chdir, "chdir", "", "Set current directory for process")
40-
cmd.Flags().BoolVar(&o.ExitCode, "exit-code", false, "Set to 'true', it exits with a non-0 code if any subproject is not clean")
40+
cmd.Flags().StringSliceVarP(
41+
&o.Files, "file", "f",
42+
[]string{defaultConfigName}, "Set configuration file")
43+
cmd.Flags().StringVar(
44+
&o.LockFile, "lock-file", defaultLockName, "Set lock file")
45+
cmd.Flags().StringVar(
46+
&o.Chdir, "chdir", "", "Set current directory for process")
47+
cmd.Flags().BoolVar(
48+
&o.ExitCode, "exit-code", false,
49+
"Set to 'true', it exits with a non-0 code if any "+
50+
"subproject is not clean")
4151

4252
return cmd
4353
}
4454

4555
func (o *StatusOptions) Run() error {
46-
if len(o.Chdir) > 0 {
56+
if len(o.Chdir) > 0 { //nolint:revive
4757
err := os.Chdir(o.Chdir)
4858
if err != nil {
4959
return fmt.Errorf("Running chdir: %s", err)
@@ -88,7 +98,7 @@ func fullStatus(
8898
conf ctlconf.Config,
8999
syncOpts ctldir.SyncOpts,
90100
existingLockConfig ctlconf.LockConfig,
91-
ui ui.UI,
101+
ui ui.UI, //nolint:revive
92102
) (ctlstatus.StatusList, error) {
93103
status := ctlstatus.StatusList{}
94104
for _, dirConf := range conf.Directories {
@@ -97,7 +107,8 @@ func fullStatus(
97107

98108
dirStatus, err := directory.Status(syncOpts)
99109
if err != nil {
100-
return nil, fmt.Errorf("Reading directory '%s': %s", dirConf.Path, err)
110+
return nil, fmt.Errorf(
111+
"Reading directory '%s': %s", dirConf.Path, err)
101112
}
102113

103114
status = append(status, dirStatus...)

pkg/vendir/cmd/sync.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func NewSyncCmd(o *SyncOptions) *cobra.Command {
5858
cmd.Flags().StringVar(&o.Chdir, "chdir", "", "Set current directory for process")
5959
cmd.Flags().BoolVar(&o.AllowAllSymlinkDestinations, "dangerous-allow-all-symlink-destinations", false, "Symlinks to all destinations are allowed")
6060

61-
cmd.Flags().BoolVar(&o.Safe, "safe", false, "sync only if local DVCS clones are clean")
61+
cmd.Flags().BoolVar(
62+
&o.Safe, "safe", false, "sync only if local DVCS clones are clean")
6263

6364
return cmd
6465
}

pkg/vendir/directory/status.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 The Carvel Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package directory
25

36
import (
@@ -12,12 +15,14 @@ func (d *Directory) Status(syncOpts SyncOpts) (ctlstatus.StatusList, error) {
1215
var res ctlstatus.StatusList
1316

1417
for _, contents := range d.opts.Contents {
15-
path := path.Join(d.opts.Path, contents.Path)
18+
contentPath := path.Join(d.opts.Path, contents.Path)
1619
switch {
1720
case contents.Git != nil:
18-
gitSync := ctlgit.NewSync(*contents.Git, NewInfoLog(d.ui), syncOpts.RefFetcher, syncOpts.Cache)
21+
gitSync := ctlgit.NewSync(
22+
*contents.Git, NewInfoLog(d.ui),
23+
syncOpts.RefFetcher, syncOpts.Cache)
1924

20-
gitStatus, err := gitSync.Status(path)
25+
gitStatus, err := gitSync.Status(contentPath)
2126
if err != nil {
2227
return nil, err
2328
}
@@ -29,9 +34,10 @@ func (d *Directory) Status(syncOpts SyncOpts) (ctlstatus.StatusList, error) {
2934
}
3035
case contents.Hg != nil:
3136
hgSync := ctlhg.NewSync(
32-
*contents.Hg, NewInfoLog(d.ui), syncOpts.RefFetcher, syncOpts.Cache)
37+
*contents.Hg, NewInfoLog(d.ui),
38+
syncOpts.RefFetcher, syncOpts.Cache)
3339

34-
hgStatus, err := hgSync.Status(path)
40+
hgStatus, err := hgSync.Status(contentPath)
3541
if err != nil {
3642
return nil, err
3743
}

pkg/vendir/fetch/git/status.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 The Carvel Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package git
25

36
import (
@@ -24,21 +27,24 @@ func (d Sync) Status(target string) (*ctlstatus.Status, error) {
2427
TargetRef: d.opts.Ref,
2528
}
2629

27-
out, _, err := git.cmdRunner.Run([]string{"rev-parse", "HEAD"}, []string{}, target)
30+
out, _, err := git.cmdRunner.Run(
31+
[]string{"rev-parse", "HEAD"}, []string{}, target)
2832
if err != nil {
2933
return nil, err
3034
}
3135
status.Ref.SHA = strings.TrimSpace(out)
3236

33-
out, _, err = git.cmdRunner.Run([]string{"tag", "--contains"}, []string{}, target)
37+
out, _, err = git.cmdRunner.Run(
38+
[]string{"tag", "--contains"}, []string{}, target)
3439
if err != nil {
3540
return nil, err
3641
}
3742
if out != "" {
3843
status.Ref.Tags = strings.Split(strings.TrimSpace(out), "\n")
3944
}
4045

41-
out, _, err = git.cmdRunner.Run([]string{"branch", "--contains"}, []string{}, target)
46+
out, _, err = git.cmdRunner.Run(
47+
[]string{"branch", "--contains"}, []string{}, target)
4248
if err != nil {
4349
return nil, err
4450
}

pkg/vendir/fetch/hg/status.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 The Carvel Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package hg
25

36
import (
@@ -12,6 +15,8 @@ import (
1215
ctlstatus "carvel.dev/vendir/pkg/vendir/status"
1316
)
1417

18+
const emptyString = ""
19+
1520
func (d Sync) Status(target string) (*ctlstatus.Status, error) {
1621
_, err := os.Stat(path.Join(target, ".hg"))
1722
if err != nil {
@@ -37,38 +42,38 @@ func (d Sync) Status(target string) (*ctlstatus.Status, error) {
3742
}
3843

3944
splitted := strings.Split(out, "\n")
40-
sha := splitted[0]
41-
tags := splitted[1]
42-
branch := splitted[2]
43-
topic := splitted[3]
44-
bookmarks := splitted[4]
45+
sha := splitted[0] //nolint:revive
46+
tags := splitted[1] //nolint:revive
47+
branch := splitted[2] //nolint:revive
48+
topic := splitted[3] //nolint:revive
49+
bookmarks := splitted[4] //nolint:revive
4550

4651
status := ctlstatus.Status{
4752
TargetRef: d.opts.Ref,
4853
Ref: ctlstatus.CompleteReference{
4954
SHA: sha,
5055
},
5156
}
52-
if tags != "" {
57+
if tags != emptyString {
5358
status.Ref.Tags = strings.Split(tags, " ")
5459
status.Ref.Tags = slices.DeleteFunc(
5560
status.Ref.Tags, func(t string) bool { return t == "tip" })
5661
}
57-
if branch != "" {
62+
if branch != emptyString {
5863
status.Ref.Others = append(status.Ref.Others, branch)
5964
}
60-
if topic != "" {
65+
if topic != emptyString {
6166
status.Ref.Others = append(status.Ref.Others, topic)
6267
}
63-
if bookmarks != "" {
68+
if bookmarks != emptyString {
6469
status.Ref.Others = append(status.Ref.Others, bookmarks)
6570
}
6671

6772
out, _, err = hg.run([]string{"status"}, target)
6873
if err != nil {
6974
return nil, err
7075
}
71-
if out != "" {
76+
if out != emptyString {
7277
status.UncommitedChanges = strings.Split(strings.TrimSpace(out), "\n")
7378
}
7479

@@ -80,7 +85,7 @@ func (d Sync) Status(target string) (*ctlstatus.Status, error) {
8085
}
8186
}
8287

83-
if out != "" {
88+
if out != emptyString {
8489
status.LocalCsets = strings.Split(strings.TrimSpace(out), "\n")
8590
}
8691

0 commit comments

Comments
 (0)