Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
# if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
go: ["1.21", "1.22", "1.23"]
go: ["1.23", "1.24", "1.25"]
name: Test on Go ${{ matrix.go }}
steps:
# https://github.com/actions/checkout
Expand Down
14 changes: 14 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "2"
linters:
# Default: standard
# default: all

disable:
- errcheck
- staticcheck
# exclusions:
# rules:
# # Exclude some linters from running on tests files.
# - path: _test\.go
# linters:
# - errcheck
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ default: build lint test
build:
go build -o /dev/null .

TEST_OPTS=

.PHONY: test
test:
go test -v ./...

GOLANGCI_LINT_VERSION=v1.61.0
GOLANGCI_LINT = $(shell go env GOPATH)/bin/golangci-lint
go test $(TEST_OPTS) ./...

GOLANG_TOOL_GOBIN=$(shell go env GOBIN)
GOLANG_TOOL_GOPATH=$(shell go env GOPATH)
ifneq ($(GOLANG_TOOL_GOBIN),)
GOLANG_TOOL_PATH_TO_BIN=$(GOLANG_TOOL_GOBIN)
else
GOLANG_TOOL_PATH_TO_BIN=$(GOLANG_TOOL_GOPATH)/bin
endif

GOLANGCI_LINT_VERSION=v2.7.2
GOLANGCI_LINT = $(GOLANG_TOOL_PATH_TO_BIN)/golangci-lint
$(GOLANGCI_LINT):
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

.PHONY: lint
lint: $(GOLANGCI_LINT)
Expand Down
5 changes: 3 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ func NewCommand(args []string) *Command {
}

func splitArgsToEnvsAndCommand(args []string) ([]string, []string) {
equalNotFound := false
notEnvFound := false
var a, b []string
for _, arg := range args {
if !equalNotFound && strings.Contains(arg, "=") {
if !notEnvFound && strings.Contains(arg, "=") {
a = append(a, arg)
} else {
b = append(b, arg)
notEnvFound = true
}
}
return a, b
Expand Down
5 changes: 5 additions & 0 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func TestSplitArgsToEnvsAndCommand(t *testing.T) {
[]string{"key1=val1", "key2=val2"},
[]string{"command", "--arg1", "arg2"},
},
{
[]string{"key1=val1", "command", "--arg1", "arg2", "key2=val2"},
[]string{"key1=val1"},
[]string{"command", "--arg1", "arg2", "key2=val2"},
},
{
[]string{"command", "--arg1", "arg2"},
nil,
Expand Down
4 changes: 4 additions & 0 deletions command/standard_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"bytes"
"io"
"log/slog"
"os"
"os/exec"
)
Expand All @@ -22,6 +23,9 @@ func NewStandardRunner(debugLog bool) *StandardRunner {
func (x *StandardRunner) Run(c *Command) (rerr error) {
cmd := exec.Command(c.Args[0], c.Args[1:]...)
cmd.Env = append(os.Environ(), c.Envs...)

slog.Debug("Executing command", "args", cmd.Args, "envs", c.Envs)

cmd.Stdin = os.Stdin
var buf bytes.Buffer

Expand Down
12 changes: 6 additions & 6 deletions core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ var optionTypes = func() []*opts.Definition[Options] {
),

f.Bool("", "--skip-guard", "Skip the guard check for uncommitted changes and untracked files before executing command.",
func(o *Options) bool { return o.SkipGuard },
func(o *Options) { o.SkipGuard = true },
func(o *Options) bool { return o.GuardOptions.SkipGuard },
func(o *Options) { o.GuardOptions.SkipGuard = true },
),
f.Bool("", "--skip-guard-uncommitted-changes", "Skip the guard check for uncommitted changes before executing command.",
func(o *Options) bool { return o.SkipGuardUncommittedChanges },
func(o *Options) { o.SkipGuardUncommittedChanges = true },
func(o *Options) bool { return o.GuardOptions.SkipGuardUncommittedChanges },
func(o *Options) { o.GuardOptions.SkipGuardUncommittedChanges = true },
),
f.Bool("", "--skip-guard-untracked-files", "Skip the guard check for untracked files before executing command.",
func(o *Options) bool { return o.SkipGuardUntrackedFiles },
func(o *Options) { o.SkipGuardUntrackedFiles = true },
func(o *Options) bool { return o.GuardOptions.SkipGuardUntrackedFiles },
func(o *Options) { o.GuardOptions.SkipGuardUntrackedFiles = true },
),

f.Bool("-D", "--debug-log", "Output debug log.",
Expand Down
14 changes: 14 additions & 0 deletions core/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ func TestParseOptions(t *testing.T) {
[]string{},
"",
},
{
map[string]string{},
[]string{"go", "mod", "edit", "-replace", "foo=bar"},
newExpected(),
[]string{"go", "mod", "edit", "-replace", "foo=bar"},
"",
},
{
map[string]string{},
[]string{"-C", "foo/bar", "go", "mod", "edit", "-replace", "baz/qux=../../baz/qux"},
newExpected(directory("foo/bar")),
[]string{"go", "mod", "edit", "-replace", "baz/qux=../../baz/qux"},
"",
},
}

for _, ptn := range patterns {
Expand Down
2 changes: 2 additions & 0 deletions core/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func Run(options *Options, commandArgs []string) error {
slog.Debug("Run started", "options", options, "commandArgs", commandArgs)

var guardMessage string
if guardResult, err := git.Guard(&options.GuardOptions); err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/akm/git-exec/core"
)
Expand All @@ -14,6 +16,20 @@ func main() {
os.Exit(1)
}

logLevelMap := map[string]slog.Level{
"debug": slog.LevelDebug,
"info": slog.LevelInfo,
"warn": slog.LevelWarn,
"error": slog.LevelError,
}

logLevel, ok := logLevelMap[strings.ToLower(os.Getenv("LOG_LEVEL"))]
if !ok {
logLevel = slog.LevelWarn
}
logHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(logHandler))

options, commandArgs, err := core.ParseOptions(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse arguments: %s\n", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import "fmt"

const Version = "0.1.3"
const Version = "0.1.4"

func showVersion() {
fmt.Println(Version)
Expand Down