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)
…
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…
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…
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
│ ├──…
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…
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…
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…
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…
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…
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…
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…
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")
…
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…
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…
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.
…