Skip to content

Commit bcb9426

Browse files
committed
add golangci-lint adn clean up a bit
1 parent 62ddeb2 commit bcb9426

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+827
-753
lines changed

.github/workflows/golangci-lint.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
golangci:
14+
name: lint
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-go@v5
19+
with:
20+
go-version: stable
21+
- name: golangci-lint
22+
uses: golangci/golangci-lint-action@v6
23+
with:
24+
version: v1.60

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
/Godeps/
2121
/build/
2222
/.terraform/
23+
.vscode/

cmd/main.go

+33-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
24
//
3-
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4-
// not use this file except in compliance with the License. A copy of the
5-
// License is located at
5+
// http://www.apache.org/licenses/LICENSE-2.0
66
//
7-
// http://aws.amazon.com/apache2.0/
8-
//
9-
// or in the "license" file accompanying this file. This file is distributed
10-
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11-
// express or implied. See the License for the specific language governing
12-
// permissions and limitations under the License.
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
1312

1413
package main
1514

@@ -24,18 +23,19 @@ import (
2423
"syscall"
2524
"time"
2625

27-
commandline "github.com/aws/amazon-ec2-instance-selector/v3/pkg/cli"
28-
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/env"
29-
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/instancetypes"
30-
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector"
31-
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector/outputs"
32-
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/sorter"
3326
"github.com/aws/aws-sdk-go-v2/aws"
3427
"github.com/aws/aws-sdk-go-v2/config"
3528
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
3629
tea "github.com/charmbracelet/bubbletea"
3730
"github.com/spf13/cobra"
3831
"go.uber.org/multierr"
32+
33+
commandline "github.com/aws/amazon-ec2-instance-selector/v3/pkg/cli"
34+
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/env"
35+
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/instancetypes"
36+
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector"
37+
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector/outputs"
38+
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/sorter"
3939
)
4040

