From 5529afbd17439aca95b67d528d54540d10603f9a Mon Sep 17 00:00:00 2001 From: Mark van Holsteijn Date: Thu, 3 Aug 2023 11:48:01 +0200 Subject: [PATCH] chore: formatted using gofumpt --- clone.go | 18 ++++++++---------- clone_test.go | 8 ++++---- committer.go | 8 ++++---- main.go | 16 ++++++++-------- ref/list_test.go | 6 ++++-- ref/ref.go | 14 ++++++++------ ref/resolve_test.go | 4 ++-- ref/update_test.go | 6 ++++-- serve.go | 6 +++--- tag/tag.go | 17 ++++++++++------- tag/tag_test.go | 5 +++-- walk.go | 10 +++++----- 12 files changed, 63 insertions(+), 55 deletions(-) diff --git a/clone.go b/clone.go index c79947c..4dbf56e 100644 --- a/clone.go +++ b/clone.go @@ -4,6 +4,13 @@ import ( "bytes" "context" "fmt" + "io" + "log" + neturl "net/url" + "os" + "os/exec" + "strings" + "github.com/binxio/gcloudconfig" sshconfig "github.com/kevinburke/ssh_config" "github.com/mitchellh/go-homedir" @@ -16,12 +23,6 @@ import ( githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh" "gopkg.in/src-d/go-git.v4/storage/memory" - "io" - "log" - neturl "net/url" - "os" - "os/exec" - "strings" ) func getCredentialHelper(url *neturl.URL) string { @@ -50,7 +51,6 @@ func getCredentialHelper(url *neturl.URL) string { } func getPassword(repositoryUrl *neturl.URL) (transport.AuthMethod, error) { - if os.Getenv("GIT_ASKPASS") == "" && getCredentialHelper(repositoryUrl) == "" { // No credential helper specified, not passing in credentials return nil, nil @@ -122,8 +122,7 @@ func getGoogleCredentials(url *neturl.URL) (transport.AuthMethod, error) { } func identityFileAuthentication(user string, host string) (auth transport.AuthMethod, err error) { - - var keyFile = sshconfig.Get(host, "IdentityFile") + keyFile := sshconfig.Get(host, "IdentityFile") if keyFile, err = homedir.Expand(keyFile); err != nil { return nil, fmt.Errorf("ERROR: failed to expand home directory of IdentityFile %s, %s", sshconfig.Get(host, "IdentityFile"), err) @@ -192,7 +191,6 @@ func sshAgentAuthentication(user, host, keyFile string, key ssh.PublicKey) (auth } func GetAuth(url string) (auth transport.AuthMethod, plainOpen bool, err error) { - if MatchesScheme(url) { repositoryUrl, err := neturl.Parse(url) if err != nil { diff --git a/clone_test.go b/clone_test.go index 635dd3d..85f1d17 100644 --- a/clone_test.go +++ b/clone_test.go @@ -2,6 +2,10 @@ package main import ( "fmt" + "io/ioutil" + "os" + "testing" + "golang.org/x/crypto/ssh" "gopkg.in/src-d/go-billy.v4/memfs" "gopkg.in/src-d/go-git.v4" @@ -9,13 +13,9 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing" go_git_ssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh" "gopkg.in/src-d/go-git.v4/storage/memory" - "io/ioutil" - "os" - "testing" ) func TestClone(t *testing.T) { - s := fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME")) sshKey, err := ioutil.ReadFile(s) signer, err := ssh.ParsePrivateKey([]byte(sshKey)) diff --git a/committer.go b/committer.go index 00689ce..3a68c6e 100644 --- a/committer.go +++ b/committer.go @@ -3,13 +3,14 @@ package main import ( "bytes" "fmt" - "gopkg.in/src-d/go-git.v4" - "gopkg.in/src-d/go-git.v4/plumbing" - "gopkg.in/src-d/go-git.v4/plumbing/object" "io" "log" "os" "time" + + "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/object" ) func (c *Cru) Commit() (hash plumbing.Hash, err error) { @@ -52,7 +53,6 @@ func (c *Cru) Commit() (hash plumbing.Hash, err error) { } func (c *Cru) Push() error { - if !c.DryRun { var progress io.Writer = os.Stderr if !c.Verbose { diff --git a/main.go b/main.go index 487bd3b..5244bb3 100644 --- a/main.go +++ b/main.go @@ -3,17 +3,18 @@ package main import ( "bytes" "fmt" + "io" + "io/ioutil" + "log" + "os" + "path/filepath" + "github.com/binxio/cru/ref" "github.com/docopt/docopt-go" "gopkg.in/src-d/go-billy.v4" "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" - "io" - "io/ioutil" - "log" - "os" - "path/filepath" ) type Cru struct { @@ -88,7 +89,6 @@ func (c *Cru) WriteFile(filename string, content []byte, perm os.FileMode) error } func List(c *Cru, filename string) error { - content, err := c.ReadFile(filename) if err != nil { return fmt.Errorf("could not read %s, %s", filename, err) @@ -114,7 +114,7 @@ func Update(c *Cru, filename string) error { content, updated := ref.UpdateReferences(content, c.imageRefs, c.RelPath(filename), c.MatchingTag, c.Verbose) if updated { if !c.DryRun { - err := c.WriteFile(filename, content, 0644) + err := c.WriteFile(filename, content, 0o644) if err != nil { return fmt.Errorf("failed to overwrite %s with updated references, %s", c.RelPath(filename), err) } @@ -249,7 +249,7 @@ Options: log.Fatalf("%s\n", err) } if !cru.MatchingTag { - for i, _ := range cru.imageRefs { + for i := range cru.imageRefs { cru.imageRefs[i].SetTag("latest") } } diff --git a/ref/list_test.go b/ref/list_test.go index 7544613..87fbbea 100644 --- a/ref/list_test.go +++ b/ref/list_test.go @@ -20,8 +20,10 @@ func TestListReferenceMultiple(t *testing.T) { and this is another mvanholsteijn/paas-monitor:3.1.0 and this is just a directory Name mvanholsteijn/paas-monitor, which should not be changed`) - expect := []ContainerImageReference{*MustNewContainerImageReference(`eu.gcr.io/binxio/paas-monitor:v0.3.1`), - *MustNewContainerImageReference(`mvanholsteijn/paas-monitor:3.1.0`)} + expect := []ContainerImageReference{ + *MustNewContainerImageReference(`eu.gcr.io/binxio/paas-monitor:v0.3.1`), + *MustNewContainerImageReference(`mvanholsteijn/paas-monitor:3.1.0`), + } result := FindAllContainerImageReference(input) for i, ref := range result { diff --git a/ref/ref.go b/ref/ref.go index 87c970c..a2d6c00 100644 --- a/ref/ref.go +++ b/ref/ref.go @@ -3,15 +3,16 @@ package ref import ( "bytes" "fmt" + "log" + "regexp" + "sort" + "strings" + "github.com/docker/distribution/reference" "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/crane" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/remote" - "log" - "regexp" - "sort" - "strings" ) var ( @@ -55,6 +56,7 @@ func NewContainerImageReference(imageReference string) (*ContainerImageReference return &result, nil } + func MustNewContainerImageReference(imageReference string) *ContainerImageReference { r, err := NewContainerImageReference(imageReference) if err != nil { @@ -78,7 +80,7 @@ func (r ContainerImageReference) String() string { } func FindAllContainerImageReference(content []byte) []ContainerImageReference { - var result = make(ContainerImageReferences, 0) + result := make(ContainerImageReferences, 0) allMatches := imageReferencePattern.FindAllIndex(content, -1) for _, match := range allMatches { s := string(content[match[0]:match[1]]) @@ -149,7 +151,7 @@ func UpdateReference(content []byte, reference ContainerImageReference, filename } } if previous < len(content) { - result.Write(content[previous:len(content)]) + result.Write(content[previous:]) } return result.Bytes(), updated diff --git a/ref/resolve_test.go b/ref/resolve_test.go index ce7c333..58b591b 100644 --- a/ref/resolve_test.go +++ b/ref/resolve_test.go @@ -1,10 +1,11 @@ package ref import ( + "testing" + "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/remote" - "testing" ) func TestImageResolve(t *testing.T) { @@ -35,7 +36,6 @@ func TestImageResolve(t *testing.T) { if digest.String() == rr.Digest { t.Fatalf("expected different Digest than %s, got %s", digest, rr.Digest) } - } func TestImageResolves(t *testing.T) { diff --git a/ref/update_test.go b/ref/update_test.go index 09e9bfb..68cf5e1 100644 --- a/ref/update_test.go +++ b/ref/update_test.go @@ -53,8 +53,10 @@ not be changed. And how about a digest like gcr.io/binx-io-public/paas-monitor:v1.0.0? does that work? `) - references := []ContainerImageReference{*MustNewContainerImageReference(`gcr.io/binx-io-public/paas-monitor:v1.0.0`), - *MustNewContainerImageReference(`mvanholsteijn/paas-monitor:3.1.0`)} + references := []ContainerImageReference{ + *MustNewContainerImageReference(`gcr.io/binx-io-public/paas-monitor:v1.0.0`), + *MustNewContainerImageReference(`mvanholsteijn/paas-monitor:3.1.0`), + } result, updated := UpdateReferences(input, references, "myfile.txt", false, true) if !updated { diff --git a/serve.go b/serve.go index 3bb0987..b9294f2 100644 --- a/serve.go +++ b/serve.go @@ -2,11 +2,12 @@ package main import ( "encoding/json" - "github.com/binxio/cru/ref" - "gopkg.in/src-d/go-git.v4/plumbing" "log" "net/http" "os" + + "github.com/binxio/cru/ref" + "gopkg.in/src-d/go-git.v4/plumbing" ) type ContainerReferenceUpdateRequest struct { @@ -22,7 +23,6 @@ type ContainerReferenceUpdateResponse struct { } func (c Cru) ServeHTTP(w http.ResponseWriter, r *http.Request) { - var hash plumbing.Hash var request ContainerReferenceUpdateRequest var response ContainerReferenceUpdateResponse diff --git a/tag/tag.go b/tag/tag.go index c5c5e3c..ea39914 100644 --- a/tag/tag.go +++ b/tag/tag.go @@ -2,11 +2,12 @@ package tag import ( "fmt" - "github.com/google/go-containerregistry/pkg/crane" - "github.com/google/go-containerregistry/pkg/name" "regexp" "sort" "strconv" + + "github.com/google/go-containerregistry/pkg/crane" + "github.com/google/go-containerregistry/pkg/name" ) type Tag struct { @@ -25,7 +26,7 @@ var ( ) func MakeTag(tag string) Tag { - var result = Tag{ + result := Tag{ Literal: tag, Version: make([]int, 0, 3), } @@ -66,6 +67,7 @@ func compareInt(a int, b int) int { } return 0 } + func compareVersion(a []int, b []int) int { var result int for i, v := range a { @@ -97,7 +99,6 @@ func (a Tag) Equals(b Tag) bool { } func ListAllTags(reference name.Reference) ([]Tag, error) { - tags, err := crane.ListTags(reference.Context().String()) if err != nil { return nil, fmt.Errorf("could not retrieve tags for %s", reference) @@ -111,8 +112,10 @@ func ListAllTags(reference name.Reference) ([]Tag, error) { return result, nil } -type TagList []Tag -type TagCategories map[string]TagList +type ( + TagList []Tag + TagCategories map[string]TagList +) func (l TagList) FindGreaterThan(tag Tag) TagList { result := make(TagList, 0) @@ -129,7 +132,7 @@ func (a TagList) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a TagList) Less(i, j int) bool { return (a[i]).Compare(a[j]) < 0 } func MakeTagCategories(tags TagList) TagCategories { - var result = make(map[string]TagList) + result := make(map[string]TagList) for _, tag := range tags { if list, ok := result[tag.Category]; ok { result[tag.Category] = append(list, tag) diff --git a/tag/tag_test.go b/tag/tag_test.go index f89ea55..a68eb91 100644 --- a/tag/tag_test.go +++ b/tag/tag_test.go @@ -2,12 +2,13 @@ package tag import ( "fmt" - "github.com/google/go-containerregistry/pkg/name" "testing" + + "github.com/google/go-containerregistry/pkg/name" ) func TestMakeTag(t *testing.T) { - var outputs = []Tag{ + outputs := []Tag{ {Literal: "1.15", Version: []int{1, 15}}, {Literal: "1.15.2", Version: []int{1, 15, 2}}, {Literal: "v1.15.2", Version: []int{1, 15, 2}, Prefix: "v", Category: "v|"}, diff --git a/walk.go b/walk.go index d979bcf..7def08b 100644 --- a/walk.go +++ b/walk.go @@ -2,14 +2,15 @@ package main import ( "fmt" - "golang.org/x/tools/godoc/util" - "golang.org/x/tools/godoc/vfs" - "gopkg.in/src-d/go-billy.v4" - "gopkg.in/src-d/go-git.v4/plumbing/format/gitignore" "os" "path" "path/filepath" "strings" + + "golang.org/x/tools/godoc/util" + "golang.org/x/tools/godoc/vfs" + "gopkg.in/src-d/go-billy.v4" + "gopkg.in/src-d/go-git.v4/plumbing/format/gitignore" ) type Visitor func(cru *Cru, path string) error @@ -36,7 +37,6 @@ func (this BillyVFS) Open(name string) (vfs.ReadSeekCloser, error) { } func (c *Cru) walkFn(visitor Visitor, filename string, info os.FileInfo) error { - if info.IsDir() { var patterns GitIgnorePatterns patterns, err := gitignore.ReadPatterns(*c.filesystem, strings.Split(filename, "/"))