Skip to content

Commit

Permalink
chore: cleanup golangci-lint suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtajuckel committed Jul 8, 2024
1 parent d753d95 commit b3d0d5d
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 15 deletions.
5 changes: 2 additions & 3 deletions cmd/vela-manifest-tool/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (

const manifestToolBin = "/manifest-tool"

// private variables just for test mocking
// Private variables just for test mocking.
var stdout io.Writer = os.Stdout
var stderr io.Writer = os.Stderr

// execCmd is a helper function to
// run the provided command.
// execCmd is a helper function to run the provided command.
func execCmd(e *exec.Cmd) error {
logrus.Tracef("executing cmd %s", strings.Join(e.Args, " "))

Expand Down
13 changes: 11 additions & 2 deletions cmd/vela-manifest-tool/command_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

package main

import (
Expand All @@ -15,37 +17,44 @@ func TestVersion(t *testing.T) {
{cmd.Args[0], "manifest-tool"},
{cmd.Args[1], "--version"},
}

for _, tc := range cases {
if !strings.Contains(tc.arg, tc.expected) {
t.Errorf(`Expected %v to contain %q`, tc.arg, tc.expected)
}
}
}

// Feels like execCmd should be written/tested in shared lib
// Feels like execCmd should be written/tested in shared lib.
func TestExecution(t *testing.T) {
cases := []struct {
args []string
expout, experr string
}{
{[]string{"echo", "-n", "foo"}, "foo", ""},
}

oldStdout := stdout
defer func() { stdout = oldStdout }()

oldStderr := stderr
defer func() { stderr = oldStderr }()

for _, tc := range cases {
var outbuf, errbuf bytes.Buffer

stdout, stderr = &outbuf, &errbuf
cmd := exec.Command(tc.args[0], tc.args[1:]...)
cmd := exec.Command(tc.args[0], tc.args[1:]...) //nolint:gosec // we control the test data

err := execCmd(cmd)
if err != nil {
t.Errorf("Expected no error when creating command: %v", err)
}

if tc.expout != outbuf.String() {
t.Errorf("Expected %q to be equal to %q", outbuf.String(), tc.expout)
}

if tc.experr != errbuf.String() {
t.Errorf("Expected %q to be equal to %q", errbuf.String(), tc.experr)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/vela-manifest-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
_ "github.com/joho/godotenv/autoload"
)

//nolint:funlen // ignore function length due to comments and flags
func main() {
v := version.New()

Expand Down
3 changes: 3 additions & 0 deletions cmd/vela-manifest-tool/main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

package main

import (
Expand All @@ -15,6 +17,7 @@ func TestVersionCompatible(t *testing.T) {

func TestVersionSemver(t *testing.T) {
version.Tag = "abcd"

v := version.New()
if v != nil {
t.Errorf("version.New should return nil if a non-semver Tag (%q) is provided", version.Tag)
Expand Down
16 changes: 15 additions & 1 deletion cmd/vela-manifest-tool/manifestspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Manifest struct {
Template *template.Template
}

// ManifestSpec represents the structure of the manifest-tool yaml spec file
// ManifestSpec represents the structure of the manifest-tool yaml spec file.
type ManifestSpec struct {
Image string // name of the image index including tag
Manifests []ManifestComponent // list of component images to include in index
Expand Down Expand Up @@ -55,21 +55,25 @@ type ComponentContext struct {
func NewManifestSpec(reg *Registry, repo *Repo) ([]*ManifestSpec, error) {
specs := []*ManifestSpec{}
tmpl, err := template.New("component_template").Parse(repo.ComponentTemplate)

if err != nil {
return specs, err
}

if len(reg.Name) == 0 {
return specs, fmt.Errorf("no registry name provided")
}

if len(repo.Name) == 0 {
return specs, fmt.Errorf("no repository name provided")
}

for _, tag := range repo.Tags {
ms := ManifestSpec{
Image: reg.Name + repo.Name + ":" + tag,
Manifests: []ManifestComponent{},
}

for _, platform := range repo.Platforms {
platformComp := strings.Split(platform, "/")
if len(platformComp) < 2 {
Expand All @@ -79,18 +83,22 @@ func NewManifestSpec(reg *Registry, repo *Repo) ([]*ManifestSpec, error) {
// else to make the variant below clean
platformComp = append(platformComp, "")
}

ctx := ComponentContext{
Repo: repo.Name,
Tag: tag,
Os: platformComp[0],
Arch: platformComp[1],
Variant: platformComp[2],
}

var compImgBuf bytes.Buffer
err = tmpl.Execute(&compImgBuf, ctx)

if err != nil {
return specs, err
}

compImg := compImgBuf.String()
comp := ManifestComponent{
Image: fmt.Sprintf("%s%s", reg.Name, compImg),
Expand All @@ -102,8 +110,10 @@ func NewManifestSpec(reg *Registry, repo *Repo) ([]*ManifestSpec, error) {
}
ms.Manifests = append(ms.Manifests, comp)
}

specs = append(specs, &ms)
}

return specs, nil
}

Expand Down Expand Up @@ -141,7 +151,9 @@ func (ms *ManifestSpec) Render(wr io.Writer) error {
if err != nil {
return err
}

_, err = wr.Write(yamlData)

return err
}

Expand All @@ -151,8 +163,10 @@ func validateTagOfImage(fullImage string) error {
if len(topLevelImgParts) != 2 {
return fmt.Errorf("%s not in image:tag format", fullImage)
}

if !tagRegexp.MatchString(topLevelImgParts[1]) {
return fmt.Errorf(errTagValidation, topLevelImgParts[1])
}

return nil
}
10 changes: 9 additions & 1 deletion cmd/vela-manifest-tool/manifestspec_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

package main

import (
Expand All @@ -19,11 +21,14 @@ func TestManifestSpec_New_Validate(t *testing.T) {
man.Manifests[0].Image)
assertImageMatch(t, "index.docker.io/octocat/hello-world:latest-linux-arm64-v8",
man.Manifests[1].Image)

var data bytes.Buffer

err := man.Render(&data)
if err != nil {
t.Errorf("Error encountered during render: %v", err)
}

expected := "image: index.docker.io/octocat/hello-world:latest\n" +
"manifests:\n" +
"- image: index.docker.io/octocat/hello-world:latest-linux-amd64\n" +
Expand Down Expand Up @@ -143,6 +148,7 @@ func firstMS(ms []*ManifestSpec, _ error) *ManifestSpec {
if len(ms) > 0 {
return ms[0]
}

return nil
}

Expand Down Expand Up @@ -170,13 +176,15 @@ func defaultFixture(t *testing.T) *ManifestSpec {
if err != nil {
t.Fatalf("error encountered: %v", err)
}

if len(ms) != 1 {
t.Fatalf("should only have returned a single manifest spec")
}

return ms[0]
}

// Translate ManifestSpec
// Translate ManifestSpec.
func trMS(t *testing.T, f func(*ManifestSpec) *ManifestSpec) *ManifestSpec {
return f(defaultFixture(t))
}
Expand Down
19 changes: 14 additions & 5 deletions cmd/vela-manifest-tool/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ type Plugin struct {
manifestSpecs []*ManifestSpec // Parsed specs, populated as side effect of validate
}

// Command formats and outputs the command necessary for
// manifest-tool to build and publish a Docker Manifest List or
// OCI Image Index
// Formats and outputs the command necessary for manifest-tool to build
// and publish a Docker Manifest List or OCI Image Index.
func (p *Plugin) Command(specFile string) *exec.Cmd {
logrus.Debug("creating manifest-tool command from plugin configuration")

Expand Down Expand Up @@ -75,25 +74,34 @@ func (p *Plugin) Exec() error {
if err != nil {
return err
}

a := &afero.Afero{
Fs: appFS,
}

err = a.Mkdir("/root/specs", 0755)
if err != nil {
return err
}

for i, spec := range manifestSpecs {
fmt.Printf("Processing manifest list/image index %s\n", spec.Image)
var data bytes.Buffer

fmt.Printf("Processing manifest list/image index %s\n", spec.Image)

err = spec.Render(&data)
if err != nil {
return err
}

fmt.Printf("Rendered spec file:\n%s\n", data.String())
specFilename := fmt.Sprintf("/root/specs/spec_%d.yml", i)
a.WriteFile(specFilename, data.Bytes(), 0644)

err = a.WriteFile(specFilename, data.Bytes(), 0644)
if err != nil {
return err
}

cmd := p.Command(specFilename)
// If a dry run, return without executing the cmd
if p.Registry.DryRun {
Expand Down Expand Up @@ -139,6 +147,7 @@ func (p *Plugin) Validate() error {
if err != nil {
return err
}

p.manifestSpecs = manifestSpecs

return nil
Expand Down
4 changes: 3 additions & 1 deletion cmd/vela-manifest-tool/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

package main

import (
Expand Down Expand Up @@ -83,7 +85,7 @@ func makeDefaultPlugin() *Plugin {
}
}

// Translate Plugin
// Translate Plugin.
func trP(t func(*Plugin) *Plugin) *Plugin {
return t(makeDefaultPlugin())
}
2 changes: 1 addition & 1 deletion cmd/vela-manifest-tool/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type (
// Repo represents the plugin configuration for repo information
// Repo represents the plugin configuration for repo information.
Repo struct {
Name string // name of the repository for the image
Tags []string // tags of the image for the repository
Expand Down
2 changes: 2 additions & 0 deletions cmd/vela-manifest-tool/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestDocker_Repo_Validate_NoPlatforms(t *testing.T) {
Name: "/target/vela-manifest-tool",
Tags: []string{"latest"},
}

err := r.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
Expand All @@ -77,6 +78,7 @@ func TestDocker_Repo_InvalidPlatform(t *testing.T) {
Tags: []string{"latest"},
Platforms: []string{"windows/riscv64"},
}

err := r.Validate()
if err == nil {
t.Errorf("Validate should have returned an err")
Expand Down

0 comments on commit b3d0d5d

Please sign in to comment.