Skip to content

Commit 4e59a5e

Browse files
committed
feat: linter fixes
1 parent 9aaa625 commit 4e59a5e

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

internal/cmd/beta/image/create/create.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,15 @@ type imageConfig struct {
6767
type inputModel struct {
6868
*globalflags.GlobalFlagModel
6969

70+
Id *string
7071
Name string
7172
DiskFormat string
7273
LocalFilePath string
7374
Labels *map[string]string
7475
Config *imageConfig
7576
MinDiskSize *int64
7677
MinRam *int64
77-
Owner *string
7878
Protected *bool
79-
Scope *string
80-
Status *string
8179
}
8280

8381
func NewCmd(p *print.Printer) *cobra.Command {
@@ -90,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
9088
examples.NewExample(`Create a named imaged`, `$ stackit beta image create --name my-new-image --disk-format=raw --local-file-path=/my/raw/image`),
9189
examples.NewExample(`Create a named image with labels`, `$ stackit beta image create --name my-new-image --disk-format=raw --local-file-path=/my/raw/image--labels dev,amd64`),
9290
),
93-
RunE: func(cmd *cobra.Command, _ []string) error {
91+
RunE: func(cmd *cobra.Command, _ []string) (err error) {
9492
ctx := context.Background()
9593
model, err := parseInput(p, cmd)
9694
if err != nil {
@@ -108,7 +106,11 @@ func NewCmd(p *print.Printer) *cobra.Command {
108106
if err != nil {
109107
return fmt.Errorf("create image: file %q is not readable: %w", model.LocalFilePath, err)
110108
}
111-
defer file.Close()
109+
defer func() {
110+
if inner := file.Close(); inner != nil {
111+
err = fmt.Errorf("error closing input file: %w (%w)", inner, err)
112+
}
113+
}()
112114

113115
if !model.AssumeYes {
114116
prompt := fmt.Sprintf("Are you sure you want to create the image %q?", model.Name)
@@ -125,6 +127,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
125127
if err != nil {
126128
return fmt.Errorf("create image: %w", err)
127129
}
130+
model.Id = result.Id
128131
url, ok := result.GetUploadUrlOk()
129132
if !ok {
130133
return fmt.Errorf("create image: no upload URL has been provided")
@@ -145,7 +148,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
145148
return cmd
146149
}
147150

148-
func uploadFile(ctx context.Context, p *print.Printer, file *os.File, url string) error {
151+
func uploadFile(ctx context.Context, p *print.Printer, file *os.File, url string) (err error) {
149152
var filesize int64
150153
if stat, err := file.Stat(); err != nil {
151154
p.Debug(print.DebugLevel, "create image: cannot open file %q: %w", file.Name(), err)
@@ -168,6 +171,11 @@ func uploadFile(ctx context.Context, p *print.Printer, file *os.File, url string
168171
if err != nil {
169172
return fmt.Errorf("create image: error contacting server for upload: %w", err)
170173
}
174+
defer func() {
175+
if inner := uploadResponse.Body.Close(); inner != nil {
176+
err = fmt.Errorf("error closing file: %wqq (%w)", inner, err)
177+
}
178+
}()
171179
if uploadResponse.StatusCode != http.StatusOK {
172180
return fmt.Errorf("create image: server rejected image upload with %s", uploadResponse.Status)
173181
}
@@ -258,7 +266,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
258266
return request
259267
}
260268

261-
func createPayload(ctx context.Context, model *inputModel) iaas.CreateImagePayload {
269+
func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload {
262270
var labelsMap *map[string]any
263271
if model.Labels != nil && len(*model.Labels) > 0 {
264272
// convert map[string]string to map[string]interface{}
@@ -315,7 +323,7 @@ func outputResult(p *print.Printer, model *inputModel, resp *iaas.ImageCreateRes
315323

316324
return nil
317325
default:
318-
p.Outputf("Created image %q\n", model.Name)
326+
p.Outputf("Created image %q with id %s\n", model.Name, *model.Id)
319327
return nil
320328
}
321329
}

internal/cmd/beta/image/create/create_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
7878
return flagValues
7979
}
8080

81-
func parseLabels(labelstring string) *map[string]string {
81+
func parseLabels(labelstring string) map[string]string {
8282
labels := map[string]string{}
8383
for _, part := range strings.Split(labelstring, ",") {
8484
v := strings.Split(part, "=")
8585
labels[v[0]] = v[1]
8686
}
8787

88-
return &labels
88+
return labels
8989
}
9090

9191
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
@@ -94,7 +94,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
9494
Name: testName,
9595
DiskFormat: testDiskFormat,
9696
LocalFilePath: testLocalImagePath,
97-
Labels: parseLabels(testLabels),
97+
Labels: utils.Ptr(parseLabels(testLabels)),
9898
Config: &imageConfig{
9999
BootMenu: &testBootmenu,
100100
CdromBus: &testCdRomBus,
@@ -120,17 +120,6 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
120120
return model
121121
}
122122

123-
func toStringAnyMapPtr(m map[string]string) map[string]any {
124-
if m == nil {
125-
return nil
126-
}
127-
result := map[string]any{}
128-
for k, v := range m {
129-
result[k] = v
130-
}
131-
return result
132-
}
133-
134123
func fixtureCreatePayload(mods ...func(payload *iaas.CreateImagePayload)) (payload iaas.CreateImagePayload) {
135124
payload = iaas.CreateImagePayload{
136125
Config: &iaas.ImageConfig{
@@ -162,7 +151,7 @@ func fixtureCreatePayload(mods ...func(payload *iaas.CreateImagePayload)) (paylo
162151
for _, mod := range mods {
163152
mod(&payload)
164153
}
165-
return
154+
return payload
166155
}
167156

168157
func fixtureRequest(mods ...func(request *iaas.ApiCreateImageRequest)) iaas.ApiCreateImageRequest {
@@ -347,7 +336,6 @@ func TestBuildRequest(t *testing.T) {
347336
if diff != "" {
348337
t.Fatalf("Data does not match: %s", diff)
349338
}
350-
351339
})
352340
}
353341
}

internal/cmd/beta/image/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package delete
1+
package delete
22

33
import (
44
"context"

internal/cmd/beta/image/describe/describe_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ var projectIdFlag = globalflags.ProjectIdFlag
1818
type testCtxKey struct{}
1919

2020
var (
21-
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
22-
testClient = &iaas.APIClient{}
23-
testProjectId = uuid.NewString()
24-
testImageId = []string{uuid.NewString()}
21+
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
22+
testClient = &iaas.APIClient{}
23+
testProjectId = uuid.NewString()
24+
testImageId = []string{uuid.NewString()}
2525
)
2626

2727
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
@@ -37,7 +37,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
3737
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
3838
model := &inputModel{
3939
GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Verbosity: globalflags.VerbosityDefault},
40-
ImageId: testImageId[0],
40+
ImageId: testImageId[0],
4141
}
4242
for _, mod := range mods {
4343
mod(model)

internal/cmd/beta/image/list/list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ func outputResult(p *print.Printer, outputFormat string, items []iaas.Image) err
150150
}
151151
if v := cfg.OperatingSystemDistro; v != nil && v.IsSet() {
152152
distro = *v.Get()
153-
154153
}
155154
if v := cfg.OperatingSystemVersion; v != nil && v.IsSet() {
156155
version = *v.Get()

0 commit comments

Comments
 (0)