Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 45 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
name: ci
on: [push]
on:
push:
branches: [main]
pull_request:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
GO_VERSION: '1.26.x'

jobs:
make:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v4
- uses: actions/checkout@v7.0.1
- uses: actions/setup-go@v7.1.0
with:
go-version: "^1.21"
- name: test
run: make test
go-version: ${{ env.GO_VERSION }}
check-latest: true
- run: go mod tidy -diff
- run: make test

lint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7.0.1
- uses: actions/setup-go@v7.1.0
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
- run: make lint

vuln:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7.0.1
- uses: actions/setup-go@v7.1.0
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
- run: make vuln
10 changes: 6 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
linter-settings:
linters:
disable:
- errcheck
version: "2"

linters:
disable:
- errcheck

12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
SHELL = /bin/bash
.ONESHELL:

GOLANGCI_LINT_VERSION ?= v2.15.0
GOVULNCHECK_VERSION ?= v1.10.0

.PHONY: lint
lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3
~/go/bin/golangci-lint run
go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) run ./...

.PHONY: vuln
vuln:
go run golang.org/x/vuln/cmd/govulncheck@$(GOVULNCHECK_VERSION) ./...

.PHONY: test
test:
go test -timeout=3m -race .
go test -timeout=3m -race ./...
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ When compared to cobra, serpent strives for:
* Composition via middleware
* Testability (e.g. OS Stdout and Stderr is only available to commands explicitly)

## Requirements

Serpent requires Go 1.26 or newer. CI tests against the latest Go 1.26 patch release.

## Basic Usage

See `example/echo`:
Expand Down
6 changes: 3 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package serpent

import (
"cmp"
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"slices"
"strings"
"testing"
"unicode"

"cdr.dev/slog/v3"

"github.com/spf13/pflag"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -90,7 +90,7 @@ func (c *Command) Walk(fn func(*Command)) {
}
}

func ascendingSortFn[T constraints.Ordered](a, b T) int {
func ascendingSortFn[T cmp.Ordered](a, b T) int {
if a < b {
return -1
} else if a == b {
Expand Down
3 changes: 1 addition & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func sampleCommand(t *testing.T) *serpent.Command {
return nil
},
Handler: func(i *serpent.Invocation) error {
_, _ = i.Stdout.Write([]byte(fmt.Sprintf("%s-%t", reqStr, reqBool)))
_, _ = fmt.Fprintf(i.Stdout, "%s-%t", reqStr, reqBool)
return nil
},
},
Expand Down Expand Up @@ -1014,7 +1014,6 @@ func TestCommand_DefaultFn(t *testing.T) {
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 4 additions & 4 deletions completion/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,20 @@ func templateConfigSplit(header, footer, data []byte) (before, after []byte, err
startCount := bytes.Count(data, header)
endCount := bytes.Count(data, footer)
if startCount > 1 || endCount > 1 {
return nil, nil, fmt.Errorf("Malformed config file: multiple config sections")
return nil, nil, fmt.Errorf("malformed config file: multiple config sections")
}

startIndex := bytes.Index(data, header)
endIndex := bytes.Index(data, footer)
if startIndex == -1 && endIndex != -1 {
return data, nil, fmt.Errorf("Malformed config file: missing completion header")
return data, nil, fmt.Errorf("malformed config file: missing completion header")
}
if startIndex != -1 && endIndex == -1 {
return data, nil, fmt.Errorf("Malformed config file: missing completion footer")
return data, nil, fmt.Errorf("malformed config file: missing completion footer")
}
if startIndex != -1 && endIndex != -1 {
if startIndex > endIndex {
return data, nil, fmt.Errorf("Malformed config file: completion header after footer")
return data, nil, fmt.Errorf("malformed config file: completion header after footer")
}
// Include leading and trailing newline, if present
start := startIndex
Expand Down
2 changes: 0 additions & 2 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func TestFileCompletion(t *testing.T) {
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
for _, path := range tc.paths {
Expand Down Expand Up @@ -294,7 +293,6 @@ func TestCompletionInstall(t *testing.T) {
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "fake.sh")
Expand Down
1 change: 0 additions & 1 deletion env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestFilterNamePrefix(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := serpent.ParseEnviron(tt.args.environ, tt.args.prefix); !reflect.DeepEqual(got, tt.want) {
Expand Down
2 changes: 1 addition & 1 deletion example/completetest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func installCommand() *serpent.Command {
Handler: func(inv *serpent.Invocation) error {
defaultShell, err := completion.DetectUserShell(inv.Command.Parent.Name())
if err != nil {
return fmt.Errorf("Could not detect user shell, please specify a shell using `--shell`")
return fmt.Errorf("could not detect user shell, please specify a shell using `--shell`")
}
return defaultShell.WriteCompletion(inv.Stdout)
},
Expand Down
52 changes: 22 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
module github.com/coder/serpent

go 1.21.4
go 1.26.0

require (
cdr.dev/slog/v3 v3.0.0-rc1
cdr.dev/slog/v3 v3.2.0
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0
github.com/hashicorp/go-multierror v1.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-wordwrap v1.0.1
github.com/muesli/termenv v0.15.2
github.com/muesli/termenv v0.16.0
github.com/natefinch/atomic v1.0.1
github.com/pion/udp v0.1.4
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/xhit/go-str2duration/v2 v2.1.0
golang.org/x/crypto v0.19.0
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
github.com/pion/transport/v3 v3.1.1
github.com/spf13/pflag v1.1.0
github.com/stretchr/testify v1.12.1
github.com/xhit/go-str2duration/v2 v2.2.0
golang.org/x/term v0.48.0
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.8.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pion/transport/v2 v2.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
go.opentelemetry.io/otel v1.47.0 // indirect
go.opentelemetry.io/otel/sdk v1.47.0 // indirect
go.opentelemetry.io/otel/trace v1.47.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/net v0.63.0 // indirect
golang.org/x/sys v0.50.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260817212433-ac3dfec99bb1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1 // indirect
google.golang.org/grpc v1.85.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
Loading