Skip to content

Commit

Permalink
chore: add support for gofumpt (#4055)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Aug 21, 2024
1 parent 3bdcce0 commit 44f37c5
Show file tree
Hide file tree
Showing 93 changed files with 392 additions and 475 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ linters:
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
- gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
- goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt. [fast: true, auto-fix: true]
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. [fast: true, auto-fix: false]
- goprintffuncname # Checks that printf-like functions are named with `f` at the end [fast: true, auto-fix: false]
Expand Down Expand Up @@ -73,6 +74,9 @@ linters:
linters-settings:
goconst:
min-len: 5
gosec:
excludes:
- G115

issues:
exclude-dirs:
Expand Down
34 changes: 16 additions & 18 deletions internal/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,22 @@ func (a RawArgs) ExistsArgByName(name string) bool {
return ok
}

var (
scalarKinds = map[reflect.Kind]bool{
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Float32: true,
reflect.Float64: true,
reflect.Bool: true,
reflect.String: true,
}
)
var scalarKinds = map[reflect.Kind]bool{
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Float32: true,
reflect.Float64: true,
reflect.Bool: true,
reflect.String: true,
}

// SplitRaw creates a map that maps arg names to their values.
// ["arg1=1", "arg2=2", "arg3"] => {"arg1": "1", "arg2": "2", "arg3":"" }
Expand Down
21 changes: 7 additions & 14 deletions internal/args/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
// Marshal & Unmarshal Errors
//

type DataMustBeAPointerError struct {
}
type DataMustBeAPointerError struct{}

func (e *DataMustBeAPointerError) Error() string {
return "data must be a pointer to a struct"
Expand All @@ -21,8 +20,7 @@ func (e *DataMustBeAPointerError) Error() string {
// Marshal Errors
//

type DataMustBeAMarshalableValueError struct {
}
type DataMustBeAMarshalableValueError struct{}

func (e *DataMustBeAMarshalableValueError) Error() string {
return "data must be a marshalable value (a scalar type or a Marshaler)"
Expand Down Expand Up @@ -71,22 +69,19 @@ func (e *UnmarshalArgError) Unwrap() error {
return e.Err
}

type InvalidArgNameError struct {
}
type InvalidArgNameError struct{}

func (e *InvalidArgNameError) Error() string {
return "arg name must only contain lowercase letters, numbers or dashes"
}

type UnknownArgError struct {
}
type UnknownArgError struct{}

func (e *UnknownArgError) Error() string {
return "unknown argument"
}

type DuplicateArgError struct {
}
type DuplicateArgError struct{}

func (e *DuplicateArgError) Error() string {
return "duplicate argument"
Expand All @@ -100,8 +95,7 @@ func (e *CannotSetNestedFieldError) Error() string {
return fmt.Sprintf("cannot set nested field for unmarshalable type %T", e.Dest)
}

type MissingIndexOnArrayError struct {
}
type MissingIndexOnArrayError struct{}

func (e *MissingIndexOnArrayError) Error() string {
return "missing index on the array"
Expand Down Expand Up @@ -129,8 +123,7 @@ func (e *MissingIndicesInArrayError) Error() string {
}
}

type MissingMapKeyError struct {
}
type MissingMapKeyError struct{}

func (e *MissingMapKeyError) Error() string {
return "missing map key"
Expand Down
2 changes: 1 addition & 1 deletion internal/args/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestMarshal(t *testing.T) {
t.Run("well-known-types", run(TestCase{
data: &WellKnownTypes{
Size: 20 * scw.GB,
Time: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC),
Time: time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC),
},
expected: []string{
"size=20GB",
Expand Down
5 changes: 3 additions & 2 deletions internal/args/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestUnmarshalStruct(t *testing.T) {
"time=2006-01-02T15:04:05Z",
},
expected: &WellKnownTypes{
Time: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC),
Time: time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC),
},
}))

Expand All @@ -260,7 +260,7 @@ func TestUnmarshalStruct(t *testing.T) {
"time=+1m1s",
},
expected: &WellKnownTypes{
Time: time.Date(1970, 01, 01, 0, 1, 1, 0, time.UTC),
Time: time.Date(1970, 1, 1, 0, 1, 1, 0, time.UTC),
},
}))

Expand Down Expand Up @@ -533,6 +533,7 @@ func TestUnmarshalStruct(t *testing.T) {
error: "cannot unmarshal arg 'strings': missing index on the array",
}))
}

