Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
5f09103
Fix file formatting with gofumpt -w -s
invidian Oct 29, 2021
d8684ec
.golangci.yml: initial commit
invidian Oct 29, 2021
51eb3a7
.github/workflows/go.yml: rename build job to test
invidian Oct 29, 2021
649d5d4
.github/workflows/go.yml: add lint job
invidian Oct 29, 2021
bfea7cf
pkg/client: fix error wrapping on error unmarshalling
invidian Oct 29, 2021
10f3403
.golangci.yml: re-enable errorlint linter
invidian Oct 29, 2021
157b784
pkg/client: improve name of URL variable and struct field
invidian Oct 29, 2021
8282967
pkg/client: rename variable ID -> id
invidian Oct 31, 2021
a674d0d
.golangci.yml: enable gocritic linter
invidian Oct 31, 2021
8985817
pkg/client: run all tests in parallel
invidian Oct 31, 2021
d8c467d
.golangci.yml: enable paralleltest linter
invidian Oct 31, 2021
0bfa3bb
pkg/client: add missing dots in comments
invidian Oct 31, 2021
3bf6988
.golangci.yml: enable godot linter
invidian Oct 31, 2021
01e4719
pkg/client: improve formatting
invidian Oct 31, 2021
24c0c4a
.golangci.yml: enable wsl linter
invidian Oct 31, 2021
860d78c
Consistently write FleetLock as "FleetLock" when possible
invidian Oct 31, 2021
5360680
pkg/client: fix ClientParams documentation styling
invidian Oct 31, 2021
2206f52
Add basic package comments
invidian Oct 31, 2021
b71ff23
.golangci.yml: enable stylecheck linter
invidian Oct 31, 2021
2614160
pkg/client: fix typo identifer -> identifier
invidian Oct 31, 2021
f968900
.golangci.yml: enable misspell linter
invidian Oct 31, 2021
4704413
pkg/client: fix too long line and duplicated error string in tests
invidian Oct 31, 2021
eb66470
.golangci.yml: enable lll linter
invidian Oct 31, 2021
e87e1b2
pkg/client/client_test.go: improve formatting
invidian Oct 31, 2021
465076d
.golangci.yml: enable nlreturn linter
invidian Oct 31, 2021
988ef1c
pkg/client: rename ClientParams struct to just Params
invidian Oct 31, 2021
a79a6a0
cmd: add comment for exported variable Root
invidian Oct 31, 2021
3ed8192
.golangci.yml: enable revive linter
invidian Oct 31, 2021
e5fbfda
pkg/client: add nolint tag for client_params JSON field
invidian Oct 31, 2021
8d9945c
.golangci.yml: enable tagliatelle linter
invidian Oct 31, 2021
aad369a
cmd: don't use global variables and init functions
invidian Oct 31, 2021
3a3b997
.golangci.yml: enable gochecknoglobals and gochecknoinits variables
invidian Oct 31, 2021
4fa57ee
.golangci.yml: exclude false-positives from paralleltest linter
invidian Oct 31, 2021
16a7d15
pkg/client: refactor client tests to avoid cases duplication
invidian Oct 31, 2021
02529f5
.golangci.yml: enable dupl linter
invidian Oct 31, 2021
bc5e637
pkg/client: remove unreachable code from handling response
invidian Oct 31, 2021
92a646f
.golangci.yml: enable govet linter
invidian Oct 31, 2021
d51ff0f
pkg/client: add nolint comment on closing response body
invidian Oct 31, 2021
655b49d
pkg/client: add error handling in tests
invidian Oct 31, 2021
2c3dc97
.golangci.yml: enable errcheck linter
invidian Oct 31, 2021
298a8f8
.golangci.yml: enable extra linter settings
invidian Oct 31, 2021
c462242
pkg/client: accept context for operations
invidian Oct 31, 2021
3b472be
.golangci.yml: enable noctx linter
invidian Oct 31, 2021
b167c5e
pkg/client: get rid of magic numbers for HTTP response codes
invidian Oct 31, 2021
507dc95
.golangci.yml: enable gomnd linter
invidian Oct 31, 2021
3f2ab41
.golangci.yml: update list of linters
invidian Oct 31, 2021
f170b88
Makefile: add targets for validating golangci-lint configuration
invidian Oct 31, 2021
42eb797
.github/workflows/go.yml: check if list of linters is up to date
invidian Oct 31, 2021
86a59b3
Makefile: don't run build in parallel
invidian Oct 31, 2021
2ce5c53
Makefile: add help target
invidian Oct 31, 2021
2ff0ffc
.github/workflows/go.yml: add spell checking job
invidian Nov 1, 2021
421638c
pkg/client: move exported functions to the top of the file
invidian Nov 1, 2021
fc4b824
cmd: improve description of id flag
invidian Nov 2, 2021
60bf0cb
main.go: exit with error exit code on execution error
invidian Nov 2, 2021
a1262c8
main.go: don't print error message twice
invidian Nov 2, 2021
0e1c5d3
cmd: add description for subcommands
invidian Nov 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ on:

