Skip to content

Commit b030178

Browse files
committed
Refactoriszation + fix constructors list
1 parent a02c2fc commit b030178

File tree

11 files changed

+115
-1329
lines changed

11 files changed

+115
-1329
lines changed

cmd/goreorder/commands.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
logger "github.com/metal3d/goreorder/log"
13-
"github.com/metal3d/goreorder/ordering"
1412
"github.com/spf13/cobra"
1513
"github.com/spf13/viper"
14+
15+
logger "github.com/metal3d/goreorder/log"
16+
"github.com/metal3d/goreorder/ordering"
1617
)
1718

1819
var (
@@ -89,7 +90,7 @@ to write to the file, use the -write flag.`
8990
return initializeViper(cmd, args...)
9091
},
9192
RunE: func(cmd *cobra.Command, args []string) error {
92-
return fmt.Errorf("You need to specify a command or an option")
93+
return fmt.Errorf("you need to specify a command or an option")
9394
},
9495
}
9596

@@ -129,10 +130,9 @@ func buildReorderCommand(config *ReorderConfig) *cobra.Command {
129130
Use: "reorder [flags] [file.go|directory|stdin]",
130131
Short: "Reorder vars, consts, stucts/types/interaces, methods/functions and constructors in a Go source file.",
131132
RunE: func(cmd *cobra.Command, args []string) error {
132-
133133
stat, _ := os.Stdin.Stat()
134134
if len(args) == 0 && (stat.Mode()&os.ModeCharDevice) != 0 {
135-
return errors.New("You should provide a file or a directory or stream content to stdin.")
135+
return errors.New("you should provide a file or a directory or stream content to stdin")
136136
}
137137

138138
// validate order flags
@@ -148,13 +148,13 @@ func buildReorderCommand(config *ReorderConfig) *cobra.Command {
148148
}
149149
}
150150
if !found {
151-
return fmt.Errorf("Invalid order name %v, valid order name are %v", v, ordering.DefaultOrder)
151+
return fmt.Errorf("invalid order name %v, valid order name are %v", v, ordering.DefaultOrder)
152152
}
153153
}
154154
}
155155
// only allow gofmt or goimports
156156
if config.FormatToolName != "gofmt" && config.FormatToolName != "goimports" {
157-
return fmt.Errorf("Only gofmt or goimports are allowed")
157+
return fmt.Errorf("only gofmt or goimports are allowed")
158158
}
159159

160160
// check if the executable exists

cmd/goreorder/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) {
4646
cmd.Flags().VisitAll(func(f *pflag.Flag) {
4747
name := f.Name
4848
if !f.Changed && v.IsSet(name) {
49-
val := v.Get(name)
5049
// ensure that the value is with the correct type
5150
switch f.Value.Type() {
5251
case "stringSlice":
5352
cmd.Flags().Lookup(name).Value.Set(strings.Join(v.GetStringSlice(name), ","))
5453
default:
55-
val = v.GetString(name)
54+
val := v.GetString(name)
5655
cmd.Flags().Set(name, fmt.Sprintf("%v", val))
5756
}
5857
}

cmd/goreorder/config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package main
22

33
import (
44
"bytes"
5-
"io/ioutil"
65
"os"
76
"testing"
87

9-
"github.com/metal3d/goreorder/ordering"
108
"gopkg.in/yaml.v3"
9+
10+
"github.com/metal3d/goreorder/ordering"
1111
)
1212

1313
func init() {
@@ -79,13 +79,13 @@ order:
7979
t.Fatal(err)
8080
}
8181
defer os.Chdir(currentDir)
82-
tmpDir, err := ioutil.TempDir("", "goreorder-test")
82+
tmpDir, err := os.MkdirTemp("", "goreorder-test")
8383
if err != nil {
8484
t.Fatal(err)
8585
}
8686
defer os.RemoveAll(tmpDir)
8787
os.Chdir(tmpDir)
88-
err = ioutil.WriteFile(".goreorder", []byte(yamlFile), 0644)
88+
err = os.WriteFile(".goreorder", []byte(yamlFile), 0644)
8989
if err != nil {
9090
t.Fatal(err)
9191
}

cmd/goreorder/main.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"strings"
@@ -27,11 +26,11 @@ func main() {
2726
// ReorderConfig is the configuration for the reorder command
2827
type ReorderConfig struct {
2928
FormatToolName string `yaml:"format"`
29+
DefOrder []string `yaml:"order"`
3030
Write bool `yaml:"write"`
3131
Verbose bool `yaml:"verbose"`
3232
ReorderTypes bool `yaml:"reorder-types"`
3333
MakeDiff bool `yaml:"diff"`
34-
DefOrder []string `yaml:"order"`
3534
}
3635

3736
func reorder(config *ReorderConfig, args ...string) error {
@@ -43,9 +42,9 @@ func reorder(config *ReorderConfig, args ...string) error {
4342
stat, _ := os.Stdin.Stat()
4443
if (stat.Mode() & os.ModeCharDevice) == 0 {
4544
// read from stdin
46-
input, err = ioutil.ReadAll(os.Stdin)
45+
input, err = io.ReadAll(os.Stdin)
4746
if err != nil {
48-
return fmt.Errorf("Error while reading stdin: %w", err)
47+
return fmt.Errorf("error while reading stdin: %w", err)
4948
}
5049
filename = "stdin.go"
5150
config.Write = false
@@ -54,11 +53,11 @@ func reorder(config *ReorderConfig, args ...string) error {
5453
// read from file or directory
5554
filename = args[0]
5655
if filename == "" {
57-
return fmt.Errorf("Filename is empty")
56+
return fmt.Errorf("filename is empty")
5857
}
5958
_, err := os.Stat(filename)
6059
if err != nil {
61-
return fmt.Errorf("Error while getting file stat: %w", err)
60+
return fmt.Errorf("error while getting file stat: %w", err)
6261
}
6362
}
6463

@@ -70,7 +69,7 @@ func processFile(fileOrDirectoryName string, input []byte, config *ReorderConfig
7069
return fmt.Errorf("Skipping test file: " + fileOrDirectoryName)
7170
}
7271

73-
if input != nil && len(input) != 0 {
72+
if len(input) != 0 {
7473
// process stdin
7574
content, err := ordering.ReorderSource(ordering.ReorderConfig{
7675
Filename: fileOrDirectoryName,
@@ -80,26 +79,26 @@ func processFile(fileOrDirectoryName string, input []byte, config *ReorderConfig
8079
Src: input,
8180
})
8281
if err != nil {
83-
return fmt.Errorf("Error while reordering source: %w", err)
82+
return fmt.Errorf("error while reordering source: %w", err)
8483
}
8584
fmt.Print(string(content))
8685
return nil
8786
}
8887

8988
stat, err := os.Stat(fileOrDirectoryName)
9089
if err != nil {
91-
return fmt.Errorf("Error while getting file stat: %w", err)
90+
return fmt.Errorf("error while getting file stat: %w", err)
9291
}
9392
if stat.IsDir() {
9493
// skip vendor directory
9594
if strings.HasSuffix(fileOrDirectoryName, "vendor") {
96-
return fmt.Errorf("Skipping vendor directory: " + fileOrDirectoryName)
95+
return fmt.Errorf("skipping vendor directory: " + fileOrDirectoryName)
9796
}
9897
// get all files in directory and process them
9998
log.Println("Processing directory: " + fileOrDirectoryName)
10099
return filepath.Walk(fileOrDirectoryName, func(path string, info os.FileInfo, err error) error {
101100
if err != nil {
102-
return fmt.Errorf("Error while walking directory: %w", err)
101+
return fmt.Errorf("error while walking directory: %w", err)
103102
}
104103
if strings.HasSuffix(path, ".go") {
105104
processFile(path, nil, config)
@@ -118,15 +117,14 @@ func processFile(fileOrDirectoryName string, input []byte, config *ReorderConfig
118117
Src: input,
119118
})
120119
if err != nil {
121-
return fmt.Errorf("Error while reordering file: %w", err)
120+
return fmt.Errorf("error while reordering file: %w", err)
122121
}
123122
if config.Write {
124-
err = ioutil.WriteFile(fileOrDirectoryName, []byte(output), 0644)
123+
err = os.WriteFile(fileOrDirectoryName, []byte(output), 0644)
125124
if err != nil {
126-
return fmt.Errorf("Error while writing to file: %w", err)
125+
return fmt.Errorf("error while writing to file: %w", err)
127126
}
128127
} else {
129-
//fmt.Println(output)
130128
io.Copy(defaultOutpout, bytes.NewBufferString(output))
131129
}
132130
return nil

cmd/goreorder/main_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"bytes"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"testing"
@@ -45,7 +44,7 @@ func main() {}`)
4544
cmd := buildMainCommand()
4645
cmd.SetArgs([]string{"reorder", "--write", "./main.go"})
4746
if err := cmd.Execute(); err != nil {
48-
t.Error("Command error", err)
47+
t.Error("command error", err)
4948
}
5049

