Skip to content

Commit e5a79f1

Browse files
committed
golangci-lint: enable revive
The `revive` linter was failing with the following errors: ``` pkg/osutil/user.go:19:2: var-naming: struct field Uid should be UID (revive) Uid uint32 ^ pkg/osutil/user.go:79:2: var-naming: const fallbackUid should be fallbackUID (revive) fallbackUid = 1000 ^ pkg/osutil/osutil_others.go:16:2: var-naming: struct field Uid should be UID (revive) Uid uint32 ^ cmd/limactl/start.go:178:10: indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive) } else { if !errors.Is(err, os.ErrNotExist) { return nil, err } if arg != "" && arg != DefaultInstanceName { logrus.Infof("Creating an instance %q from template://default (Not from template://%s)", st.instName, st.instName) logrus.Warnf("This form is deprecated. Use `limactl start --name=%s template://default` instead", st.instName) } // Read the default template for creating a new instance st.yBytes, err = readDefaultTemplate() if err != nil { return nil, err } } pkg/hostagent/dns/dns_test.go:70:5: var-naming: don't use underscores in Go names; var regex_match should be regexMatch (revive) regex_match := func(value string, pattern string) cmp.Comparison { ^ pkg/networks/reconcile/reconcile.go:112:51: context-as-argument: context.Context should be the first parameter of a function (revive) func startDaemon(config *networks.NetworksConfig, ctx context.Context, name, daemon string) error { ^ pkg/networks/reconcile/reconcile.go:176:52: context-as-argument: context.Context should be the first parameter of a function (revive) func startNetwork(config *networks.NetworksConfig, ctx context.Context, name string) error { ^ pkg/networks/networks.go:5:6: exported: type name will be used as networks.NetworksConfig by other packages, and that stutters; consider calling this Config (revive) type NetworksConfig struct { ^ pkg/networks/sudoers.go:84:10: indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive) } else { return fmt.Errorf("passwordLessSudo error: %w", err) } pkg/networks/sudoers.go:96:11: indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive) } else { logrus.Debugf("%q does not exist; passwordLessSudo error: %s", sudoersFile, err) } ``` Signed-off-by: Akihiro Suda <[email protected]>
1 parent 5efc3f1 commit e5a79f1

File tree

14 files changed

+66
-66
lines changed

14 files changed

+66
-66
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ linters:
7373
# - stylecheck
7474
# - testpackage
7575
# - tparallel
76+
- revive
7677
# - unconvert
7778
# - unparam
7879
- unused

cmd/limactl/start.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,18 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
177177
logrus.Infof("Hint: To create another instance, run the following command: limactl start --name=NAME template://default")
178178
}
179179
return inst, nil
180-
} else {
181-
if !errors.Is(err, os.ErrNotExist) {
182-
return nil, err
183-
}
184-
if arg != "" && arg != DefaultInstanceName {
185-
logrus.Infof("Creating an instance %q from template://default (Not from template://%s)", st.instName, st.instName)
186-
logrus.Warnf("This form is deprecated. Use `limactl start --name=%s template://default` instead", st.instName)
187-
}
188-
// Read the default template for creating a new instance
189-
st.yBytes, err = readDefaultTemplate()
190-
if err != nil {
191-
return nil, err
192-
}
180+
}
181+
if !errors.Is(err, os.ErrNotExist) {
182+
return nil, err
183+
}
184+
if arg != "" && arg != DefaultInstanceName {
185+
logrus.Infof("Creating an instance %q from template://default (Not from template://%s)", st.instName, st.instName)
186+
logrus.Warnf("This form is deprecated. Use `limactl start --name=%s template://default` instead", st.instName)
187+
}
188+
// Read the default template for creating a new instance
189+
st.yBytes, err = readDefaultTemplate()
190+
if err != nil {
191+
return nil, err
193192
}
194193
}
195194

pkg/cidata/cidata.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
240240
}
241241
}
242242