jobs:

build:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -20,3 +21,29 @@ jobs:

- name: Test
run: go test -v ./...

lint:
name: Lint
runs-on: ubuntu-latest
container: golangci/golangci-lint:v1.42
steps:
- uses: actions/checkout@v2

- name: Check linters are up to date
run: make test-update-linters

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.42

codespell:
name: Spell Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: codespell-project/actions-codespell@master
with:
skip: .git
check_filenames: true
check_hidden: true
116 changes: 116 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
output:
sort-results: true

issues:
exclude-use-default: false
max-same-issues: 0
max-issues-per-linter: 0
exclude-rules:
# False positive: https://github.com/kunwardeep/paralleltest/issues/8.
- linters:
- paralleltest
text: "does not use range value in test Run"

linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
gci:
local-prefixes: github.com/flatcar-linux/fleetlock
godot:
capital: true
gofumpt:
extra-rules: true
govet:
enable-all: true
disable:
- fieldalignment
- shadow
makezero:
always: true
nolintlint:
allow-leading-space: false
require-explanation: true
require-specific: true
wsl:
force-err-cuddling: true

linters:
disable-all: false
disable:
# We do not have clearly defined error types yet.
- goerr113
# We do not require all used structs to have all fields initialized.
- exhaustivestruct
# This linters has been deprecated.
- interfacer
- maligned
- golint
enable:
- asciicheck
- bodyclose
- cyclop
- deadcode
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errname
- errorlint
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- ifshort
- importas
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nlreturn
- noctx
- nolintlint
- paralleltest
- prealloc
- predeclared
- promlinter
- revive
- rowserrcheck
- scopelint
- sqlclosecheck
- structcheck
- stylecheck
- tagliatelle
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- varcheck
- wastedassign
- whitespace
- wrapcheck
- wsl
24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
build:
GOLANGCILINT := go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.42.1

.PHONY: build
build: ## Compiles CLI binary.
@go build -o ./fleetlockctl main.go

.PHONY: test-working-tree-clean
test-working-tree-clean: ## Checks if working directory is clean.
@test -z "$$(git status --porcelain)" || (echo "Commit all changes before running this target"; exit 1)

.PHONY: update-linters
update-linters: ## Updates list of linters in .golangci.yml file based on currently installed golangci-lint binary.
# Remove all enabled linters.
sed -i '/^ enable:/q0' .golangci.yml
# Then add all possible linters to config.
$(GOLANGCILINT) linters | grep -E '^\S+:' | cut -d: -f1 | sort | sed 's/^/ - /g' | grep -v -E "($$(grep '^ disable:' -A 100 .golangci.yml | grep -E ' - \S+$$' | awk '{print $$2}' | tr \\n '|' | sed 's/|$$//g'))" >> .golangci.yml

