Skip to content

Commit 76a3883

Browse files
authored
chore: add linter (#69)
1 parent 28044f0 commit 76a3883

33 files changed

+1532
-387
lines changed

.github/workflows/lint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
with:
1313
go-version-file: 'go.mod'
1414
- name: Lint
15-
uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0
15+
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
1616
with:
1717
version: latest
1818
args: --timeout 10m

.golangci.yml

Lines changed: 828 additions & 0 deletions
Large diffs are not rendered by default.

builder/upcloud/artifact.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package upcloud
33
import (
44
"fmt"
55
"log"
6+
"strconv"
67
"strings"
78

9+
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
10+
811
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
912
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
10-
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
1113
)
1214

13-
// packersdk.Artifact implementation
15+
// packersdk.Artifact implementation.
1416
type Artifact struct {
1517
config *Config
1618
driver driver.Driver
@@ -21,15 +23,15 @@ type Artifact struct {
2123
StateData map[string]interface{}
2224
}
2325

24-
func (*Artifact) BuilderId() string {
25-
return BuilderId
26+
func (*Artifact) BuilderId() string { //nolint:revive // method is required by packer-plugin-sdk
27+
return BuilderID
2628
}
2729

2830
func (a *Artifact) Files() []string {
2931
return []string{}
3032
}
3133

32-
func (a *Artifact) Id() string {
34+
func (a *Artifact) Id() string { //nolint:revive // method is required by packer-plugin-sdk
3335
result := []string{}
3436
for _, t := range a.Templates {
3537
result = append(result, t.UUID)
@@ -59,7 +61,7 @@ func (a *Artifact) Destroy() error {
5961
for _, t := range a.Templates {
6062
err := a.driver.DeleteTemplate(ctx, t.UUID)
6163
if err != nil {
62-
return err
64+
return fmt.Errorf("failed to delete template %s: %w", t.UUID, err)
6365
}
6466
}
6567
return nil
@@ -68,11 +70,15 @@ func (a *Artifact) Destroy() error {
6870
func (a *Artifact) buildHCPPackerRegistryMetadata() ([]*image.Image, error) {
6971
var sourceTemplateUUID, sourceTemplateTitle string
7072
if v, ok := a.StateData["source_template_uuid"]; ok {
71-
sourceTemplateUUID = v.(string)
73+
if uuid, ok := v.(string); ok {
74+
sourceTemplateUUID = uuid
75+
}
7276
}
7377

7478
if v, ok := a.StateData["source_template_title"]; ok {
75-
sourceTemplateTitle = v.(string)
79+
if title, ok := v.(string); ok {
80+
sourceTemplateTitle = title
81+
}
7682
}
7783

7884
images := make([]*image.Image, 0)
@@ -83,15 +89,15 @@ func (a *Artifact) buildHCPPackerRegistryMetadata() ([]*image.Image, error) {
8389
image.WithProvider("upcloud"),
8490
)
8591
if err != nil {
86-
return images, err
92+
return images, fmt.Errorf("failed to create registry image for template %s: %w", template.UUID, err)
8793
}
8894

8995
img.SourceImageID = sourceTemplateUUID
9096
img.Labels["source_id"] = sourceTemplateUUID
9197
img.Labels["source"] = sourceTemplateTitle
9298
img.Labels["name"] = template.Title
9399
img.Labels["name_prefix"] = a.config.TemplatePrefix
94-
img.Labels["size"] = fmt.Sprint(template.Size)
100+
img.Labels["size"] = strconv.Itoa(template.Size)
95101
images = append(images, img)
96102
}
97103
return images, nil

builder/upcloud/artifact_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
package upcloud
1+
package upcloud //nolint:testpackage // not all fields can be exported in Artifact
22

33
import (
44
"fmt"
55
"testing"
66

7-
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
87
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
98
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
109
"github.com/stretchr/testify/assert"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1112
)
1213

1314
func TestArtifact_impl(t *testing.T) {
15+
t.Parallel()
1416
var _ packersdk.Artifact = new(Artifact)
1517
}
1618

1719
func TestArtifact_Id(t *testing.T) {
20+
t.Parallel()
1821
uuid1 := "some-uuid-1"
1922
uuid2 := "some-uuid-2"
2023
expected := fmt.Sprintf("%s,%s", uuid1, uuid2)
2124

2225
templates := []*upcloud.Storage{}
23-
templates = append(templates, &upcloud.Storage{UUID: uuid1})
24-
templates = append(templates, &upcloud.Storage{UUID: uuid2})
26+
templates = append(templates, &upcloud.Storage{UUID: uuid1}, &upcloud.Storage{UUID: uuid2})
2527

2628
a := &Artifact{Templates: templates}
2729
result := a.Id()
@@ -32,6 +34,7 @@ func TestArtifact_Id(t *testing.T) {
3234
}
3335

3436
func TestArtifact_String(t *testing.T) {
37+
t.Parallel()
3538
expected := `Storage template created, UUID: some-uuid`
3639

3740
templates := []*upcloud.Storage{}
@@ -46,6 +49,7 @@ func TestArtifact_String(t *testing.T) {
4649
}
4750

4851
func TestArtifact_Metadata(t *testing.T) {
52+
t.Parallel()
4953
templates := []*upcloud.Storage{}
5054
templates = append(templates,
5155
&upcloud.Storage{
@@ -74,7 +78,11 @@ func TestArtifact_Metadata(t *testing.T) {
7478
"source_template_uuid": "source-uuid",
7579
},
7680
}
77-
got := a.State(image.ArtifactStateURI).([]*image.Image)
81+
result := a.State(image.ArtifactStateURI)
82+
got, ok := result.([]*image.Image)
83+
if !ok {
84+
t.Fatalf("Expected []*image.Image, got %T", result)
85+
}
7886
want := &image.Image{
7987
ImageID: "some-uuid",
8088
ProviderName: "upcloud",

builder/upcloud/builder.go

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ package upcloud
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

8-
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
9-
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
109
"github.com/hashicorp/hcl/v2/hcldec"
1110
"github.com/hashicorp/packer-plugin-sdk/communicator"
1211
"github.com/hashicorp/packer-plugin-sdk/multistep"
1312
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
1413
"github.com/hashicorp/packer-plugin-sdk/packer"
1514
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
15+
16+
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
17+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1618
)
1719

1820
const (
19-
BuilderId = "upcloud.builder"
21+
BuilderID = "upcloud.builder"
2022
defaultTimeout time.Duration = 1 * time.Hour
2123
)
2224

@@ -28,7 +30,7 @@ type Builder struct {
2830

2931
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
3032

31-
func (b *Builder) Prepare(raws ...interface{}) (generatedVars []string, warnings []string, err error) {
33+
func (b *Builder) Prepare(raws ...interface{}) (generatedVars, warnings []string, err error) {
3234
warnings, errs := b.config.Prepare(raws...)
3335
if errs != nil {
3436
return nil, warnings, errs
@@ -65,43 +67,32 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
6567
generatedData := &packerbuilderdata.GeneratedData{State: state}
6668

6769
// Build the steps
68-
steps := []multistep.Step{
69-
&StepCreateSSHKey{
70-
Debug: b.config.PackerDebug,
71-
DebugKeyPath: fmt.Sprintf("ssh_key-%s.pem", b.config.PackerBuildName),
72-
},
73-
&StepCreateServer{
74-
Config: &b.config,
75-
GeneratedData: generatedData,
76-
},
77-
b.communicatorStep(),
78-
&commonsteps.StepProvision{},
79-
&commonsteps.StepCleanupTempKeys{
80-
Comm: &b.config.Comm,
81-
},
82-
&StepTeardownServer{},
83-
&StepCreateTemplate{
84-
Config: &b.config,
85-
GeneratedData: generatedData,
86-
},
87-
}
70+
steps := b.buildSteps(generatedData)
8871

8972
// Run
9073
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
9174
b.runner.Run(ctx, state)
9275

9376
// If there was an error, return that
9477
if err, ok := state.GetOk("error"); ok {
95-
return nil, err.(error)
78+
if errVal, ok := err.(error); ok {
79+
return nil, errVal
80+
}
81+
return nil, fmt.Errorf("unknown error type: %T", err)
9682
}
9783

9884
templates, ok := state.GetOk("templates")
9985
if !ok {
100-
return nil, fmt.Errorf("No template found in state, the build was probably cancelled")
86+
return nil, errors.New("no template found in state, the build was probably cancelled")
87+
}
88+
89+
templatesVal, ok := templates.([]*upcloud.Storage)
90+
if !ok {
91+
return nil, fmt.Errorf("templates is not of expected type []*upcloud.Storage, got %T", templates)
10192
}
10293

10394
artifact := &Artifact{
104-
Templates: templates.([]*upcloud.Storage),
95+
Templates: templatesVal,
10596
config: &b.config,
10697
driver: b.driver,
10798
StateData: map[string]interface{}{
@@ -116,9 +107,33 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
116107
return artifact, nil
117108
}
118109

110+
// buildSteps creates and returns the sequence of steps for the build process.
111+
func (b *Builder) buildSteps(generatedData *packerbuilderdata.GeneratedData) []multistep.Step {
112+
return []multistep.Step{
113+
&StepCreateSSHKey{
114+
Debug: b.config.PackerDebug,
115+
DebugKeyPath: fmt.Sprintf("ssh_key-%s.pem", b.config.PackerBuildName),
116+
},
117+
&StepCreateServer{
118+
Config: &b.config,
119+
GeneratedData: generatedData,
120+
},
121+
b.communicatorStep(),
122+
&commonsteps.StepProvision{},
123+
&commonsteps.StepCleanupTempKeys{
124+
Comm: &b.config.Comm,
125+
},
126+
&StepTeardownServer{},
127+
&StepCreateTemplate{
128+
Config: &b.config,
129+
GeneratedData: generatedData,
130+
},
131+
}
132+
}
133+
119134
// CommunicatorStep returns step based on communicator type
120135
// We currently support only SSH communicator but 'none' type
121-
// can also be used for e.g. testing purposes
136+
// can also be used for e.g. testing purposes.
122137
func (b *Builder) communicatorStep() multistep.Step {
123138
switch b.config.Comm.Type {
124139
case "none":

0 commit comments

Comments
 (0)