243-
if guestAgentBinary, err := GuestAgentBinary(*y.Arch); err != nil {
243+
guestAgentBinary, err := GuestAgentBinary(*y.Arch)
244+
if err != nil {
244245
return err
245-
} else {
246-
defer guestAgentBinary.Close()
247-
layout = append(layout, iso9660util.Entry{
248-
Path: "lima-guestagent",
249-
Reader: guestAgentBinary,
250-
})
251246
}
247+
defer guestAgentBinary.Close()
248+
layout = append(layout, iso9660util.Entry{
249+
Path: "lima-guestagent",
250+
Reader: guestAgentBinary,
251+
})
252252

253253
if nerdctlArchive != "" {
254254
nftgzR, err := os.Open(nerdctlArchive)

pkg/hostagent/dns/dns_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestTXTRecords(t *testing.T) {
6767
req := new(dns.Msg)
6868
req.SetQuestion(dns.Fqdn(testDomains[i]), dns.TypeTXT)
6969
h.ServeDNS(w, req)
70-
regex_match := func(value string, pattern string) cmp.Comparison {
70+
regexMatch := func(value string, pattern string) cmp.Comparison {
7171
return func() cmp.Result {
7272
re := regexp.MustCompile(pattern)
7373
if re.MatchString(value) {
@@ -77,7 +77,7 @@ func TestTXTRecords(t *testing.T) {
7777
fmt.Sprintf("%q did not match pattern %q", value, pattern))
7878
}
7979
}
80-
assert.Assert(t, regex_match(dnsResult.String(), expectedResults[i]))
80+
assert.Assert(t, regexMatch(dnsResult.String(), expectedResults[i]))
8181
}
8282
}
8383
})

pkg/networks/commands.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ const (
2121
// Commands in `sudoers` cannot use quotes, so all arguments are printed via "%s"
2222
// and not "%q". config.Paths.* entries must not include any whitespace!
2323

24-
func (config *NetworksConfig) Check(name string) error {
24+
func (config *YAML) Check(name string) error {
2525
if _, ok := config.Networks[name]; ok {
2626
return nil
2727
}
2828
return fmt.Errorf("network %q is not defined", name)
2929
}
3030

3131
// DaemonPath returns the daemon path.
32-
func (config *NetworksConfig) DaemonPath(daemon string) (string, error) {
32+
func (config *YAML) DaemonPath(daemon string) (string, error) {
3333
switch daemon {
3434
case VDESwitch:
3535
return config.Paths.VDESwitch, nil
@@ -43,7 +43,7 @@ func (config *NetworksConfig) DaemonPath(daemon string) (string, error) {
4343
}
4444

4545
// IsDaemonInstalled checks whether the daemon is installed.
46-
func (config *NetworksConfig) IsDaemonInstalled(daemon string) (bool, error) {
46+
func (config *YAML) IsDaemonInstalled(daemon string) (bool, error) {
4747
p, err := config.DaemonPath(daemon)
4848
if err != nil {
4949
return false, err
@@ -61,29 +61,29 @@ func (config *NetworksConfig) IsDaemonInstalled(daemon string) (bool, error) {
6161
}
6262

6363
// Sock returns a socket_vmnet socket.
64-
func (config *NetworksConfig) Sock(name string) string {
64+
func (config *YAML) Sock(name string) string {
6565
return filepath.Join(config.Paths.VarRun, fmt.Sprintf("socket_vmnet.%s", name))
6666
}
6767

6868
// VDESock returns a vde socket.
6969
//
7070
// Deprecated. Use Sock.
71-
func (config *NetworksConfig) VDESock(name string) string {
71+
func (config *YAML) VDESock(name string) string {
7272
return filepath.Join(config.Paths.VarRun, fmt.Sprintf("%s.ctl", name))
7373
}
7474

75-
func (config *NetworksConfig) PIDFile(name, daemon string) string {
75+
func (config *YAML) PIDFile(name, daemon string) string {
7676
daemonTrimmed := strings.TrimPrefix(daemon, "vde_") // for compatibility
7777
return filepath.Join(config.Paths.VarRun, fmt.Sprintf("%s_%s.pid", name, daemonTrimmed))
7878
}
7979

80-
func (config *NetworksConfig) LogFile(name, daemon, stream string) string {
80+
func (config *YAML) LogFile(name, daemon, stream string) string {
8181
networksDir, _ := dirnames.LimaNetworksDir()
8282
daemonTrimmed := strings.TrimPrefix(daemon, "vde_") // for compatibility
8383
return filepath.Join(networksDir, fmt.Sprintf("%s_%s.%s.log", name, daemonTrimmed, stream))
8484
}
8585

86-
func (config *NetworksConfig) User(daemon string) (osutil.User, error) {
86+
func (config *YAML) User(daemon string) (osutil.User, error) {
8787
if ok, _ := config.IsDaemonInstalled(daemon); !ok {
8888
daemonPath, _ := config.DaemonPath(daemon)
8989
return osutil.User{}, fmt.Errorf("daemon %q (path=%q) is not available", daemon, daemonPath)
@@ -104,11 +104,11 @@ func (config *NetworksConfig) User(daemon string) (osutil.User, error) {
104104
return osutil.User{}, fmt.Errorf("daemon %q not defined", daemon)
105105
}
106106

107-
func (config *NetworksConfig) MkdirCmd() string {
107+
func (config *YAML) MkdirCmd() string {
108108
return fmt.Sprintf("/bin/mkdir -m 775 -p %s", config.Paths.VarRun)
109109
}
110110

111-
func (config *NetworksConfig) StartCmd(name, daemon string) string {
111+
func (config *YAML) StartCmd(name, daemon string) string {
112112
if ok, _ := config.IsDaemonInstalled(daemon); !ok {
113113
panic(fmt.Errorf("daemon %q is not available", daemon))
114114
}
@@ -154,6 +154,6 @@ func (config *NetworksConfig) StartCmd(name, daemon string) string {
154154
return cmd
155155
}
156156

157-
func (config *NetworksConfig) StopCmd(name, daemon string) string {
157+
func (config *YAML) StopCmd(name, daemon string) string {
158158
return fmt.Sprintf("/usr/bin/pkill -F %s", config.PIDFile(name, daemon))
159159
}

pkg/networks/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
//go:embed networks.yaml
1818
var defaultConfig []byte
1919

20-
func DefaultConfig() (NetworksConfig, error) {
21-
var config NetworksConfig
20+
func DefaultConfig() (YAML, error) {
21+
var config YAML
2222
err := yaml.UnmarshalWithOptions(defaultConfig, &config, yaml.Strict())
2323
return config, err
2424
}
2525

2626
var cache struct {
2727
sync.Once
28-
config NetworksConfig
28+
config YAML
2929
err error
3030
}
3131

@@ -74,9 +74,9 @@ func loadCache() {
7474
}
7575

7676
// Config returns the network config from the _config/networks.yaml file.
77-
func Config() (NetworksConfig, error) {
77+
func Config() (YAML, error) {
7878
if runtime.GOOS != "darwin" {
79-
return NetworksConfig{}, errors.New("networks.yaml configuration is only supported on macOS right now")
79+
return YAML{}, errors.New("networks.yaml configuration is only supported on macOS right now")
8080
}
8181
loadCache()
8282
return cache.config, cache.err

pkg/networks/networks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package networks
22

33
import "net"
44

5-
type NetworksConfig struct {
5+
type YAML struct {
66
Paths Paths `yaml:"paths"`
77
Group string `yaml:"group,omitempty"` // default: "everyone"
88
Networks map[string]Network `yaml:"networks"`

pkg/networks/reconcile/reconcile.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Reconcile(ctx context.Context, newInst string) error {
5454
for name := range config.Networks {
5555
var err error
5656
if activeNetwork[name] {
57-
err = startNetwork(&config, ctx, name)
57+
err = startNetwork(ctx, &config, name)
5858
} else {
5959
err = stopNetwork(&config, name)
6060
}
@@ -80,7 +80,7 @@ func sudo(user, group, command string) error {
8080
return nil
8181
}
8282

83-
func makeVarRun(config *networks.NetworksConfig) error {
83+
func makeVarRun(config *networks.YAML) error {
8484
err := sudo("root", "wheel", config.MkdirCmd())
8585
if err != nil {
8686
return err
@@ -109,7 +109,7 @@ func makeVarRun(config *networks.NetworksConfig) error {
109109
return nil
110110
}
111111

112-
func startDaemon(config *networks.NetworksConfig, ctx context.Context, name, daemon string) error {
112+
func startDaemon(ctx context.Context, config *networks.YAML, name, daemon string) error {
113113
if err := makeVarRun(config); err != nil {
114114
return err
115115
}
@@ -162,7 +162,7 @@ var validation struct {
162162
err error
163163
}
164164

165-
func validateConfig(config *networks.NetworksConfig) error {
165+
func validateConfig(config *networks.YAML) error {
166166
validation.Do(func() {
167167
// make sure all config.Paths.* are secure
168168
validation.err = config.Validate()
@@ -173,7 +173,7 @@ func validateConfig(config *networks.NetworksConfig) error {
173173
return validation.err
174174
}
175175

176-
func startNetwork(config *networks.NetworksConfig, ctx context.Context, name string) error {
176+
func startNetwork(ctx context.Context, config *networks.YAML, name string) error {
177177
logrus.Debugf("Make sure %q network is running", name)
178178
if err := validateConfig(config); err != nil {
179179
return err
@@ -195,15 +195,15 @@ func startNetwork(config *networks.NetworksConfig, ctx context.Context, name str
195195
pid, _ := store.ReadPIDFile(config.PIDFile(name, daemon))
196196
if pid == 0 {
197197
logrus.Infof("Starting %s daemon for %q network", daemon, name)
198-
if err := startDaemon(config, ctx, name, daemon); err != nil {
198+
if err := startDaemon(ctx, config, name, daemon); err != nil {
199199
return err
200200
}
201201
}
202202
}
203203
return nil
204204
}
205205

206-
func stopNetwork(config *networks.NetworksConfig, name string) error {
206+
func stopNetwork(config *networks.YAML, name string) error {
207207
logrus.Debugf("Make sure %q network is stopped", name)
208208
// Don't call validateConfig() until we actually need to stop a daemon because
209209
// stopNetwork() may be called even when the vde daemons are not installed.

pkg/networks/sudoers.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Sudoers() (string, error) {
5050
return sb.String(), nil
5151
}
5252

53-
func (config *NetworksConfig) passwordLessSudo() error {
53+
func (config *YAML) passwordLessSudo() error {
5454
// Flush cached sudo password
5555
cmd := exec.Command("sudo", "-k")
5656
if err := cmd.Run(); err != nil {
@@ -76,26 +76,26 @@ func (config *NetworksConfig) passwordLessSudo() error {
7676
return nil
7777
}
7878

79-
func (config *NetworksConfig) VerifySudoAccess(sudoersFile string) error {
79+
func (config *YAML) VerifySudoAccess(sudoersFile string) error {
8080
if sudoersFile == "" {
81-
if err := config.passwordLessSudo(); err == nil {
81+
err := config.passwordLessSudo()
82+
if err == nil {
8283
logrus.Debug("sudo doesn't seem to require a password")
8384
return nil
84-
} else {
85-
return fmt.Errorf("passwordLessSudo error: %w", err)
8685
}
86+
return fmt.Errorf("passwordLessSudo error: %w", err)
8787
}
8888
b, err := os.ReadFile(sudoersFile)
8989
if err != nil {
9090
// Default networks.yaml specifies /etc/sudoers.d/lima file. Don't throw an error when the
9191
// file doesn't exist, as long as password-less sudo still works.
9292
if errors.Is(err, os.ErrNotExist) {
93-
if err := config.passwordLessSudo(); err == nil {
93+
err = config.passwordLessSudo()
94+
if err == nil {
9495
logrus.Debugf("%q does not exist, but sudo doesn't seem to require a password", sudoersFile)
9596
return nil
96-
} else {
97-
logrus.Debugf("%q does not exist; passwordLessSudo error: %s", sudoersFile, err)
9897
}
98+
logrus.Debugf("%q does not exist; passwordLessSudo error: %s", sudoersFile, err)
9999
}
100100
return fmt.Errorf("can't read %q: %s", sudoersFile, err)
101101
}

pkg/networks/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/lima-vm/lima/pkg/osutil"
1313
)
1414

15-
func (config *NetworksConfig) Validate() error {
15+
func (config *YAML) Validate() error {
1616
// validate all paths.* values
1717
paths := reflect.ValueOf(&config.Paths).Elem()
1818
pathsMap := make(map[string]string, paths.NumField())

0 commit comments

Comments
 (0)