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
1
vote
0 answers

Cobra and Viper to edit config

I've got a basic config file that's mashalled and unmarshalled by viper to a struct. type Config struct { field1 string field2 string field3 string } I want to set up cobra to let me do myapp get field1 and myapp set field1 foo for…
XeroxDucati
  • 5,130
  • 2
  • 37
  • 66
1
vote
1 answer

Importing a constants file in go

I have this package structure in my go module, and I am adding commands using cobra-cli: . ├── LICENSE ├── cmd │   ├── constants.go │   ├── root.go │   └── tools │   └── tools.go ├── go.mod ├── go.sum └── main.go In tools.go, I want to import…
Ufder
  • 527
  • 4
  • 20
1
vote
2 answers

viper does not unmarshal values neither from env or pflags

I have the following very simple go program package main import ( "fmt" "github.com/spf13/cobra" "github.com/spf13/viper" ) type Config struct { AFlag string `mapstructure:"A_FLAG"` } var rootCmd = &cobra.Command{ Use: …
pkaramol
  • 16,451
  • 43
  • 149
  • 324
1
vote
0 answers

Golang cobra CLI - get the currentl running subcommand

Does cobra store the name of currently running sub-command somewhere? E.g. if user started the tool with commands like: tool sub-command -flag1 -flag2, etc. tool -globalflag1 sub-command -flag1 -flag2 is it possible to determine inside the program…
user2541436
  • 107
  • 1
  • 1
  • 4
1
vote
1 answer

Unable to initialize a new package using cobra init --pkg-name demo command in go

cobra init --pkg-name demo Error: unknown flag: --pkg-name Usage: cobra init [path] [flags] Aliases: init, initialize, initialise, create Flags: -h, --help help for init Global Flags: -a, --author string author name for copyright…
Naushad
  • 11
  • 2
1
vote
0 answers

Differentiate between required and optional flags in help message in Cobra

I have created a command as follows: cmd := &cobra.Command{ Use: "get", Short: "Gets information", Long: heredoc.Doc("Long description"), Example: heredoc.Doc("example"), ValidArgs: []string{"arg1", "arg2",…
tortuga
  • 737
  • 2
  • 13
  • 34
1
vote
1 answer

Why is Cobra flags not working in this code

How to get flags to work in Cobra using Local flags package main import ( "fmt" "github.com/spf13/cobra" ) func main() { myCmd := &cobra.Command{ Use: "myaction", Run: func(cmd *cobra.Command, args []string) { …
Chris G.
  • 23,930
  • 48
  • 177
  • 302
1
vote
0 answers

Using Cobra and Viper for a Go CLI

I'm creating a CLI in Go and I've decided to use both Cobra and Viper for parsing cli parameters and config files. I looked all over for a way to use both cobra and viper to merge both the configuration file and the given CLI parameters. I've…
Ori Zerah
  • 89
  • 1
  • 7
1
vote
0 answers

How to run sub commands from inside the Cobra root command

I have a Go Cobra CLI app, and I would like to run subcommands based on user input in an infinite loop when the root command is run without any subcommands. myapp has 3 subcommands; list, delete and create. Typically, Cobra apps work by running…
Depp
  • 51
  • 1
  • 8
1
vote
1 answer

Using Logs or cobra.Command Println for user feedback?

Spf13/cobra command offers a number of elegant tools to provide feedback to the user. I have more experience using Python/headless service where the standard is to use logging libraries and then redirect to stdio if necessary. However, the more I’ve…
aronchick
  • 6,786
  • 9
  • 48
  • 75
1
vote
1 answer

How to call SetOut() on subcommands in Cobra?

I am trying to test my CLI application written with Cobra, specifically to test if subcommands are writing correctly to STDOUT. For this, I try to redirect the output from STDOUT to my buffer. Unfortunately, for whatever reason, the SetOut()…
Petr
  • 486
  • 7
  • 19
1
vote
1 answer

How to set command name prefixed with -- in cobra

I want to create a command with name --list, but if I set --list as Use property of cobra.Command it does not works. Like bellow code does not works. Any help? list := &cobra.Command{ Use: "--list", Short: "Lists all data", Run:…
Anup
  • 1,502
  • 2
  • 15
  • 31
1
vote
0 answers

Set default value of Cobra flag with Viper

I'm working on a Cobra-based app in Go and for one of my commands I'm defining a flag like this: func init() { myCmd.Flags().StringVarP(&project, "project", "p", "defaultProject", "Project name") } I also have a Viper set up in my root command…
Radoslav Stoyanov
  • 1,460
  • 5
  • 27
  • 40
1
vote
1 answer

How to add commands at runtime, or to define behavior for unknown commands?

I have a project that works with modules, which are called with CLI commands (ie. myapp foo calls the foo module). Modules can be builtin or user defined. I defined some builtin modules with Cobra commands and it works well. Now I want to handle…
roipoussiere
  • 5,142
  • 3
  • 28
  • 37
1
vote
2 answers

How to hide aliases from help screen in a cobra app

Is it possible to suppress Aliases to show up in the help screen of a golang cobra app? I basically would like to get rid of the whole "Aliases" section below: Usage: my-app [flags] Aliases: alias1 , alias2 Flags: -h, --help help…
Chris
  • 567
  • 6
  • 24
1 2 3
8 9