.PHONY: test-update-linters
test-update-linters: test-working-tree-clean update-linters ## Verifies that list of linters in .golangci.yml file is up to date.
@test -z "$$(git status --porcelain)" || (echo "Linter configuration outdated. Run 'make update-linters' and commit generated changes to fix."; exit 1)

.PHONY: help
help: ## Prints help message.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Fleetlock client
## FleetLock client

[![Go Reference](https://pkg.go.dev/badge/github.com/flatcar-linux/fleetlock.svg)](https://pkg.go.dev/github.com/flatcar-linux/fleetlock)
[![Go](https://github.com/flatcar-linux/fleetlock/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/flatcar-linux/fleetlock/actions/workflows/go.yml)
Expand Down
14 changes: 8 additions & 6 deletions cmd/lock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"net/http"

Expand All @@ -9,22 +10,23 @@ import (
"github.com/flatcar-linux/fleetlock/pkg/client"
)

var (
lock = &cobra.Command{
Use: "recursive-lock",
func lock(group, id, url *string) *cobra.Command {
return &cobra.Command{
Use: "recursive-lock",
Short: "Try to reserve (lock) a slot for rebooting",
RunE: func(cmd *cobra.Command, args []string) error {
httpClient := http.DefaultClient

c, err := client.New(url, group, id, httpClient)
c, err := client.New(*url, *group, *id, httpClient)
if err != nil {
return fmt.Errorf("building the client: %w", err)
}

if err := c.RecursiveLock(); err != nil {
if err := c.RecursiveLock(context.Background()); err != nil {
return fmt.Errorf("locking: %w", err)
}

return nil
},
}
)
}
22 changes: 12 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// Package cmd implements fleetlockctl CLI.
package cmd

import (
"github.com/spf13/cobra"
)

var (
group, id, url string
// Command returns CLI command to be executed.
func Command() *cobra.Command {
cli := &cobra.Command{Use: "fleetlockctl"}

Root = &cobra.Command{Use: "fleetlockctl"}
)
var group, id, url string

cli.PersistentFlags().StringVarP(&group, "group", "g", "default", "FleetLock group")
cli.PersistentFlags().StringVarP(&id, "id", "i", "", "FleetLock instance ID (e.g. content of /etc/machine-id file)")
cli.PersistentFlags().StringVarP(&url, "url", "u", "", "FleetLock endpoint URL")

func init() {
Root.PersistentFlags().StringVarP(&group, "group", "g", "default", "Fleet-Lock group")
Root.PersistentFlags().StringVarP(&id, "id", "i", "", "Fleet-Lock instance ID (/etc/machine-id for example)")
Root.PersistentFlags().StringVarP(&url, "url", "u", "", "Fleet-Lock endpoint URL")
cli.AddCommand(lock(&group, &id, &url))
cli.AddCommand(unlock(&group, &id, &url))

Root.AddCommand(lock)
Root.AddCommand(unlock)
return cli
}
14 changes: 8 additions & 6 deletions cmd/unlock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"net/http"

Expand All @@ -9,22 +10,23 @@ import (
"github.com/flatcar-linux/fleetlock/pkg/client"
)

var (
unlock = &cobra.Command{
Use: "unlock-if-held",
func unlock(group, id, url *string) *cobra.Command {
return &cobra.Command{
Use: "unlock-if-held",
Short: "Try to release (unlock) a slot that it was previously holding",
RunE: func(cmd *cobra.Command, args []string) error {
httpClient := http.DefaultClient

c, err := client.New(url, group, id, httpClient)
c, err := client.New(*url, *group, *id, httpClient)
if err != nil {
return fmt.Errorf("building the client: %w", err)
}

if err := c.UnlockIfHeld(); err != nil {
if err := c.UnlockIfHeld(context.Background()); err != nil {
return fmt.Errorf("unlocking: %w", err)
}

return nil
},
}
)
}
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"fmt"
"os"

"github.com/flatcar-linux/fleetlock/cmd"
)

func main() {
if err := cmd.Root.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "unable to execute command: %v", err)
if err := cmd.Command().Execute(); err != nil {
os.Exit(1)
}
}
Loading