Questions tagged [go-cobra]

A Commander for modern Go CLI interactions

A Commander for modern Go CLI interactions (https://github.com/spf13/cobra)

125 questions
4
votes
1 answer

How to put cobra sub commands sources into separate folders

I'm a Go beginner, and I'm trying to create a CLI with Cobra. To bootstrap the project, I used the Cobra Generator, generated a command, a subcommand, and everything works fine. I now have this type of layout : cli ├── cmd │   ├── command.go │   ├──…
aTom
  • 406
  • 6
  • 17
4
votes
1 answer

What does this command do 'GOFLAGS=-mod=mod'?

I am trying to make a Taskfile.yml file for building go application, but I can't quite understand the need of "GOFLAGS=-mod=mod" command before go build main.go. reference:…
Harshit Kumar
  • 79
  • 1
  • 5
4
votes
1 answer

If defallt value of "cobra.Command BoolVarP" is "true", it is always "true"

my code var require bool cmd.Flags().BoolVarP(&require, "require", "r", true, "default: true.") If default value is "true", weather I add "-r" or not, "require" is always "true". But if default value is "false", it works…
WarmSunshine
  • 81
  • 1
  • 7
4
votes
1 answer

How do I extract only flagsets that are being set explicitly in the cli?

I am new to cobra and viper. I wanted to know if there's a way to exclude flag values that aren't set by the user from cli. So my problem is, I have some optional flags in my cobra cmd that have default values. I wonder if there's a way to exclude…
dekauliya
  • 1,303
  • 2
  • 15
  • 26
4
votes
2 answers

Share properties from separate commands/process

Im providing command line tool with several command and sub commands, Im using cobra command line and I’ve two separate commands that first is prerequisite to other e.g. the first command is preferring the environment by creating temp folder and…
user6124024
3
votes
1 answer

cobra-cli pass all arguments and flags to an executable

I have a cobra CLI for my own stuff. Now I want to add commonly used executables e.g. kubectl, calicoctl as subcommands which will consume all arguments and flags like mywrapper kubectl get all --all-namespaces mywrapper kubectl create deployment…
Moazzem Hossen
  • 2,276
  • 1
  • 19
  • 30
3
votes
0 answers

getting cross-package coverage in golang without init() running

I have inherited a very large, very old golang codebase. I'm trying to get coverage stats and most of the tests are testing across packages. If I run go test ./... the tests pass. If i run go test -coverpkg=./... ./... then the tests fail because…
Shiny
  • 111
  • 1
  • 4
3
votes
1 answer

how to debug golang cobra cli app in vscode

I have a golang cobra cli app. have configured my vscode for debugging. I want to debug a specific command using vscode for my application. I am using this launch.json { "name": "Launch Package", "type": "go", …
Chetan
  • 137
  • 2
  • 9
3
votes
1 answer

How to support an old flag name when changing to a new flag name with Cobra?

I have a CLI with a flag name that I would like to rename, but I would also like to make it so this change is backwards compatible and using the old flag name still works, such as below: # New flag name root subcommand --no-color # Old flag name…
dhelfand
  • 141
  • 1
  • 7
3
votes
1 answer

cobra PersistentPreRun stack overflow

why does the following CLI program using the cobra package throw a stack overflow error when ran with go run /tmp/test.go branch leaf, but does not error when when leaf subcommand is directly connected to root(as commented in the main…
Shang Jian Ding
  • 1,931
  • 11
  • 24
3
votes
1 answer

Golang CLI cobra count flag

I would like to define a CLI flag that counts the number of times that it appears. For example, let's define the flag --verbose with its shorthand -v: # verbose value should be 0 > myCmd # verbose value should be 1 > myCmd -v # verbose value…
user13969944
3
votes
4 answers

API Gateway HTTP client request with IAM auth with Go

Hello StackOverflow AWS Gophers, I'm implementing a CLI with the excellent cobra/viper packages from spf13. We have an Athena database fronted by an API Gateway endpoint, which authenticates with IAM. That is, in order to interact with its…
3
votes
1 answer

GO C-Style Console Application

I created a Go CLI app with the cobra framework via the cobra add command. After building it with go build it works totally fine if I'm in the repository in the windows cmd. If I click on the .exe I get the following message on a terminal: This is a…
alexfwulf
  • 199
  • 1
  • 13
3
votes
1 answer

Making Cobra flag bash completion using remote data

I'm working on a Cobra-based Go app and I want to have a bash completion for command line flags: $ my-cool-app --some-id= The list of valid values for --some-id flag is available by making some HTTP call. How to make such bash completion available…
Petr Razumov
  • 1,952
  • 2
  • 17
  • 32
3
votes
1 answer

How to Unmarshall Viper config value to struct containing array of string properly?

I noticed that this is perhaps a bug when viper tries to unmarshall to struct. To explain it better, consider this: I have a cli command like below dd-cli submit-bug --name "Bug 1" --tag reason1 --tag reason2 Here is my command line source…
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77
1
2
3
8 9