Skip to content

Commit e5b5092

Browse files
authored
Add linting to build (#49)
* Add linting to build
1 parent d0da5c2 commit e5b5092

File tree

16 files changed

+168
-111
lines changed

16 files changed

+168
-111
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
- main
9+
pull_request:
10+
permissions:
11+
contents: read
12+
pull-requests: read
13+
jobs:
14+
golangci:
15+
name: lint
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: golangci-lint
20+
uses: golangci/golangci-lint-action@v2
21+

.golangci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
linters:
3+
disable-all: true
4+
enable:
5+
- deadcode
6+
- errcheck
7+
- gosimple
8+
- govet
9+
- ineffassign
10+
- staticcheck
11+
- structcheck
12+
- typecheck
13+
- unused
14+
- varcheck
15+
- bodyclose
16+
- contextcheck
17+
- durationcheck
18+
- errname
19+
- errorlint
20+
- exportloopref
21+
- goimports
22+
- gosec
23+
- gocritic
24+

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![GoReportCard](https://goreportcard.com/badge/github.com/liamg/peridot)](https://goreportcard.com/report/github.com/liamg/peridot)
2+
![Build Status](https://github.com/liamg/peridot/actions/workflows/build.yml/badge.svg)
3+
14
# peridot
25

36
Developer machine management for Linux/OSX. Think Terraform for your dotfiles.

internal/pkg/builtins/apt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func init() {
3333
}).
3434
WithInstallFunc(func(r *module.Runner, vars variable.Collection) error {
3535
if err := r.Run("apt -qq update", true); err != nil {
36-
return fmt.Errorf("failed to sync package db: %s", err)
36+
return fmt.Errorf("failed to sync package db: %w", err)
3737
}
3838
for _, pkg := range vars.Get("packages").AsList().All() {
3939
if err := r.Run(fmt.Sprintf("apt -qq list %s | grep -q '\\[installed\\]'", pkg.AsString()), false); err != nil {

internal/pkg/builtins/fonts.go

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -32,104 +32,108 @@ func getFontsDir(vars variable.Collection) (string, error) {
3232
return dir, nil
3333
}
3434

35-
func init() {
35+
func fontsRequiresInstall(_ *module.Runner, vars variable.Collection) (bool, error) {
3636

37-
fontsBuiltin := module.NewFactory("fonts").
38-
WithInputs([]config.Variable{
39-
{
40-
Name: "files",
41-
Required: true,
42-
},
43-
{
44-
Name: "dir",
45-
Default: "",
46-
},
47-
}).
48-
WithRequiresInstallFunc(func(_ *module.Runner, vars variable.Collection) (bool, error) {
37+
dir, err := getFontsDir(vars)
38+
if err != nil {
39+
return false, err
40+
}
41+
if err := os.MkdirAll(dir, 0700); err != nil {
42+
return false, err
43+
}
4944

50-
dir, err := getFontsDir(vars)
51-
if err != nil {
52-
return false, err
53-
}
54-
if err := os.MkdirAll(dir, 0700); err != nil {
55-
return false, err
56-
}
45+
for _, file := range vars.Get("files").AsList().All() {
46+
var filename string
47+
if strings.HasPrefix(file.AsString(), "./") {
48+
filename = filepath.Base(file.AsString())
49+
} else if parsed, err := url.Parse(file.AsString()); err == nil && parsed.Host != "" {
50+
filename = path.Base(parsed.Path)
51+
}
5752

58-
for _, file := range vars.Get("files").AsList().All() {
59-
var filename string
60-
if strings.HasPrefix(file.AsString(), "./") {
61-
filename = filepath.Base(file.AsString())
62-
} else if parsed, err := url.Parse(file.AsString()); err == nil && parsed.Host != "" {
63-
filename = path.Base(parsed.Path)
64-
}
53+
expectedPath := filepath.Join(dir, filename)
54+
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
55+
return true, nil
56+
}
57+
}
6558

66-
expectedPath := filepath.Join(dir, filename)
67-
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
68-
return true, nil
69-
}
70-
}
59+
return false, nil
60+
}
7161

72-
return false, nil
73-
}).
74-
WithInstallFunc(func(r *module.Runner, vars variable.Collection) error {
62+
func fontsInstall(r *module.Runner, vars variable.Collection) error {
7563

76-
dir, err := getFontsDir(vars)
64+
dir, err := getFontsDir(vars)
65+
if err != nil {
66+
return err
67+
}
68+
if err := os.MkdirAll(dir, 0700); err != nil {
69+
return err
70+
}
71+
72+
for _, file := range vars.Get("files").AsList().All() {
73+
var filename string
74+
if strings.HasPrefix(file.AsString(), "./") {
75+
filename = filepath.Base(file.AsString())
76+
targetPath := filepath.Join(dir, filename)
77+
if _, err := os.Stat(targetPath); !os.IsNotExist(err) {
78+
continue
79+
}
80+
81+
fontData, err := ioutil.ReadFile(file.AsString())
7782
if err != nil {
78-
return err
83+
return fmt.Errorf("failed to install font from %s: %w", file.AsString(), err)
7984
}
80-
if err := os.MkdirAll(dir, 0700); err != nil {
85+
if err := ioutil.WriteFile(targetPath, fontData, 0600); err != nil {
8186
return err
8287
}
88+
} else if parsed, err := url.Parse(file.AsString()); err == nil && parsed.Host != "" {
89+
filename = path.Base(parsed.Path)
90+
targetPath := filepath.Join(dir, filename)
91+
if _, err := os.Stat(targetPath); !os.IsNotExist(err) {
92+
continue
93+
}
94+
if err := func() error {
95+
resp, err := http.Get(parsed.String())
96+
if err != nil {
97+
return err
98+
}
99+
defer resp.Body.Close()
83100

84-
for _, file := range vars.Get("files").AsList().All() {
85-
var filename string
86-
if strings.HasPrefix(file.AsString(), "./") {
87-
filename = filepath.Base(file.AsString())
88-
targetPath := filepath.Join(dir, filename)
89-
if _, err := os.Stat(targetPath); !os.IsNotExist(err) {
90-
continue
91-
}
92-
93-
fontData, err := ioutil.ReadFile(file.AsString())
94-
if err != nil {
95-
return fmt.Errorf("failed to install font from %s: %s", file.AsString(), err)
96-
}
97-
if err := ioutil.WriteFile(targetPath, fontData, 0600); err != nil {
98-
return err
99-
}
100-
} else if parsed, err := url.Parse(file.AsString()); err == nil && parsed.Host != "" {
101-
filename = path.Base(parsed.Path)
102-
targetPath := filepath.Join(dir, filename)
103-
if _, err := os.Stat(targetPath); !os.IsNotExist(err) {
104-
continue
105-
}
106-
if err := func() error {
107-
resp, err := http.Get(parsed.String())
108-
if err != nil {
109-
return err
110-
}
111-
defer resp.Body.Close()
112-
113-
fontData, err := ioutil.ReadAll(resp.Body)
114-
if err != nil {
115-
return err
116-
}
117-
118-
if err := ioutil.WriteFile(targetPath, fontData, 0600); err != nil {
119-
return err
120-
}
121-
122-
return nil
123-
}(); err != nil {
124-
return err
125-
}
126-
} else {
127-
return fmt.Errorf("invalid font file: '%s'", file.AsString())
101+
fontData, err := ioutil.ReadAll(resp.Body)
102+
if err != nil {
103+
return err
128104
}
105+
106+
if err := ioutil.WriteFile(targetPath, fontData, 0600); err != nil {
107+
return err
108+
}
109+
110+
return nil
111+
}(); err != nil {
112+
return err
129113
}
114+
} else {
115+
return fmt.Errorf("invalid font file: '%s'", file.AsString())
116+
}
117+
}
118+
119+
return r.Run("fc-cache -f", false)
120+
}
121+
122+
func init() {
130123

131-
return r.Run("fc-cache -f", false)
124+
fontsBuiltin := module.NewFactory("fonts").
125+
WithInputs([]config.Variable{
126+
{
127+
Name: "files",
128+
Required: true,
129+
},
130+
{
131+
Name: "dir",
132+
Default: "",
133+
},
132134
}).
135+
WithRequiresInstallFunc(fontsRequiresInstall).
136+
WithInstallFunc(fontsInstall).
133137
Build()
134138

135139
module.RegisterBuiltin("fonts", fontsBuiltin)

internal/pkg/builtins/pacman.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func init() {
2727
}).
2828
WithInstallFunc(func(r *module.Runner, vars variable.Collection) error {
2929
if err := r.Run("pacman -Syy", true); err != nil {
30-
return fmt.Errorf("failed to sync package db: %s", err)
30+
return fmt.Errorf("failed to sync package db: %w", err)
3131
}
3232
for _, pkg := range vars.Get("packages").AsList().All() {
3333
if err := r.Run(fmt.Sprintf("pacman -Qi %s >/dev/null", pkg.AsString()), false); err != nil {

internal/pkg/builtins/yay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func init() {
2727
}).
2828
WithInstallFunc(func(r *module.Runner, vars variable.Collection) error {
2929
if err := r.Run("yay -Syy", false); err != nil {
30-
return fmt.Errorf("failed to sync package db: %s", err)
30+
return fmt.Errorf("failed to sync package db: %w", err)
3131
}
3232
for _, pkg := range vars.Get("packages").AsList().All() {
3333
if err := r.Run(fmt.Sprintf("yay -Qi %s >/dev/null", pkg.AsString()), false); err != nil {

internal/pkg/module/diff.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,24 @@ func Diff(m Module) ([]ModuleDiff, error) {
7878
}
7979

8080
// run scripts.update_required and scripts.install_required to see if update is needed
81-
if m.RequiresInstall() {
81+
switch {
82+
case m.RequiresInstall():
8283
logger.Log("Installation is required!")
8384
moduleDiffs = append(moduleDiffs, &moduleDiff{
8485
module: m,
8586
before: StateUninstalled,
8687
after: StateInstalled,
8788
fileDiffs: fileDiffs,
8889
})
89-
} else if m.RequiresUpdate() {
90+
case m.RequiresUpdate():
9091
logger.Log("Update is required!")
9192
moduleDiffs = append(moduleDiffs, &moduleDiff{
9293
module: m,
9394
before: StateInstalled,
9495
after: StateUpdated,
9596
fileDiffs: fileDiffs,
9697
})
97-
} else if len(fileDiffs) > 0 {
98+
case len(fileDiffs) > 0:
9899
logger.Log("Module has file(s) needing updates!")
99100
moduleDiffs = append(moduleDiffs, &moduleDiff{
100101
module: m,

internal/pkg/module/factory_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ func TestFactorySetsHandlers(t *testing.T) {
4444

4545
built.RequiresInstall()
4646
built.RequiresUpdate()
47-
built.Install()
48-
built.Update()
49-
built.AfterFileChange()
47+
_ = built.Install()
48+
_ = built.Update()
49+
_ = built.AfterFileChange()
5050

5151
assert.True(t, requiresInstall, "RequiresInstall() was not configured")
5252
assert.True(t, requiresUpdate, "RequiresUpdate() was not configured")

internal/pkg/module/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r *Runner) Run(command string, sudo bool) error {
4747
cmd = exec.Command("sh", "-c", command)
4848
output := log.NewPrefixedWriter(r.module.Name(), r.operation)
4949
defer output.Flush()
50-
output.Write([]byte(fmt.Sprintf("Running command: %s\n", command)))
50+
_, _ = output.Write([]byte(fmt.Sprintf("Running command: %s\n", command)))
5151
cmd.Stdout = output
5252
cmd.Stderr = output
5353
}

test/apply_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestApplyCommandWithEmptyConfig(t *testing.T) {
1515
if err != nil {
1616
t.Fatal(err)
1717
}
18-
defer c.Stop()
18+
defer func() { _ = c.Stop() }()
1919

2020
require.NoError(t, c.WriteConfig(``))
2121

@@ -31,7 +31,7 @@ func TestApplyCommandWithInvalidConfig(t *testing.T) {
3131
if err != nil {
3232
t.Fatal(err)
3333
}
34-
defer c.Stop()
34+
defer func() { _ = c.Stop() }()
3535

3636
require.NoError(t, c.WriteConfig(`this is invalid`))
3737

@@ -46,7 +46,7 @@ func TestApplyCommandWithSingleFileChanges(t *testing.T) {
4646
if err != nil {
4747
t.Fatal(err)
4848
}
49-
defer c.Stop()
49+
defer func() { _ = c.Stop() }()
5050

5151
require.NoError(t, c.WriteHomeFile(".config/peridot/lol.tmpl", `hello world`))
5252

@@ -71,7 +71,7 @@ func TestApplyCommandWhenOnlyFileAlreadyMatches(t *testing.T) {
7171
if err != nil {
7272
t.Fatal(err)
7373
}
74-
defer c.Stop()
74+
defer func() { _ = c.Stop() }()
7575

7676
require.NoError(t, c.WriteHomeFile(".config/peridot/lol.tmpl", `hello world`))
7777

@@ -95,7 +95,7 @@ func TestApplyWithSudoRequired(t *testing.T) {
9595
if err != nil {
9696
t.Fatal(err)
9797
}
98-
defer c.Stop()
98+
defer func() { _ = c.Stop() }()
9999

100100
// intall sudo and allow default user to sudo without password
101101
_, exit, err := c.RunAsRoot("sh", "-c", fmt.Sprintf(`apt update && apt install -y sudo && echo '%s ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers`, defaultUser))

0 commit comments

Comments
 (0)