Skip to content

Commit

Permalink
feat!: Stop parsing flags after tool name in shed run (#13)
Browse files Browse the repository at this point in the history
* feat!: Stop parsing flags after the tool name in shed run, -- is no longer necessary

* chore: Change usages of shed run to match new usage
  • Loading branch information
cszatmary committed May 20, 2021
1 parent 86bc654 commit 3736ed4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
command: make build-snapshot
- run:
name: Run tests
command: make test-ci
command: make test

workflows:
lint-build-test:
Expand Down
2 changes: 1 addition & 1 deletion .hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ shed() {
}

if GO_FILES="$(git diff --name-only --staged --diff-filter=ACMR | grep "\.go$")"; then
shed run goimports -- -w $GO_FILES && git add $GO_FILES
shed run goimports -w $GO_FILES && git add $GO_FILES
fi
50 changes: 23 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
SHED = go run main.go
COVERPKGS = ./cache,./client,./internal/spinner,./internal/util,./lockfile,./tool

# Get all dependencies
setup:
# Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: help

setup: ## Install all dependencies
@echo Installing dependencies
@go mod tidy
# Self-hoisted!
Expand All @@ -12,67 +16,59 @@ setup:
@$(SHED) run go-fish install
.PHONY: setup

build:
build: ## Build shed
@go build
.PHONY: build

build-snapshot:
@$(SHED) run goreleaser build -- --snapshot --rm-dist
build-snapshot: ## Create a snapshot release build
@$(SHED) run goreleaser build --snapshot --rm-dist
.PHONY: build-snapshot

release:
release: ## Create a new release of shed
$(if $(version),,$(error version variable is not set))
git tag -a v$(version) -m "v$(version)"
git push origin v$(version)
$(SHED) run goreleaser release -- --rm-dist
$(SHED) run goreleaser release --rm-dist
.PHONY: release

# Generate shell completions for distribution
completions:
completions: ## Generate shell completions for distribution
@mkdir -p completions
@$(SHED) completions bash > completions/shed.bash
@$(SHED) completions zsh > completions/_shed
.PHONY: completions

# Clean all build artifacts
clean:
clean: ## Clean all build artifacts
@rm -rf completions
@rm -rf coverage
@rm -rf dist
@rm -f shed
.PHONY: clean

fmt:
@$(SHED) run goimports -- -w .
fmt: ## Format all go files
@$(SHED) run goimports -w .
.PHONY: fmt

check-fmt:
check-fmt: ## Check if any go files need to be formatted
@./scripts/check_fmt.sh
.PHONY: check-fmt

lint:
lint: ## Lint go files
@$(SHED) run golangci-lint run ./...
.PHONY: lint

# Remove version installed with go install
go-uninstall:
go-uninstall: ## Remove version installed with go install
@rm $(shell go env GOPATH)/bin/shed
.PHONY: go-uninstall

# Run tests and collect coverage data
test:
test: ## Run all tests
@mkdir -p coverage
@go test -coverpkg=$(COVERPKGS) -coverprofile=coverage/coverage.txt ./...
@go tool cover -html=coverage/coverage.txt -o coverage/coverage.html
.PHONY: test

# Run tests and print coverage data to stdout
test-ci:
@mkdir -p coverage
@go test -coverpkg=$(COVERPKGS) -coverprofile=coverage/coverage.txt ./...
@go tool cover -func=coverage/coverage.txt
.PHONY: test-ci
cover: test ## Run all tests and generate coverage data
@go tool cover -html=coverage/coverage.txt -o coverage/coverage.html
.PHONY: cover

# Generate install script to download binaries
scripts/install.sh: .goreleaser.yml
scripts/install.sh: .goreleaser.yml ## Generate install script to download binaries
@$(SHED) run godownloader .goreleaser.yml > $@
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ Or
shed run github.com/golangci/golangci-lint/cmd/golangci-lint run
```

All additional arguments are passed to the tool being run. If you wish to pass flags to the tool, you must
put `--` before any flags to indicate that the flags are for the tool and not shed itself.
All additional arguments are passed to the tool being run. Any flags after the tool name are passed to the
tool directly and are not parsed by shed.

```
shed run stringer -- -type=Pill
shed run stringer -type=Pill
```

## `shed.lock`
Expand Down
13 changes: 9 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ var runCmd = &cobra.Command{
Long: `shed run runs an installed tool passing all arguments to it.
The tool name can either be the full import path or the binary name if it is unique.
In order to pass flags to the tool, you must preceed them with '--'. This signifies to
shed that these flags are meant to be treated as arguments for the tool, and not flags for shed.
All arguments after the tool name will be passed to the tool as is, even if they are flags.
That is, 'shed run --verbose foo' is treated differently than 'shed run foo --verbose'.
The first treats the '--verbose' flag as belonging to shed, the second treats it as belonging
to the 'foo' tool.
For example to run the stringer tool you can either run:
shed run stringer -- -type=Pill
shed run stringer -type=Pill
Or:
shed run golang.org/x/tools/cmd/stringer -- -type=Pill`,
shed run golang.org/x/tools/cmd/stringer -type=Pill`,
Run: func(cmd *cobra.Command, args []string) {
toolName := args[0]
logger := newLogger()
Expand Down Expand Up @@ -62,5 +64,8 @@ Or:
}

func init() {
// Stop parsing flags after first non-flag arg
// so we can pass them to the command being run
runCmd.Flags().SetInterspersed(false)
rootCmd.AddCommand(runCmd)
}
8 changes: 3 additions & 5 deletions scripts/check_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ shed() {
go run main.go "$@"
}

if [ "$(shed run goimports -- -l . | wc -l)" -gt 0 ]; then
echo "Unformatted files found:"
# Run again to print files. Couldn't figure out a way to
# save the output and make it work with wc.
shed run goimports -- -l .
files="$(shed run goimports -l .)"
if [ -n "$files" ]; then
printf "Unformatted files found:\n%s\n" "$files"
exit 1
fi

0 comments on commit 3736ed4

Please sign in to comment.