5150
// check file
@@ -68,7 +67,7 @@ func TestWithDir(t *testing.T) {
6867
t.Fatal(err)
6968
}
7069
defer os.Chdir(currentDir)
71-
tmpDir, err := ioutil.TempDir("", "goreorder-test")
70+
tmpDir, err := os.MkdirTemp("", "goreorder-test")
7271
if err != nil {
7372
t.Fatal(err)
7473
}
@@ -118,7 +117,7 @@ func TestHelp(t *testing.T) {
118117
cmd := buildMainCommand()
119118
cmd.SetArgs([]string{"--help"})
120119
if err := cmd.Execute(); err != nil {
121-
t.Error("Command error", err)
120+
t.Error("command error", err)
122121
}
123122

124123
}
@@ -128,7 +127,7 @@ func TestVersion(t *testing.T) {
128127
cmd := buildMainCommand()
129128
cmd.SetArgs([]string{"--version"})
130129
if err := cmd.Execute(); err != nil {
131-
t.Error("Version Command error", err)
130+
t.Error("version Command error", err)
132131
}
133132

134133
}
@@ -138,7 +137,7 @@ func TestNoArgs(t *testing.T) {
138137
// no arguments
139138
cmd.SetArgs([]string{})
140139
if err := cmd.Execute(); err == nil {
141-
t.Error("An error should occur with no argument", err)
140+
t.Error("an error should occur with no argument", err)
142141
}
143142
}
144143

