Skip to content

Commit b382f74

Browse files
authored
Merge pull request #77 from nlewo/linting
github: add linting CI step
2 parents bca9d37 + 2a7483d commit b382f74

25 files changed

+109
-117
lines changed

.github/workflows/golangci-lint.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
golangci:
13+
name: lint
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-go@v5
18+
with:
19+
go-version: stable
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v7
22+
with:
23+
version: v2.0

cmd/fetcher.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var fetchCmd = &cobra.Command{
2020
if err != nil {
2121
return
2222
}
23-
client.Do(req)
23+
_, _ = client.Do(req)
2424
},
2525
}
2626

cmd/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ var runCmd = &cobra.Command{
8888

8989
func init() {
9090
runCmd.PersistentFlags().StringVarP(&configFilepath, "config", "", "", "the configuration file path")
91-
runCmd.MarkPersistentFlagRequired("config")
91+
_ = runCmd.MarkPersistentFlagRequired("config")
9292
rootCmd.AddCommand(runCmd)
9393
}

cmd/status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func getStatus() (status manager.State, err error) {
3232
return
3333
}
3434
if res.Body != nil {
35-
defer res.Body.Close()
35+
defer res.Body.Close() // nolint
3636
}
3737
body, err := io.ReadAll(res.Body)
3838
if err != nil {

flake.lock

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
description = "Comin - GitOps for NixOS Machines";
33

4-
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
4+
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
55

66
outputs = { self, nixpkgs }:
77
let
@@ -33,6 +33,7 @@
3333
in pkgs.mkShell {
3434
buildInputs = [
3535
pkgs.go pkgs.godef pkgs.gopls
36+
pkgs.golangci-lint
3637
];
3738
};
3839
};

internal/builder/builder.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ func (b *Builder) Build() error {
192192
defer b.mu.Unlock()
193193

194194
if b.generation == nil || b.generation.EvalStatus != Evaluated {
195-
return fmt.Errorf("The generation is not evaluated")
195+
return fmt.Errorf("the generation is not evaluated")
196196
}
197197
if b.IsBuilding {
198-
return fmt.Errorf("The builder is already building")
198+
return fmt.Errorf("the builder is already building")
199199
}
200200
if b.generation.BuildStatus == Built {
201-
return fmt.Errorf("The generation is already built")
201+
return fmt.Errorf("the generation is already built")
202202
}
203203
b.generation.BuildStartedAt = time.Now().UTC()
204204
b.generation.BuildStatus = Building

internal/builder/builder_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestBuilderBuild(t *testing.T) {
4646

4747
b := New("", "", "my-machine", 2*time.Second, mkNixEvalMock(evalDone), 2*time.Second, mkNixBuildMock(buildDone))
4848

49-
assert.ErrorContains(t, b.Build(), "The generation is not evaluated")
49+
assert.ErrorContains(t, b.Build(), "the generation is not evaluated")
5050
// Run the evaluator
5151
b.Eval(repository.RepositoryStatus{})
5252
close(evalDone)
@@ -55,12 +55,12 @@ func TestBuilderBuild(t *testing.T) {
5555
assert.False(c, b.IsEvaluating)
5656
}, 2*time.Second, 100*time.Millisecond)
5757

58-
b.Build()
58+
_ = b.Build()
5959
assert.EventuallyWithT(t, func(c *assert.CollectT) {
6060
assert.True(c, b.IsBuilding)
6161
}, 2*time.Second, 100*time.Millisecond)
6262
err := b.Build()
63-
assert.ErrorContains(t, err, "The builder is already building")
63+
assert.ErrorContains(t, err, "the builder is already building")
6464

6565
// Stop the evaluator and builder
6666
b.Stop()
@@ -89,7 +89,7 @@ func TestBuilderBuild(t *testing.T) {
8989

9090
// The generation is already built
9191
err = b.Build()
92-
assert.ErrorContains(t, err, "The generation is already built")
92+
assert.ErrorContains(t, err, "the generation is already built")
9393
}
9494

9595
func TestEval(t *testing.T) {

internal/builder/generation.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ func GenerationShow(g Generation) {
103103
fmt.Printf("%sEvaluation started %s\n", padding, humanize.Time(g.EvalStartedAt))
104104
return
105105
}
106-
if g.EvalStatus == Evaluated {
106+
switch g.EvalStatus {
107+
case Evaluated:
107108
fmt.Printf("%sEvaluation succedded %s\n", padding, humanize.Time(g.EvalEndedAt))
108109
fmt.Printf("%s DrvPath: %s\n", padding, g.DrvPath)
109-
} else if g.EvalStatus == EvalFailed {
110+
case EvalFailed:
110111
fmt.Printf("%sEvaluation failed %s\n", padding, humanize.Time(g.EvalEndedAt))
111112
}
112-
113113
if g.BuildStatus == BuildInit {
114114
fmt.Printf("%sNo build started\n", padding)
115115
return
@@ -118,10 +118,11 @@ func GenerationShow(g Generation) {
118118
fmt.Printf("%sBuild started %s\n", padding, humanize.Time(g.BuildStartedAt))
119119
return
120120
}
121-
if g.BuildStatus == Built {
121+
switch g.BuildStatus {
122+
case Built:
122123
fmt.Printf("%sBuilt %s\n", padding, humanize.Time(g.BuildEndedAt))
123124
fmt.Printf("%s Outpath: %s\n", padding, g.OutPath)
124-
} else if g.BuildStatus == BuildFailed {
125+
case BuildFailed:
125126
fmt.Printf("%sBuild failed %s\n", padding, humanize.Time(g.BuildEndedAt))
126127
}
127128
}

internal/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Read(path string) (config types.Configuration, err error) {
1515
if err != nil {
1616
return
1717
}
18-
defer file.Close()
18+
defer file.Close() // nolint
1919

2020
d := yaml.NewDecoder(file)
2121
if err := d.Decode(&config); err != nil {

internal/deployer/deployer.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ type Deployer struct {
6161
previousDeployment *Deployment
6262
IsDeploying bool
6363
// The next generation to deploy. nil when there is no new generation to deploy
64-
GenerationToDeploy *builder.Generation
65-
// Is there a generation which
66-
generationAvailable bool
64+
GenerationToDeploy *builder.Generation
6765
generationAvailableCh chan struct{}
6866
}
6967

@@ -190,9 +188,7 @@ func (d *Deployer) Run() {
190188
}
191189
d.Deployment.RestartComin = cominNeedRestart
192190
d.Deployment.ProfilePath = profilePath
193-
select {
194-
case d.DeploymentDoneCh <- *d.Deployment:
195-
}
191+
d.DeploymentDoneCh <- *d.Deployment
196192
d.mu.Unlock()
197193
}
198194
}()

internal/executor/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func runNixCommand(args []string, stdout, stderr io.Writer) (err error) {
5656
cmd.Stderr = stderr
5757
err = cmd.Run()
5858
if err != nil {
59-
return fmt.Errorf("Command '%s' fails with %s", cmdStr, err)
59+
return fmt.Errorf("command '%s' fails with %s", cmdStr, err)
6060
}
6161
return nil
6262
}
@@ -130,7 +130,7 @@ func switchToConfiguration(operation string, outPath string, dryRun bool) error
130130
logrus.Infof("nix: dry-run enabled: '%s switch' has not been executed", switchToConfigurationExe)
131131
} else {
132132
if err := cmd.Run(); err != nil {
133-
return fmt.Errorf("Command %s switch fails with %s", switchToConfigurationExe, err)
133+
return fmt.Errorf("command %s switch fails with %s", switchToConfigurationExe, err)
134134
}
135135
logrus.Infof("nix: switch successfully terminated")
136136
}

internal/http/http.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ func handlerStatus(m *manager.Manager, w http.ResponseWriter, r *http.Request) {
2121
if err != nil {
2222
logrus.Error(err)
2323
}
24-
io.WriteString(w, string(rJson))
25-
return
24+
_, _ = io.Writer.Write(w, rJson)
2625
}
2726

2827
// Serve starts http servers. We create two HTTP servers to easily be
@@ -31,13 +30,12 @@ func handlerStatus(m *manager.Manager, w http.ResponseWriter, r *http.Request) {
3130
func Serve(m *manager.Manager, p prometheus.Prometheus, apiAddress string, apiPort int, metricsAddress string, metricsPort int) {
3231
handlerStatusFn := func(w http.ResponseWriter, r *http.Request) {
3332
handlerStatus(m, w, r)
34-
return
3533
}
3634
handlerFetcherFn := func(w http.ResponseWriter, r *http.Request) {
3735
w.WriteHeader(http.StatusOK)
3836
s := m.GetState().Fetcher
3937
rJson, _ := json.MarshalIndent(s, "", "\t")
40-
io.WriteString(w, string(rJson))
38+
_, _ = io.Writer.Write(w, rJson)
4139
}
4240
handlerFetcherFetchFn := func(w http.ResponseWriter, r *http.Request) {
4341
switch r.Method {

internal/manager/manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (m *Manager) FetchAndBuild() {
9191
logrus.Infof("manager: the comin.machineId %s is not the host machine-id %s", generation.MachineId, m.machineId)
9292
} else {
9393
logrus.Infof("manager: a generation is building for commit %s", generation.SelectedCommitId)
94-
m.builder.Build()
94+
_ = m.builder.Build()
9595
}
9696
case generation := <-m.builder.BuildDone:
9797
if generation.BuildErr == nil {
@@ -120,7 +120,7 @@ func (m *Manager) Run() {
120120
m.prometheus.SetDeploymentInfo(dpl.Generation.SelectedCommitId, deployer.StatusToString(dpl.Status))
121121
getsEvicted, evicted := m.storage.DeploymentInsertAndCommit(dpl)
122122
if getsEvicted && evicted.ProfilePath != "" {
123-
profile.RemoveProfilePath(evicted.ProfilePath)
123+
_ = profile.RemoveProfilePath(evicted.ProfilePath)
124124
}
125125
m.needToReboot = utils.NeedToReboot()
126126
m.prometheus.SetHostInfo(m.needToReboot)

internal/manager/manager_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
)
2020

21-
type metricsMock struct{}
22-
23-
func (m metricsMock) SetDeploymentInfo(commitId, status string) {}
24-
2521
var mkNixEvalMock = func(evalOk chan bool) builder.EvalFunc {
2622
return func(ctx context.Context, repositoryPath string, hostname string) (string, string, string, error) {
2723
ok := <-evalOk

internal/profile/profile_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
func TestRemoveProfilePath(t *testing.T) {
1313
dir := t.TempDir()
1414
file1 := path.Join(dir, "file1")
15-
os.Create(file1)
15+
_, _ = os.Create(file1)
1616
file2 := path.Join(dir, "file2")
17-
os.Create(file2)
17+
_, _ = os.Create(file2)
1818

19-
RemoveProfilePath(file1)
19+
_ = RemoveProfilePath(file1)
2020
entries, _ := os.ReadDir(dir)
2121
files := make([]string, len(entries))
2222
for i, e := range entries {

internal/repository/git.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func RepositoryClone(directory, url, commitId, accessToken string) error {
3838
Hash: plumbing.NewHash(commitId),
3939
})
4040
if err != nil {
41-
return fmt.Errorf("Cannot checkout the commit ID %s: '%s'", commitId, err)
41+
return fmt.Errorf("cannot checkout the commit ID %s: '%s'", commitId, err)
4242
}
4343
return nil
4444
}
@@ -66,7 +66,7 @@ func hasNotBeenHardReset(r repository, branchName string, currentMainHash *plumb
6666
return err
6767
}
6868
if !ok {
69-
return fmt.Errorf("This branch has been hard reset: its head '%s' is not on top of '%s'",
69+
return fmt.Errorf("this branch has been hard reset: its head '%s' is not on top of '%s'",
7070
remoteMainHead.String(), currentMainHash.String())
7171
}
7272
}
@@ -77,7 +77,7 @@ func getHeadFromRemoteAndBranch(r repository, remoteName, branchName, currentMai
7777
var currentMainHash *plumbing.Hash
7878
head := getRemoteCommitHash(r, remoteName, branchName)
7979
if head == nil {
80-
return newHead, "", fmt.Errorf("The branch '%s/%s' doesn't exist", remoteName, branchName)
80+
return newHead, "", fmt.Errorf("the branch '%s/%s' doesn't exist", remoteName, branchName)
8181
}
8282
if currentMainCommitId != "" {
8383
c := plumbing.NewHash(currentMainCommitId)
@@ -100,7 +100,7 @@ func hardReset(r repository, newHead plumbing.Hash) error {
100100
var w *git.Worktree
101101
w, err := r.Repository.Worktree()
102102
if err != nil {
103-
return fmt.Errorf("Failed to get the worktree")
103+
return fmt.Errorf("failed to get the worktree")
104104
}
105105
err = w.Checkout(&git.CheckoutOptions{
106106
Hash: newHead,
@@ -154,7 +154,7 @@ func isAncestor(r *git.Repository, base, top plumbing.Hash) (found bool, err err
154154

155155
// To skip the first commit
156156
isFirst := true
157-
iter.ForEach(func(commit *object.Commit) error {
157+
_ = iter.ForEach(func(commit *object.Commit) error {
158158
if !isFirst && commit.Hash == base {
159159
found = true
160160
// This error is ignored and used to terminate early the loop :/
@@ -223,9 +223,9 @@ func manageRemote(r *git.Repository, remote types.Remote) error {
223223
}
224224

225225
func headSignedBy(r *git.Repository, publicKeys []string) (signedBy *openpgp.Entity, err error) {
226-
head, err := r.Head()
226+
head, _ := r.Head()
227227
if head == nil {
228-
return nil, fmt.Errorf("Repository HEAD should not be nil")
228+
return nil, fmt.Errorf("repository HEAD should not be nil")
229229
}
230230
commit, err := r.CommitObject(head.Hash())
231231
if err != nil {
@@ -238,5 +238,5 @@ func headSignedBy(r *git.Repository, publicKeys []string) (signedBy *openpgp.Ent
238238
return entity, nil
239239
}
240240
}
241-
return nil, fmt.Errorf("Commit %s is not signed", head.Hash())
241+
return nil, fmt.Errorf("commit %s is not signed", head.Hash())
242242
}

internal/repository/git_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func commitFileAndSign(remoteRepository *git.Repository, dir, branch, content st
2222
if err != nil {
2323
return
2424
}
25-
err = w.Checkout(&git.CheckoutOptions{
25+
_ = w.Checkout(&git.CheckoutOptions{
2626
Branch: plumbing.NewBranchReferenceName(branch),
2727
Force: true,
2828
})
@@ -106,7 +106,7 @@ func TestIsAncestor(t *testing.T) {
106106

107107
commits := make([]object.Commit, 3)
108108
idx := 0
109-
err = iter.ForEach(func(commit *object.Commit) error {
109+
_ = iter.ForEach(func(commit *object.Commit) error {
110110
commits[idx] = *commit
111111
idx += 1
112112
return nil
@@ -131,9 +131,9 @@ func TestHeadSignedBy(t *testing.T) {
131131
dir := t.TempDir()
132132
remoteRepository, _ := git.PlainInit(dir, false)
133133

134-
r, err := os.Open("./test.private")
134+
r, _ := os.Open("./test.private")
135135
entityList, _ := openpgp.ReadArmoredKeyRing(r)
136-
commitFileAndSign(remoteRepository, dir, "main", "file-1", entityList[0])
136+
_, _ = commitFileAndSign(remoteRepository, dir, "main", "file-1", entityList[0])
137137

138138
failPublic, _ := os.ReadFile("./fail.public")
139139
testPublic, _ := os.ReadFile("./test.public")
@@ -145,7 +145,7 @@ func TestHeadSignedBy(t *testing.T) {
145145
assert.ErrorContains(t, err, "is not signed")
146146
assert.Nil(t, signedBy)
147147

148-
commitFileAndSign(remoteRepository, dir, "main", "file-2", nil)
148+
_, _ = commitFileAndSign(remoteRepository, dir, "main", "file-2", nil)
149149
signedBy, err = headSignedBy(remoteRepository, []string{string(failPublic), string(testPublic)})
150150
assert.ErrorContains(t, err, "is not signed")
151151
assert.Nil(t, signedBy)

0 commit comments

Comments
 (0)