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…
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…
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: …
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…
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",…
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) {
…
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…
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…
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…
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()…
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:…
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…
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…
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…