4141
const (
@@ -45,19 +45,19 @@ const (
4545
defaultProfile = "default"
4646
awsConfigFile = "~/.aws/config"
4747
// 0 means the last price
48-
// increasing this results in a lot more API calls to EC2 which can slow things down
48+
// increasing this results in a lot more API calls to EC2 which can slow things down.
4949
spotPricingDaysBack = 0
5050

5151
tableOutput = "table"
5252
tableWideOutput = "table-wide"
5353
oneLine = "one-line"
5454
bubbleTeaOutput = "interactive"
5555

56-
// Sort filter default
56+
// Sort filter default.
5757
instanceNamePath = ".InstanceType"
5858
)
5959

60-
// Filter Flag Constants
60+
// Filter Flag Constants.
6161
const (
6262
vcpus = "vcpus"
6363
memory = "memory"
@@ -106,14 +106,14 @@ const (
106106
generation = "generation"
107107
)
108108

109-
// Aggregate Filter Flags
109+
// Aggregate Filter Flags.
110110
const (
111111
instanceTypeBase = "base-instance-type"
112112
flexible = "flexible"
113113
service = "service"
114114
)
115115

116-
// Configuration Flag Constants
116+
// Configuration Flag Constants.
117117
const (
118118
maxResults = "max-results"
119119
profile = "profile"
@@ -128,13 +128,10 @@ const (
128128
sortBy = "sort-by"
129129
)
130130

131-
var (
132-
// versionID is overridden at compilation with the version based on the git tag
133-
versionID = "dev"
134-
)
131+
// versionID is overridden at compilation with the version based on the git tag
132+
var versionID = "dev"
135133

136134
func main() {
137-
138135
log.SetOutput(os.Stderr)
139136
log.SetPrefix("NOTE: ")
140137
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
@@ -276,7 +273,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
276273
cacheTTLDuration := time.Hour * time.Duration(*cli.IntMe(flags[cacheTTL]))
277274
instanceSelector, err := selector.NewWithCache(ctx, cfg, cacheTTLDuration, *cli.StringMe(flags[cacheDir]))
278275
if err != nil {
279-
fmt.Printf("An error occurred when initialising the ec2 selector: %v", err)
276+
fmt.Printf("An error occurred when initializing the ec2 selector: %v", err)
280277
os.Exit(1)
281278
}
282279
if flags[debug] != nil {
@@ -477,7 +474,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
477474
var instanceTypes []string
478475
if outputFlag != nil && *outputFlag == bubbleTeaOutput {
479476
p := tea.NewProgram(outputs.NewBubbleTeaModel(instanceTypesDetails), tea.WithMouseCellMotion())
480-
if err := p.Start(); err != nil {
477+
if _, err := p.Run(); err != nil {
481478
fmt.Printf("An error occurred when starting bubble tea: %v", err)
482479
os.Exit(1)
483480
}
@@ -516,7 +513,7 @@ func hydrateCaches(ctx context.Context, instanceSelector selector.Selector) (err
516513
defer waitGroup.Done()
517514
if instanceSelector.EC2Pricing.OnDemandCacheCount() == 0 {
518515
if err := instanceSelector.EC2Pricing.RefreshOnDemandCache(ctx); err != nil {
519-
return multierr.Append(errs, fmt.Errorf("There was a problem refreshing the on-demand pricing cache: %w", err))
516+
return multierr.Append(errs, fmt.Errorf("there was a problem refreshing the on-demand pricing cache: %w", err))
520517
}
521518
}
522519
return nil
@@ -525,7 +522,7 @@ func hydrateCaches(ctx context.Context, instanceSelector selector.Selector) (err
525522
defer waitGroup.Done()
526523
if instanceSelector.EC2Pricing.SpotCacheCount() == 0 {
527524
if err := instanceSelector.EC2Pricing.RefreshSpotCache(ctx, spotPricingDaysBack); err != nil {
528-
return multierr.Append(errs, fmt.Errorf("There was a problem refreshing the spot pricing cache: %w", err))
525+
return multierr.Append(errs, fmt.Errorf("there was a problem refreshing the spot pricing cache: %w", err))
529526
}
530527
}
531528
return nil
@@ -534,15 +531,19 @@ func hydrateCaches(ctx context.Context, instanceSelector selector.Selector) (err
534531
defer waitGroup.Done()
535532
if instanceSelector.InstanceTypesProvider.CacheCount() == 0 {
536533
if _, err := instanceSelector.InstanceTypesProvider.Get(ctx, nil); err != nil {
537-
return multierr.Append(errs, fmt.Errorf("There was a problem refreshing the instance types cache: %w", err))
534+
return multierr.Append(errs, fmt.Errorf("there was a problem refreshing the instance types cache: %w", err))
538535
}
539536
}
540537
return nil
541538
},
542539
}
543540
wg.Add(len(hydrateTasks))
544541
for _, task := range hydrateTasks {
545-
go task(wg)
542+
go func() {
543+
if err := task(wg); err != nil {
544+
log.Printf("Hydrate task error: %v", err)
545+
}
546+
}()
546547
}
547548
wg.Wait()
548549
return errs

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/muesli/termenv v0.15.2
1818
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
1919
github.com/patrickmn/go-cache v2.1.0+incompatible
20+
github.com/samber/lo v1.47.0
2021
github.com/spf13/cobra v1.8.1
2122
github.com/spf13/pflag v1.0.5
2223
go.uber.org/multierr v1.11.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
8989
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9090
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
9191
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
92+
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
93+
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
9294
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
9395
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
9496
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

pkg/awsapi/selectorec2.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package awsapi
215

316
import (
417
"context"
18+
519
"github.com/aws/aws-sdk-go-v2/service/ec2"
620
)
721

pkg/bytequantity/bytequantity.go

+25-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
24
//
3-
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4-
// not use this file except in compliance with the License. A copy of the
5-
// License is located at
5+
// http://www.apache.org/licenses/LICENSE-2.0
66
//
7-
// http://aws.amazon.com/apache2.0/
8-
//
9-
// or in the "license" file accompanying this file. This file is distributed
10-
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11-
// express or implied. See the License for the specific language governing
12-
// permissions and limitations under the License.
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
1312

1413
package bytequantity
1514

@@ -22,7 +21,7 @@ import (
2221
)
2322

2423
const (
25-
/// Examples: 1mb, 1 gb, 1.0tb, 1mib, 2g, 2.001 t
24+
/// Examples: 1mb, 1 gb, 1.0tb, 1mib, 2g, 2.001 t.
2625
byteQuantityRegex = `^([0-9]+\.?[0-9]{0,3})[ ]?(mi?b?|gi?b?|ti?b?)?$`
2726
mib = "MiB"
2827
gib = "GiB"
@@ -33,7 +32,7 @@ const (
3332
maxTiB = math.MaxUint64 / tbConvert
3433
)
3534

36-
// ByteQuantity is a data type representing a byte quantity
35+
// ByteQuantity is a data type representing a byte quantity.
3736
type ByteQuantity struct {
3837
Quantity uint64
3938
}
@@ -54,7 +53,7 @@ func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) {
5453
}
5554
quantity := uint64(0)
5655
switch strings.ToLower(string(unit[0])) {
57-
//mib
56+
// mib
5857
case "m":
5958
inputDecSplit := strings.Split(quantityStr, ".")
6059
if len(inputDecSplit) == 2 {
@@ -72,19 +71,19 @@ func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) {
7271
if err != nil {
7372
return ByteQuantity{}, err
7473
}
75-
//gib
74+
// gib
7675
case "g":
77-
quantityDec, err := strconv.ParseFloat(quantityStr, 10)
76+
quantityDec, err := strconv.ParseFloat(quantityStr, 64)
7877
if err != nil {
7978
return ByteQuantity{}, err
8079
}
8180
if quantityDec > maxGiB {
8281
return ByteQuantity{}, fmt.Errorf("error GiB value is too large")
8382
}
8483
quantity = uint64(quantityDec * gbConvert)
85-
//tib
84+
// tib
8685
case "t":
87-
quantityDec, err := strconv.ParseFloat(quantityStr, 10)
86+
quantityDec, err := strconv.ParseFloat(quantityStr, 64)
8887
if err != nil {
8988
return ByteQuantity{}, err
9089
}
@@ -101,53 +100,53 @@ func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) {
101100
}, nil
102101
}
103102

104-
// FromTiB returns a byte quantity of the passed in tebibytes quantity
103+
// FromTiB returns a byte quantity of the passed in tebibytes quantity.
105104
func FromTiB(tib uint64) ByteQuantity {
106105
return ByteQuantity{
107106
Quantity: tib * tbConvert,
108107
}
109108
}
110109

111-
// FromGiB returns a byte quantity of the passed in gibibytes quantity
110+
// FromGiB returns a byte quantity of the passed in gibibytes quantity.
112111
func FromGiB(gib uint64) ByteQuantity {
113112
return ByteQuantity{
114113
Quantity: gib * gbConvert,
115114
}
116115
}
117116

118-
// FromMiB returns a byte quantity of the passed in mebibytes quantity
117+
// FromMiB returns a byte quantity of the passed in mebibytes quantity.
119118
func FromMiB(mib uint64) ByteQuantity {
120119
return ByteQuantity{
121120
Quantity: mib,
122121
}
123122
}
124123

125-
// StringMiB returns a byte quantity in a mebibytes string representation
124+
// StringMiB returns a byte quantity in a mebibytes string representation.
126125
func (bq ByteQuantity) StringMiB() string {
127126
return fmt.Sprintf("%.0f %s", bq.MiB(), mib)
128127
}
129128

130-
// StringGiB returns a byte quantity in a gibibytes string representation
129+
// StringGiB returns a byte quantity in a gibibytes string representation.
131130
func (bq ByteQuantity) StringGiB() string {
132131
return fmt.Sprintf("%.3f %s", bq.GiB(), gib)
133132
}
134133

135-
// StringTiB returns a byte quantity in a tebibytes string representation
134+
// StringTiB returns a byte quantity in a tebibytes string representation.
136135
func (bq ByteQuantity) StringTiB() string {
137136
return fmt.Sprintf("%.3f %s", bq.TiB(), tib)
138137
}
139138

140-
// MiB returns a byte quantity in mebibytes
139+
// MiB returns a byte quantity in mebibytes.
141140
func (bq ByteQuantity) MiB() float64 {
142141
return float64(bq.Quantity)
143142
}
144143

145-
// GiB returns a byte quantity in gibibytes
144+
// GiB returns a byte quantity in gibibytes.
146145
func (bq ByteQuantity) GiB() float64 {
147146
return float64(bq.Quantity) * 1 / gbConvert
148147
}
149148

150-
// TiB returns a byte quantity in tebibytes
149+
// TiB returns a byte quantity in tebibytes.
151150
func (bq ByteQuantity) TiB() float64 {
152151
return float64(bq.Quantity) * 1 / tbConvert
153152
}

pkg/bytequantity/bytequantity_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package bytequantity_test
215

316
import (
@@ -9,7 +22,6 @@ import (
922
)
1023

1124
func TestParseToByteQuantity(t *testing.T) {
12-
1325
for _, testQuantity := range []string{"10mb", "10 mb", "10.0 mb", "10.0mb", "10m", "10mib", "10 M", "10.000 MiB"} {
1426
expectationVal := uint64(10)
1527
bq, err := bytequantity.ParseToByteQuantity(testQuantity)

0 commit comments

Comments
 (0)