Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update cobra #1

Merged
merged 54 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
ad6db7f
Create unit test illustrating unknown flag bug (#1854)
brianpursley Nov 15, 2022
4305498
Update stale.yml (#1863)
enrichman Nov 22, 2022
c6b9971
fix: force ForEach-Object to return array in pwsh completion (#1850)
totkeks Nov 25, 2022
6200c8e
Makefile: add target richtest (#1865)
umarcor Nov 25, 2022
7bb1440
build(deps): bump golangci/golangci-lint-action from 3.2.0 to 3.3.1 (…
dependabot[bot] Nov 26, 2022
a6f198b
Update kubescape org (#1874)
Dec 6, 2022
9235920
ci: deprecate go 1.15 (#1866)
umarcor Dec 9, 2022
f25a3c6
fix: conflict import name with variable (#1879)
yashLadha Dec 15, 2022
fdffa5a
Update badge route (#1884)
sh-cho Dec 23, 2022
bf11ab6
fix: func name in doc strings (#1885)
yashLadha Dec 25, 2022
b4f979a
completions: do not detect arguments with dash as 2nd char as flag (#…
oncilla Jan 3, 2023
4fa4fdf
build(deps): bump github.com/inconshreveable/mousetrap (#1872)
dependabot[bot] Jan 3, 2023
d022c0f
Add documentation about disabling completion descriptions (#1901)
Shihta Jan 30, 2023
8b8ee87
Improve MarkFlagsMutuallyExclusive example in User Guide (#1904)
janhn Feb 6, 2023
87ebcd8
Update shell_completions.md (#1907)
gusega Feb 9, 2023
e839bb3
build(deps): bump golangci/golangci-lint-action from 3.3.1 to 3.4.0 (…
dependabot[bot] Feb 12, 2023
a516d41
Removes stale bot from GitHub action (#1908)
jpmcb Feb 14, 2023
3daa4b9
Add keeporder to shell completion (#1903)
h4ck3rk3y Feb 25, 2023
567ea8e
Add support for PowerShell 7.2+ (#1916)
oldium Feb 25, 2023
c7300f0
ci: deprecate go 1.16 (#1926)
umarcor Mar 6, 2023
fb36524
ci: test Golang 1.20 (#1925)
umarcor Mar 6, 2023
9e6b58a
update copyright year (#1927)
umarcor Mar 6, 2023
b197a24
Update projects_using_cobra.md (#1932)
frouioui Mar 14, 2023
c8a20a1
Document suggested layout for subcommands (#1930)
lcarva Mar 17, 2023
45360a5
Allow sourcing zsh completion script (#1917)
marckhouzam Mar 22, 2023
4dd4b25
Update main image to better handle dark background (#1883)
marckhouzam Mar 22, 2023
284f410
Fix typo in fish completions (#1945)
twpayne Apr 8, 2023
0e3a0bf
build(deps): bump golangci/golangci-lint-action from 3.4.0 to 3.5.0 (…
dependabot[bot] Jun 12, 2023
2246fa8
Fix grammar: 'allows to' (#1978)
supertassu Jun 13, 2023
988bd76
test: make fish_completions_test more robust (#1980)
branchvincent Jun 16, 2023
fdee73b
powershell: escape variable with curly brackets (#1960)
Luap99 Jun 19, 2023
cbe4865
build(deps): bump golangci/golangci-lint-action from 3.5.0 to 3.6.0 (…
dependabot[bot] Jun 20, 2023
dcb405a
Move documentation sources to site/content (#1428)
umarcor Jun 20, 2023
c81c46a
Add 'one required flag' group (#1952)
marevers Jul 16, 2023
66b215b
golangci: enable 'unused' and disable deprecated replaced by it (#1983)
umarcor Jul 18, 2023
60d056d
doc: fix typo, Deperecated -> Deprecated (#2000)
callthingsoff Jul 20, 2023
fd865a4
minor corrections to unit tests (#2003)
JunNishimura Jul 23, 2023
4955da7
build(deps): bump golangci/golangci-lint-action from 3.6.0 to 3.7.0 (…
dependabot[bot] Aug 28, 2023
285460d
command: temporarily disable G602 due to securego/gosec#1005 (#2022)
umarcor Aug 29, 2023
c5dacb3
ci: test golang 1.21 (#2024)
nunoadrego Sep 8, 2023
0c72800
Customizable error message prefix (#2023)
5ouma Sep 8, 2023
bd4d165
feat: add getters for flag completions (#1943)
avirtopeanu-ionos Sep 26, 2023
95d8a1e
Add notes to doc on preRun and postRun condition (#2041)
haoming29 Oct 9, 2023
efe8fa3
build(deps): bump actions/setup-go from 3 to 4 (#1934)
dependabot[bot] Oct 15, 2023
5c962a2
build(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.2 to 2.0.…
dependabot[bot] Oct 16, 2023
4cafa37
Allow running persistent run hooks of all parents (#2044)
vkhoroz Oct 22, 2023
8b1eba4
Fix linter errors (#2052)
marckhouzam Oct 27, 2023
b711e87
Don't complete --help flag when flag parsing disabled (#2061)
marckhouzam Oct 28, 2023
00b68a1
Add tests for flag completion registration (#2053)
marckhouzam Oct 28, 2023
22953d8
Replace all non-alphanumerics in active help env var program prefix (…
scop Oct 29, 2023
48cea5c
build(deps): bump actions/checkout from 3 to 4 (#2028)
dependabot[bot] Oct 30, 2023
890302a
Support usage as plugin for tools like kubectl (#2018)
nirs Nov 2, 2023
a0a6ae0
Improve API to get flag completion function (#2063)
marckhouzam Nov 2, 2023
bfd8d9b
feat: expose GetCompletions (was getCompletions)
gfyrag Nov 12, 2022
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
56 changes: 0 additions & 56 deletions .github/workflows/stale.yml

This file was deleted.

36 changes: 16 additions & 20 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- run: >-
docker run
-v $(pwd):/wrk -w /wrk
ghcr.io/google/addlicense
-c 'The Cobra Authors'
-y '2013-2022'
-y '2013-2023'
-l apache
-ignore '.github/**'
-check
Expand All @@ -39,17 +39,17 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: '^1.19'
go-version: '^1.21'
check-latest: true
cache: true

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: golangci/golangci-lint-action@v3.2.0
- uses: golangci/golangci-lint-action@v3.7.0
with:
version: latest
args: --verbose
Expand All @@ -63,32 +63,28 @@ jobs:
- ubuntu
- macOS
go:
- 15
- 16
- 17
- 18
- 19
- 20
- 21
name: '${{ matrix.platform }} | 1.${{ matrix.go }}.x'
runs-on: ${{ matrix.platform }}-latest
steps:

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.${{ matrix.go }}.x
cache: true

- run: |
export GOBIN=$HOME/go/bin
case "${{ matrix.go }}" in
14|15) _version='';;
*) _version='@latest';;
esac
go install github.com/kyoh86/richgo"${_version}"
go install github.com/mitchellh/gox"${_version}"
go install github.com/kyoh86/richgo@latest
go install github.com/mitchellh/gox@latest

- run: RICHGO_FORCE_COLOR=1 PATH=$HOME/go/bin/:$PATH make test
- run: RICHGO_FORCE_COLOR=1 PATH=$HOME/go/bin/:$PATH make richtest


test-win:
Expand All @@ -112,7 +108,7 @@ jobs:
unzip
mingw-w64-x86_64-go

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/cache@v3
with:
Expand All @@ -125,4 +121,4 @@ jobs:
go install github.com/kyoh86/richgo@latest
go install github.com/mitchellh/gox@latest

- run: RICHGO_FORCE_COLOR=1 PATH=$HOME/go/bin:$PATH make test
- run: RICHGO_FORCE_COLOR=1 PATH=$HOME/go/bin:$PATH make richtest
10 changes: 5 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2013-2022 The Cobra Authors
# Copyright 2013-2023 The Cobra Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@ linters:
disable-all: true
enable:
#- bodyclose
- deadcode
# - deadcode ! deprecated since v1.49.0; replaced by 'unused'
#- depguard
#- dogsled
#- dupl
Expand Down Expand Up @@ -51,12 +51,12 @@ linters:
#- rowserrcheck
#- scopelint
#- staticcheck
- structcheck
#- structcheck ! deprecated since v1.49.0; replaced by 'unused'
#- stylecheck
#- typecheck
- unconvert
#- unparam
#- unused
- varcheck
- unused
# - varcheck ! deprecated since v1.49.0; replaced by 'unused'
#- whitespace
fast: false
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ ifeq (, $(shell which golangci-lint))
$(warning "could not find golangci-lint in $(PATH), run: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh")
endif

ifeq (, $(shell which richgo))
$(warning "could not find richgo in $(PATH), run: go install github.com/kyoh86/richgo@latest")
endif

.PHONY: fmt lint test install_deps clean

default: all
Expand All @@ -25,6 +21,10 @@ lint:

test: install_deps
$(info ******************** running tests ********************)
go test -v ./...

richtest: install_deps
$(info ******************** running tests with kyoh86/richgo ********************)
richgo test -v ./...

install_deps:
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
![cobra logo](https://cloud.githubusercontent.com/assets/173412/10886352/ad566232-814f-11e5-9cd0-aa101788c117.png)
![cobra logo](assets/CobraMain.png)

Cobra is a library for creating powerful modern CLI applications.

Cobra is used in many Go projects such as [Kubernetes](https://kubernetes.io/),
[Hugo](https://gohugo.io), and [GitHub CLI](https://github.com/cli/cli) to
name a few. [This list](./projects_using_cobra.md) contains a more extensive list of projects using Cobra.
name a few. [This list](site/content/projects_using_cobra.md) contains a more extensive list of projects using Cobra.

[![](https://img.shields.io/github/workflow/status/spf13/cobra/Test?longCache=tru&label=Test&logo=github%20actions&logoColor=fff)](https://github.com/spf13/cobra/actions?query=workflow%3ATest)
[![](https://img.shields.io/github/actions/workflow/status/spf13/cobra/test.yml?branch=main&longCache=true&label=Test&logo=github%20actions&logoColor=fff)](https://github.com/spf13/cobra/actions?query=workflow%3ATest)
[![Go Reference](https://pkg.go.dev/badge/github.com/spf13/cobra.svg)](https://pkg.go.dev/github.com/spf13/cobra)
[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cobra)](https://goreportcard.com/report/github.com/spf13/cobra)
[![Slack](https://img.shields.io/badge/Slack-cobra-brightgreen)](https://gophers.slack.com/archives/CD3LP1199)
Expand Down Expand Up @@ -80,7 +80,7 @@ which maintains the same interface while adding POSIX compliance.

# Installing
Using Cobra is easy. First, use `go get` to install the latest version
of the library.
of the library.

```
go get -u github.com/spf13/cobra@latest
Expand All @@ -105,8 +105,8 @@ go install github.com/spf13/cobra-cli@latest

For complete details on using the Cobra-CLI generator, please read [The Cobra Generator README](https://github.com/spf13/cobra-cli/blob/main/README.md)

For complete details on using the Cobra library, please read the [The Cobra User Guide](user_guide.md).
For complete details on using the Cobra library, please read the [The Cobra User Guide](site/content/user_guide.md).

# License

Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)
Cobra is released under the Apache 2.0 license. See [LICENSE.txt](LICENSE.txt)
12 changes: 8 additions & 4 deletions active_help.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2022 The Cobra Authors
// Copyright 2013-2023 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@ package cobra
import (
"fmt"
"os"
"regexp"
"strings"
)

Expand All @@ -29,6 +30,8 @@ const (
activeHelpGlobalDisable = "0"
)

var activeHelpEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)

// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
// Such strings will be processed by the completion script and will be shown as ActiveHelp
// to the user.
Expand All @@ -42,7 +45,7 @@ func AppendActiveHelp(compArray []string, activeHelpStr string) []string {

// GetActiveHelpConfig returns the value of the ActiveHelp environment variable
// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
// case, with all - replaced by _.
// case, with all non-ASCII-alphanumeric characters replaced by `_`.
// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
// is set to "0".
func GetActiveHelpConfig(cmd *Command) string {
Expand All @@ -55,9 +58,10 @@ func GetActiveHelpConfig(cmd *Command) string {

// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
// root command in upper case, with all - replaced by _.
// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
func activeHelpEnvVar(name string) string {
// This format should not be changed: users will be using it explicitly.
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")
return activeHelpEnvVar
}
2 changes: 1 addition & 1 deletion active_help_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2022 The Cobra Authors
// Copyright 2013-2023 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions args.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2022 The Cobra Authors
// Copyright 2013-2023 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@ import (

type PositionalArgs func(cmd *Command, args []string) error

// Legacy arg validation has the following behaviour:
// legacyArgs validation has the following behaviour:
// - root commands with no subcommands can take arbitrary arguments
// - root commands with subcommands will do subcommand validity checking
// - subcommands will always accept arbitrary arguments
Expand Down
2 changes: 1 addition & 1 deletion args_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2022 The Cobra Authors
// Copyright 2013-2023 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Binary file added assets/CobraMain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions bash_completions.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2022 The Cobra Authors
// Copyright 2013-2023 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,7 +85,7 @@ __%[1]s_handle_go_custom_completion()
local out requestComp lastParam lastChar comp directive args

# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly %[1]s allows to handle aliases
# Calling ${words[0]} instead of directly %[1]s allows handling aliases
args=("${words[@]:1}")
# Disable ActiveHelp which is not supported for bash completion v1
requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}"
Expand Down Expand Up @@ -532,7 +532,7 @@ func writeLocalNonPersistentFlag(buf io.StringWriter, flag *pflag.Flag) {
}
}

// Setup annotations for go completions for registered flags
// prepareCustomAnnotationsForFlags setup annotations for go completions for registered flags
func prepareCustomAnnotationsForFlags(cmd *Command) {
flagCompletionMutex.RLock()
defer flagCompletionMutex.RUnlock()
Expand Down
22 changes: 18 additions & 4 deletions bash_completionsV2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2022 The Cobra Authors
// Copyright 2013-2023 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,7 +57,7 @@ __%[1]s_get_completion_results() {
local requestComp lastParam lastChar args

# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly %[1]s allows to handle aliases
# Calling ${words[0]} instead of directly %[1]s allows handling aliases
args=("${words[@]:1}")
requestComp="${words[0]} %[2]s ${args[*]}"

Expand Down Expand Up @@ -101,6 +101,7 @@ __%[1]s_process_completion_results() {
local shellCompDirectiveNoFileComp=%[5]d
local shellCompDirectiveFilterFileExt=%[6]d
local shellCompDirectiveFilterDirs=%[7]d
local shellCompDirectiveKeepOrder=%[8]d

if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion.
Expand All @@ -115,6 +116,19 @@ __%[1]s_process_completion_results() {
__%[1]s_debug "No space directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveKeepOrder) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
# no sort isn't supported for bash less than < 4.4
if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then
__%[1]s_debug "No sort directive not supported in this version of bash"
else
__%[1]s_debug "Activating keep order"
compopt -o nosort
fi
else
__%[1]s_debug "No sort directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveNoFileComp) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__%[1]s_debug "Activating no file completion"
Expand Down Expand Up @@ -183,7 +197,7 @@ __%[1]s_process_completion_results() {
# Separate activeHelp lines from real completions.
# Fills the $activeHelp and $completions arrays.
__%[1]s_extract_activeHelp() {
local activeHelpMarker="%[8]s"
local activeHelpMarker="%[9]s"
local endIndex=${#activeHelpMarker}

while IFS='' read -r comp; do
Expand Down Expand Up @@ -360,7 +374,7 @@ fi
# ex: ts=4 sw=4 et filetype=sh
`, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
activeHelpMarker))
}

Expand Down
Loading
Loading