func TestIsUmarshalableValue(t *testing.T) {
type TestCase struct {
expected bool
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
ScwConfigPathEnv = "SCW_CLI_CONFIG_PATH"

DefaultConfigFileName = "cli.yaml"
defaultConfigPermission = 0644
defaultConfigPermission = 0o644

DefaultOutput = "human"
configFileTemplate = `# Scaleway CLI config file
Expand Down Expand Up @@ -101,7 +101,7 @@ func (c *Config) Save() error {
return err
}

err = os.MkdirAll(filepath.Dir(c.path), 0700)
err = os.MkdirAll(filepath.Dir(c.path), 0o700)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions internal/config/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"runtime"
)

var (
// List of env variables where to find the editor to use
// Order in slice is override order, the latest will override the first ones
editorEnvVariables = []string{"EDITOR", "VISUAL"}
)
// List of env variables where to find the editor to use
// Order in slice is override order, the latest will override the first ones
var editorEnvVariables = []string{"EDITOR", "VISUAL"}

func GetSystemDefaultEditor() string {
switch runtime.GOOS {
Expand Down
10 changes: 4 additions & 6 deletions internal/core/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,8 @@ func TestAutocompleteArgs(t *testing.T) {
Namespace: "test",
Resource: "flower",
Verb: "list",
ArgsType: reflect.TypeOf(struct {
}{}),
ArgSpecs: core.ArgSpecs{},
ArgsType: reflect.TypeOf(struct{}{}),
ArgSpecs: core.ArgSpecs{},
Run: func(_ context.Context, _ interface{}) (interface{}, error) {
return []*struct {
Name string
Expand All @@ -239,9 +238,8 @@ func TestAutocompleteArgs(t *testing.T) {
Namespace: "test",
Resource: "material",
Verb: "list",
ArgsType: reflect.TypeOf(struct {
}{}),
ArgSpecs: core.ArgSpecs{},
ArgsType: reflect.TypeOf(struct{}{}),
ArgSpecs: core.ArgSpecs{},
Run: func(_ context.Context, _ interface{}) (interface{}, error) {
return []*struct {
Name string
Expand Down
1 change: 0 additions & 1 deletion internal/core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
rootCmd.SetArgs(args)
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
err = rootCmd.Execute()

if err != nil {
if _, ok := err.(*interactive.InterruptError); ok {
return 130, nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/core/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func GetLatestVersionUpdateFilePath(cacheDir string) string {

// CreateAndCloseFile creates a file and closes it. It returns true on succeed, false on failure.
func CreateAndCloseFile(path string) error {
err := os.MkdirAll(filepath.Dir(path), 0700)
err := os.MkdirAll(filepath.Dir(path), 0o700)
if err != nil {
return fmt.Errorf("failed creating path %s: %s", path, err)
}
newFile, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
newFile, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0o600)
if err != nil {
return fmt.Errorf("failed creating file %s: %s", path, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type cobraBuilder struct {

// build creates the cobra root command.
func (b *cobraBuilder) build() *cobra.Command {
var groups = map[string]*cobra.Group{
groups := map[string]*cobra.Group{
"available": {ID: "available", Title: "AVAILABLE"},
"labs": {ID: "labs", Title: "AVAILABLE LABS"},
"config": {ID: "config", Title: "CONFIGURATION"},
Expand Down
2 changes: 1 addition & 1 deletion internal/core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

// Command represent a CLI command. From this higher level type we create Cobra command objects.
type Command struct {

// Namespace is the top level entry point of a command. (e.g scw instance)
Namespace string

Expand Down Expand Up @@ -271,6 +270,7 @@ func (c *Commands) RemoveResource(namespace, resource string) {
}
}
}

func (c *Commands) Add(cmd *Command) {
c.commands = append(c.commands, cmd)
c.commandIndex[cmd.getPath()] = cmd
Expand Down
1 change: 0 additions & 1 deletion internal/core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
// CliError is an all-in-one error structure that can be used in commands to return useful errors to the user.
// CliError implements JSON and human marshaler for a smooth experience.
type CliError struct {

// The original error that triggers this CLI error.
// The Err.String() will be print in red to the user.
Err error
Expand Down
1 change: 1 addition & 0 deletions internal/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func InvalidSecretKeyError(value string) *CliError {
Hint: "secret_key should be a valid UUID, formatted as: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.",
}
}

func InvalidAccessKeyError(value string) *CliError {
return &CliError{
Err: fmt.Errorf("invalid access_key '%v'", value),
Expand Down
5 changes: 2 additions & 3 deletions internal/core/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

// Example represents an example for the usage of a CLI command.
type Example struct {

// Short is the title given to the example.
Short string

Expand All @@ -32,11 +31,11 @@ func (e *Example) GetCommandLine(binaryName string, cmd *Command) string {
case e.ArgsJSON != "":
// Query and path parameters don't have json tag,
// so we need to enforce a JSON tag on every field to make this work.
var cmdArgs = newObjectWithForcedJSONTags(cmd.ArgsType)
cmdArgs := newObjectWithForcedJSONTags(cmd.ArgsType)
if err := json.Unmarshal([]byte(e.ArgsJSON), cmdArgs); err != nil {
panic(fmt.Errorf("in command '%s', example '%s': %w", cmd.getPath(), cmd.Short, err))
}
var cmdArgsAsStrings, err = args.MarshalStruct(cmdArgs)
cmdArgsAsStrings, err := args.MarshalStruct(cmdArgs)
positionalArg := cmd.ArgSpecs.GetPositionalArg()
if positionalArg != nil {
for i, cmdArg := range cmdArgsAsStrings {
Expand Down
1 change: 0 additions & 1 deletion internal/core/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func Test_getValuesForFieldByName(t *testing.T) {
},
},
{

name: "Special test",
testCase: TestCase{
cmdArgs: &SpecialRequest{
Expand Down
15 changes: 7 additions & 8 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ type AfterFuncCtx struct {

// TestConfig contain configuration that can be used with the Test function
type TestConfig struct {

// Array of command to load (see main.go)
Commands *Commands

Expand Down Expand Up @@ -619,7 +618,7 @@ type GoldenReplacement struct {
// GoldenReplacePatterns replace the list of patterns with their given replacement
func GoldenReplacePatterns(golden string, replacements ...GoldenReplacement) (string, error) {
var matchFailed []string
var changedGolden = golden
changedGolden := golden

for _, replacement := range replacements {
if !replacement.Pattern.MatchString(changedGolden) {
Expand Down Expand Up @@ -647,8 +646,8 @@ func TestCheckGoldenAndReplacePatterns(replacements ...GoldenReplacement) TestCh
goldenPath := getTestFilePath(t, ".golden")
// In order to avoid diff in goldens we set all timestamp to the same date
if *UpdateGoldens {
require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0755))
require.NoError(t, os.WriteFile(goldenPath, []byte(actual), 0644)) //nolint:gosec
require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0o755))
require.NoError(t, os.WriteFile(goldenPath, []byte(actual), 0o644)) //nolint:gosec
}

expected, err := os.ReadFile(goldenPath)
Expand All @@ -666,8 +665,8 @@ func TestCheckGolden() TestCheck {
goldenPath := getTestFilePath(t, ".golden")
// In order to avoid diff in goldens we set all timestamp to the same date
if *UpdateGoldens {
require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0755))
require.NoError(t, os.WriteFile(goldenPath, []byte(actual), 0644)) //nolint:gosec
require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0o755))
require.NoError(t, os.WriteFile(goldenPath, []byte(actual), 0o644)) //nolint:gosec
}

expected, err := os.ReadFile(goldenPath)
Expand All @@ -685,8 +684,8 @@ func TestCheckS3Golden() TestCheck {
goldenPath := getTestFilePath(t, ".golden")
// In order to avoid diff in goldens we set all timestamp to the same date
if *UpdateGoldens {
require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0755))
require.NoError(t, os.WriteFile(goldenPath, []byte(normalizedActual), 0644)) //nolint:gosec
require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0o755))
require.NoError(t, os.WriteFile(goldenPath, []byte(normalizedActual), 0o644)) //nolint:gosec
}

expected, err := os.ReadFile(goldenPath)
Expand Down
3 changes: 1 addition & 2 deletions internal/core/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ type FirstNestedElement struct {
SecondNestedElement *SecondNestedElement
}

type SecondNestedElement struct {
}
type SecondNestedElement struct{}

type elementCustom struct {
*Element
Expand Down
1 change: 0 additions & 1 deletion internal/core/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type View struct {
}

type ViewField struct {

// Label is the string displayed as header or key for a field.
Label string

Expand Down
2 changes: 1 addition & 1 deletion internal/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func GenerateDocs(commands *core.Commands, outDir string) error {
if err != nil {
return err
}
err = os.WriteFile(path.Join(outDir, name+".md"), []byte(namespaceDoc), 0600)
err = os.WriteFile(path.Join(outDir, name+".md"), []byte(namespaceDoc), 0o600)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 44f37c5

Please sign in to comment.