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
2
votes
1 answer

Can't add new cobra CLI command when the file is inside a folder

I am using cobra to build CLI. I want to create a new command called config that will be inside the file config.go and the file inside the folder proxy. This is the structure: MyProject ├── cmd | ├── proxy | | └── config.go | └──…
E235
  • 11,560
  • 24
  • 91
  • 141
2
votes
2 answers

Debug file other than main.go in VS Code

I am writing a CLI in go using VS code editor. I am not able to figure out how to debug a code section. My directory structure is : - test - main.go - cmd - login.go - root.go I have set breakpoints in login.go but if I run "Start…
Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35
2
votes
0 answers

What's the proper way to wrap grpc client behind cobra command?

I am implementing a GRPC client using cobra. Different service calls are behind the sub-commands. To avoid code duplication, I keep a single connection and single client in the viper singleton. But I am not sure that's the proper way to do…
nos
  • 19,875
  • 27
  • 98
  • 134
2
votes
1 answer

GO: Run cli command with wrong args

I use cobra to create CLI command tool. everything is looking OK except the error handling what I want that if command was sent by mistake (wrong args or wrong input) return std.err instead of std.out to simplify the secnario I've created this which…
user6124024
2
votes
4 answers

How to provide Go cmd app as productive app

I’m creating the following repo which use Go command line tools cobra to generate some artifacts , while run the command go run main.go mzr convert toJSON ./ the program take the yaml file and generate from it json file. Now I want that my repo…
user8557463
2
votes
2 answers

How to make subcommand required in Go CLI using cobra

I am using Cobra to make some cli updated to my app. I want to make this command required, meaning the application should fail if it doesn't find the argument it is looking for. package commands import ( "github.com/spf13/cobra" "errors" …
wayfare
  • 1,802
  • 5
  • 20
  • 37
1
vote
0 answers

How to debug cobra command test with VS Code?

I have a go program using cobra commands. I can run and debug a command with { "name": "Reporting1", "type": "go", "request": "launch", "mode": "auto", "program": "main.go", "args":["Reporting1"], "console":…
eckard
  • 11
  • 3
1
vote
1 answer

Getting "Error: unknown shorthand flag: 'o' in -o" in a CLI Project with Go

I'm currently working on a CLI project using Go and cobra. I've completed the code and now I built the project and trying to run .However, I'm encountering an error that I can't seem to resolve. The error message I'm getting is: Error: unknown…
1
vote
1 answer

How to write testable commands for app using cobra and viper

Summary:- I have a cobra-cli based golang app which uses viper for config management. I want to make my commands testable. For this I want to inject dependencies to my commands when they are being added to the root command (which happens in the…
abnvanand
  • 203
  • 2
  • 6
1
vote
1 answer

How to combine cobra and klog

I have a project that needs to use cobra and klog to generate executable and print logs and keep. Firstly, I tested that using the following klog can output the log to the terminal and file at the same time. package main import ( "flag" …
moluzhui
  • 1,003
  • 14
  • 34
1
vote
1 answer

Golang viper pass flags from config

I have written a cli tool for use within my org, to make connecting to certain DB's easier. Not sure how to word this question, so an example will hopefully explain. I would like to use a config like so: databases: db1: db1.endpoint.company.org …
fraserc182
  • 237
  • 1
  • 4
  • 13
1
vote
0 answers

I want to retain the current session when I am building cli based application in golang

I am building CLI based application in golang for automation purpose for which I will be using cobra. Here user needs to login by supplying id, password and server name then golang will return client object and then that returned object will be used…
Varun
  • 23
  • 7
1
vote
0 answers

cobra command Flag value manipulation

I want to validate my int64 flag to make sure that its not being recognised as a octal number (the value must not start with 0, if it does, I should be able to trim the leading 0 before assigning the value to the flag). This is how I create the…
ravi kumar
  • 1,548
  • 1
  • 13
  • 47
1
vote
0 answers

How can I pass a a modified Command value to the Help() function of Cobra?

I'd like to be able to take the Long value of a Command from Cobra and modify it before it's returned to the user when [program] [command] --help is called. While there's ample documentation, Cobra lacks examples to illustrate ways of modifying…
robdominguez
  • 118
  • 7
1
vote
0 answers

Is there a way to reduce the binary size of a Cobra app

I'd like to use Cobra in a project where I have some strict binary size limitations. The binary size of a "hello world" Cobra app (as shown below) comes in at 4.1 MB. If I build it with go build -ldflags "-w -s" . then I can get it down to 2.8 MB.…
Sam Herrmann
  • 6,293
  • 4
  • 31
  • 50
1 2 3
8 9