Skip to content

Commit

Permalink
add golangci-lint adn clean up a bit (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 authored Oct 15, 2024
1 parent 62ddeb2 commit 4d49824
Show file tree
Hide file tree
Showing 44 changed files with 827 additions and 753 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read
pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
/Godeps/
/build/
/.terraform/
.vscode/
65 changes: 33 additions & 32 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
// http://www.apache.org/licenses/LICENSE-2.0
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

Expand All @@ -24,18 +23,19 @@ import (
"syscall"
"time"

commandline "github.com/aws/amazon-ec2-instance-selector/v3/pkg/cli"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/env"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/instancetypes"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector/outputs"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/sorter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"go.uber.org/multierr"

commandline "github.com/aws/amazon-ec2-instance-selector/v3/pkg/cli"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/env"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/instancetypes"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/selector/outputs"
"github.com/aws/amazon-ec2-instance-selector/v3/pkg/sorter"
)

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

tableOutput = "table"
tableWideOutput = "table-wide"
oneLine = "one-line"
bubbleTeaOutput = "interactive"

// Sort filter default
// Sort filter default.
instanceNamePath = ".InstanceType"
)

// Filter Flag Constants
// Filter Flag Constants.
const (
vcpus = "vcpus"
memory = "memory"
Expand Down Expand Up @@ -106,14 +106,14 @@ const (
generation = "generation"
)

// Aggregate Filter Flags
// Aggregate Filter Flags.
const (
instanceTypeBase = "base-instance-type"
flexible = "flexible"
service = "service"
)

// Configuration Flag Constants
// Configuration Flag Constants.
const (
maxResults = "max-results"
profile = "profile"
Expand All @@ -128,13 +128,10 @@ const (
sortBy = "sort-by"
)

var (
// versionID is overridden at compilation with the version based on the git tag
versionID = "dev"
)
// versionID is overridden at compilation with the version based on the git tag
var versionID = "dev"

func main() {

log.SetOutput(os.Stderr)
log.SetPrefix("NOTE: ")
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
Expand Down Expand Up @@ -276,7 +273,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
cacheTTLDuration := time.Hour * time.Duration(*cli.IntMe(flags[cacheTTL]))
instanceSelector, err := selector.NewWithCache(ctx, cfg, cacheTTLDuration, *cli.StringMe(flags[cacheDir]))
if err != nil {
fmt.Printf("An error occurred when initialising the ec2 selector: %v", err)
fmt.Printf("An error occurred when initializing the ec2 selector: %v", err)
os.Exit(1)
}
if flags[debug] != nil {
Expand Down Expand Up @@ -477,7 +474,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
var instanceTypes []string
if outputFlag != nil && *outputFlag == bubbleTeaOutput {
p := tea.NewProgram(outputs.NewBubbleTeaModel(instanceTypesDetails), tea.WithMouseCellMotion())
if err := p.Start(); err != nil {
if _, err := p.Run(); err != nil {
fmt.Printf("An error occurred when starting bubble tea: %v", err)
os.Exit(1)
}
Expand Down Expand Up @@ -516,7 +513,7 @@ func hydrateCaches(ctx context.Context, instanceSelector selector.Selector) (err
defer waitGroup.Done()
if instanceSelector.EC2Pricing.OnDemandCacheCount() == 0 {
if err := instanceSelector.EC2Pricing.RefreshOnDemandCache(ctx); err != nil {
return multierr.Append(errs, fmt.Errorf("There was a problem refreshing the on-demand pricing cache: %w", err))
return multierr.Append(errs, fmt.Errorf("there was a problem refreshing the on-demand pricing cache: %w", err))
}
}
return nil
Expand All @@ -525,7 +522,7 @@ func hydrateCaches(ctx context.Context, instanceSelector selector.Selector) (err
defer waitGroup.Done()
if instanceSelector.EC2Pricing.SpotCacheCount() == 0 {
if err := instanceSelector.EC2Pricing.RefreshSpotCache(ctx, spotPricingDaysBack); err != nil {
return multierr.Append(errs, fmt.Errorf("There was a problem refreshing the spot pricing cache: %w", err))
return multierr.Append(errs, fmt.Errorf("there was a problem refreshing the spot pricing cache: %w", err))
}
}
return nil
Expand All @@ -534,15 +531,19 @@ func hydrateCaches(ctx context.Context, instanceSelector selector.Selector) (err
defer waitGroup.Done()
if instanceSelector.InstanceTypesProvider.CacheCount() == 0 {
if _, err := instanceSelector.InstanceTypesProvider.Get(ctx, nil); err != nil {
return multierr.Append(errs, fmt.Errorf("There was a problem refreshing the instance types cache: %w", err))
return multierr.Append(errs, fmt.Errorf("there was a problem refreshing the instance types cache: %w", err))
}
}
return nil
},
}
wg.Add(len(hydrateTasks))
for _, task := range hydrateTasks {
go task(wg)
go func() {
if err := task(wg); err != nil {
log.Printf("Hydrate task error: %v", err)
}
}()
}
wg.Wait()
return errs
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/muesli/termenv v0.15.2
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/samber/lo v1.47.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
go.uber.org/multierr v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
14 changes: 14 additions & 0 deletions pkg/awsapi/selectorec2.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package awsapi

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
)

Expand Down
51 changes: 25 additions & 26 deletions pkg/bytequantity/bytequantity.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
// http://www.apache.org/licenses/LICENSE-2.0
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bytequantity

Expand All @@ -22,7 +21,7 @@ import (
)

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

// ByteQuantity is a data type representing a byte quantity
// ByteQuantity is a data type representing a byte quantity.
type ByteQuantity struct {
Quantity uint64
}
Expand All @@ -54,7 +53,7 @@ func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) {
}
quantity := uint64(0)
switch strings.ToLower(string(unit[0])) {
//mib
// mib
case "m":
inputDecSplit := strings.Split(quantityStr, ".")
if len(inputDecSplit) == 2 {
Expand All @@ -72,19 +71,19 @@ func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) {
if err != nil {
return ByteQuantity{}, err
}
//gib
// gib
case "g":
quantityDec, err := strconv.ParseFloat(quantityStr, 10)
quantityDec, err := strconv.ParseFloat(quantityStr, 64)
if err != nil {
return ByteQuantity{}, err
}
if quantityDec > maxGiB {
return ByteQuantity{}, fmt.Errorf("error GiB value is too large")
}
quantity = uint64(quantityDec * gbConvert)
//tib
// tib
case "t":
quantityDec, err := strconv.ParseFloat(quantityStr, 10)
quantityDec, err := strconv.ParseFloat(quantityStr, 64)
if err != nil {
return ByteQuantity{}, err
}
Expand All @@ -101,53 +100,53 @@ func ParseToByteQuantity(byteQuantityStr string) (ByteQuantity, error) {
}, nil
}

// FromTiB returns a byte quantity of the passed in tebibytes quantity
// FromTiB returns a byte quantity of the passed in tebibytes quantity.
func FromTiB(tib uint64) ByteQuantity {
return ByteQuantity{
Quantity: tib * tbConvert,
}
}

// FromGiB returns a byte quantity of the passed in gibibytes quantity
// FromGiB returns a byte quantity of the passed in gibibytes quantity.
func FromGiB(gib uint64) ByteQuantity {
return ByteQuantity{
Quantity: gib * gbConvert,
}
}

// FromMiB returns a byte quantity of the passed in mebibytes quantity
// FromMiB returns a byte quantity of the passed in mebibytes quantity.
func FromMiB(mib uint64) ByteQuantity {
return ByteQuantity{
Quantity: mib,
}
}

// StringMiB returns a byte quantity in a mebibytes string representation
// StringMiB returns a byte quantity in a mebibytes string representation.
func (bq ByteQuantity) StringMiB() string {
return fmt.Sprintf("%.0f %s", bq.MiB(), mib)
}

// StringGiB returns a byte quantity in a gibibytes string representation
// StringGiB returns a byte quantity in a gibibytes string representation.
func (bq ByteQuantity) StringGiB() string {
return fmt.Sprintf("%.3f %s", bq.GiB(), gib)
}

// StringTiB returns a byte quantity in a tebibytes string representation
// StringTiB returns a byte quantity in a tebibytes string representation.
func (bq ByteQuantity) StringTiB() string {
return fmt.Sprintf("%.3f %s", bq.TiB(), tib)
}

// MiB returns a byte quantity in mebibytes
// MiB returns a byte quantity in mebibytes.
func (bq ByteQuantity) MiB() float64 {
return float64(bq.Quantity)
}

// GiB returns a byte quantity in gibibytes
// GiB returns a byte quantity in gibibytes.
func (bq ByteQuantity) GiB() float64 {
return float64(bq.Quantity) * 1 / gbConvert
}

// TiB returns a byte quantity in tebibytes
// TiB returns a byte quantity in tebibytes.
func (bq ByteQuantity) TiB() float64 {
return float64(bq.Quantity) * 1 / tbConvert
}
14 changes: 13 additions & 1 deletion pkg/bytequantity/bytequantity_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bytequantity_test

import (
Expand All @@ -9,7 +22,6 @@ import (
)

func TestParseToByteQuantity(t *testing.T) {

for _, testQuantity := range []string{"10mb", "10 mb", "10.0 mb", "10.0mb", "10m", "10mib", "10 M", "10.000 MiB"} {
expectationVal := uint64(10)
bq, err := bytequantity.ParseToByteQuantity(testQuantity)
Expand Down
Loading

0 comments on commit 4d49824

Please sign in to comment.