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

How to setup cobra and viper for test configurations in golang

We have a binary we are starting which has several subcommands, all set up with cobra. And we want to use viper for configuration. So the initialization of this is setup according to the docs: func init() { cobra.OnInitialize(initConfig) …
transient_loop
  • 5,984
  • 15
  • 58
  • 117
1
vote
0 answers

Dynamic creation of cobra commands based on directory specified by flag

We already add cobra commands dynamically based on the current directory context (adding them in init()). If certain things are available in the directory, commands get added. Now there's a requirement to use a flag to decide what directory to…
rfay
  • 9,963
  • 1
  • 47
  • 89
1
vote
0 answers

Passing a variable from cobra command to another

I'm working on a Go app that launches a blockchain p2p nodes. For now the user has to manually start the p2p nodes and submit data via terminal. I'm trying to add a HTTP server to add/read data from the blockchain via REST. The problem is that I'm…
med.b
  • 465
  • 2
  • 12
  • 21
1
vote
1 answer

In Cobra command-line tool, how to use the same variable for different flags?

This example Cobra app, https://github.com/kurtpeek/myCobraApp, contains a Cobra app scaffolded using the Cobra generator with the following commands: cobra add serve cobra add config The directory structure is . ├── LICENSE ├── cmd │   ├──…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
1
vote
1 answer

How to prevent negative integer treated as shorthand flag

I need to parse command line args containing negative ints, spf13/cobra is the library of choice: go run main.go write -d 1 -a mock -e int 0 2 -1 Unfortunately cobra thinks that -1 is a shorthand flag which is of course not defined: Error: unknown…
andig
  • 13,378
  • 13
  • 61
  • 98
1
vote
1 answer

Trouble using Cobra/Viper

I'm having trouble using Cobra and Viper together. This is what I'm doing: var options util.Config = util.Config{} var rootCmd = &cobra.Command{ Use: "test [command] [subcommands]", Run: func(cmd *cobra.Command, args []string) { if…
ddibiase
  • 1,412
  • 1
  • 20
  • 44
1
vote
1 answer

Cobra cli fails to initialise new tool

I have an empty dir with a go project, with only go.mod being present (I have run the go mod init command) ▶ cat go.mod module github.com/myorganization/mytool go 1.13 I would expect that the following command: ▶ ~/go/bin/cobra init mytool would…
pkaramol
  • 16,451
  • 43
  • 149
  • 324
1
vote
0 answers

Config files and Golang or just beginner mistake

I have done a few tutorials in Go and wanted to try something much more relevant that is going to needed from me relatively soon. Im starting by looking at viper and cobra. I understand the config file is actually for viper to use and cobra is just…
ruck64
  • 145
  • 1
  • 10
1
vote
1 answer

How to use a flag with two different names (short and long)

I am using https://github.com/spf13/cobra to work with flags. I want my CLI to have flag with two names: -t or --token. I currently used it like that: rootCmd.PersistentFlags().String("token", "", "Token to insert") But it prints me the…
E235
  • 11,560
  • 24
  • 91
  • 141
1
vote
0 answers

Where should a *sqlx.DB object be save for a cobra application?

I have a cobra project which accesses database using sqlx package, and the command has sub-commands. The cobra related code is in the cmd folder/subpackage and the DB related code is in the db folder/subpackage. I would like to have package db to…
nos
  • 19,875
  • 27
  • 98
  • 134
1
vote
2 answers

Golang patterns for stdin testing

EDIT: Adrian's suggestion makes sense, so I moved my code into a function and called the function from my cobra block: package cmd import ( "fmt" "log" "os" "io" "github.com/spf13/cobra" "github.com/spf13/viper" input…
Peter Souter
  • 5,110
  • 1
  • 33
  • 62
1
vote
1 answer

Cobra MarkPersistentFlagRequired not working on Root

Using spf13/Cobra for cli flag parsing. root command has a field marked required: rootCmd.PersistentFlags().StringVarP(&configFilePath, "config", "c","", "REQUIRED: config file") rootCmd.MarkPersistentFlagRequired("config") …
buildmaestro
  • 1,386
  • 5
  • 22
  • 37
1
vote
0 answers

Implementing Go interfaces using cobra commands

I have written a program to calculate basic arithmetic functions using interfaces in Go language. I want to do the same using COBRA commands. Here is some Go code using interfaces. I have also implemented the arithmetic functions using cobra…
Manu
  • 77
  • 2
  • 11
0
votes
1 answer

on windows, how to execute go bin commands of go cobra package?

I am studying go cobra I need to execute, on windows, the equivalent of ~/go/bin/cobra init I already did go get -u github.com/spf13/cobra/cobra I don't know how to execute the bin/cobra command because I don't know where it's installed now the…
realtebo
  • 23,922
  • 37
  • 112
  • 189
0
votes
2 answers

Passing Persistant flags for Cobra CLI for testing

I have a CLI application written in Cobra. The app contains a root command RootCmd which contains two PersistentFlags. One of the persistent flags is called threads which has a default value of 1 and shorthand -h. …
Mohamed Yasser
  • 641
  • 7
  • 17
1 2 3
8 9