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
0
votes
2 answers

How do I pass the database connection to all cobra commands?

So I have a cobra command created func init() { rootCmd.AddCommand(versionCmd) } var versionCmd = &cobra.Command{ Use: "version", Short: "Show App version", Long: "Show App version.", Run: func(cmd *cobra.Command, args…
naneri
  • 3,771
  • 2
  • 29
  • 53
0
votes
1 answer

Moving data access and business logic to CLI and using in graphql server

O.! I'm the backend half of a small team that primarily builds apps in postgres/nodejs/apollo graphql/react stack. In my hobby projects I use golang and have gotten decent at constructing CLI apps with cobra/viper. I'm starting to play with the…
Jake Lowen
  • 899
  • 1
  • 11
  • 21
0
votes
1 answer

How to store session in command line interface

I am trying to design a command-line interface in Go based on the cobra package which mimics the functionalities available in the user interface. I am facing a problem when the user provides the initial credentials for login in one command and then…
kushal agrawal
  • 167
  • 2
  • 5
0
votes
1 answer

Running root command logic + sub-command logic

I have the following command + subcommand: aws.go // s3Cmd represents the out command var s3Cmd = &cobra.Command{ Use: "s3", Short: "Uploads saved tarballs to an s3 bucket in aws", Long: `Uploads files to S3 using the credentials…
islamhamdi
  • 207
  • 2
  • 8
0
votes
1 answer

Map flags with words separated-by-dashes to camel_case config?

I have a config format that looks like below: type config struct { FooBar string `mapstructure:"foo_bar"` BazBot string `mapstructure:"baz_bot"` } And my Cobra based CLI has flags with dashes (e.g. --foo-bar=value1 --baz-bot=value2). Viper…
Philip Lombardi
  • 1,276
  • 2
  • 17
  • 26
0
votes
0 answers

How to chain all sub commands in cobra

I want to execute all sub command in cobra. Is there any way to achive this? think that you want to create tcp packet so you need to use tcp command with flags and ipv4 command with flags. If you want to create dns packet so you will need to dns…
Rasit aydin
  • 419
  • 2
  • 6
  • 16
0
votes
1 answer

cobra go cli library ignores flags

Here is an snippet from my-tool/cmd/root.go func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) // Here you will define your flags…
pkaramol
  • 16,451
  • 43
  • 149
  • 324
0
votes
3 answers

Golang Cobra command flags in common function not getting values from cli

I'm moving my cobra command flags inside a function so I can use it in other commands. I can able to see the commands but when I type the flage it always returns false. Following is my code: func NewCommand(ctx context.Context) *cobra.Command { …
DonOfDen
  • 3,968
  • 11
  • 62
  • 112
0
votes
1 answer

How is a Cobra Flag of type `StringToStringVar` access using Viper?

I am trying to develop an application in Go that accepts inputs on the command line as a string of key value pairs. To do this I am using StrngToStringVar from the Cobra library. I am also using Viper to bind these flags to configuration, however…
Russell Seymour
  • 1,333
  • 1
  • 16
  • 35
0
votes
0 answers

Golang - set environment variable from inside COBRA command

In vanilla go, it seems pretty trivial to set an environment variable. The os library has a Setenv function that should get the job done. I'm running into trouble doing that inside of a cobra command. Is there a simple way that I'm missing, or is…
distortedsignal
  • 370
  • 5
  • 17
0
votes
1 answer

How to get 2 flags from a make command cobra

how do i create a makefile that takes in 2 arguments? myapp written in go, uses cobra cli. has a command that takes in 2 arguments(flags). this works $ go build; myapp mycmd --flag1=myvalue1 --flag2=myvalue2 in my make file i have //makefile run: …
not_fubar_yet
  • 194
  • 15
0
votes
1 answer

how to have each cobra command parse its own flags? getting initialization loop (expected, but how to avoid?)

I'm following the guide on https://github.com/spf13/cobra#flags, but I'm confused by some of the content there. I have a few services (rest api, email service, events) and I'm trying to do something like this: go run *.go rest -env DEV -p 3000 go…
a person
  • 1,518
  • 3
  • 17
  • 26
0
votes
1 answer

How to extract registered information?

I'm using cobra with my Golang application. How can I get the list of commands and values that I have registered with Cobra. If I add a root command and then a DisplayName command. var Name = "sample_" var rootCmd = &cobra.Command{Use: "Use help to…
bharat nc
  • 129
  • 2
  • 7
0
votes
1 answer

error adding golang cobra package with glide

My glide.yaml import section is import: - package: github.com/spf13/cobra I have import ( "github.com/spf13/cobra" ) in my code. but I am getting the error vendor/github.com/spf13/cobra/command.go:1092: c.lflags.SortFlags undefined (type…
Amal
  • 545
  • 6
  • 19
0
votes
1 answer

How to accept input in a single line using cobra library in Go

I am writing a code in go language using cobra, currently the input Im giving is : Calc add Enter the Number of inputs 2 Enter the Numbers 2 4 Output: Sum is : 6 In this those who are…
Manu
  • 77
  • 2
  • 11
1 2 3
8
9