@@ -147,28 +146,28 @@ func TestCompletionCommands(t *testing.T) {
147146
cmd := buildMainCommand()
148147
cmd.SetArgs([]string{"completion", shell})
149148
if err := cmd.Execute(); err != nil {
150-
t.Error("Command error", err)
149+
t.Error("command error", err)
151150
}
152151
}
153152

154153
// try bash with --bashv1
155154
cmd := buildMainCommand()
156155
cmd.SetArgs([]string{"completion", "bash", "--bashv1"})
157156
if err := cmd.Execute(); err != nil {
158-
t.Error("Command error", err)
157+
t.Error("command error", err)
159158
}
160159

161160
// with no shell
162161
cmd = buildMainCommand()
163162
cmd.SetArgs([]string{"completion"})
164163
if err := cmd.Execute(); err == nil {
165-
t.Error("An error should occur with no shell argument", err)
164+
t.Error("an error should occur with no shell argument", err)
166165
}
167166

168167
// and with a bad shell
169168
cmd = buildMainCommand()
170169
cmd.SetArgs([]string{"completion", "badshell"})
171170
if err := cmd.Execute(); err == nil {
172-
t.Error("An error should occur with a bad shell argument", err)
171+
t.Error("an error should occur with a bad shell argument", err)
173172
}
174173
}

go.mod

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
module github.com/metal3d/goreorder
22

3-
go 1.16
3+
go 1.22
44

55
require (
6-
github.com/spf13/cobra v1.7.0
6+
github.com/spf13/cobra v1.8.0
77
github.com/spf13/pflag v1.0.5
8-
github.com/spf13/viper v1.16.0
9-
golang.org/x/sys v0.9.0 // indirect
10-
golang.org/x/text v0.10.0 // indirect
8+
github.com/spf13/viper v1.18.2
119
gopkg.in/yaml.v3 v3.0.1
1210
)
11+
12+
require (
13+
github.com/fsnotify/fsnotify v1.7.0 // indirect
14+
github.com/hashicorp/hcl v1.0.0 // indirect
15+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
16+
github.com/magiconair/properties v1.8.7 // indirect
17+
github.com/mitchellh/mapstructure v1.5.0 // indirect
18+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
19+
github.com/sagikazarmark/locafero v0.4.0 // indirect
20+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
21+
github.com/sourcegraph/conc v0.3.0 // indirect
22+
github.com/spf13/afero v1.11.0 // indirect
23+
github.com/spf13/cast v1.6.0 // indirect
24+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
25+
github.com/subosito/gotenv v1.6.0 // indirect
26+
go.uber.org/atomic v1.11.0 // indirect
27+
go.uber.org/multierr v1.11.0 // indirect
28+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
29+
golang.org/x/sys v0.20.0 // indirect
30+
golang.org/x/text v0.15.0 // indirect
31+
gopkg.in/ini.v1 v1.67.0 // indirect
32+
)

0 commit comments

Comments
 (0)