Cobra init not working,: Command not found
Asked Answered
C

4

13

I am following this guide.

***@pop-os:~/go/src/foo$ cobra init --pkg-name foo
Command 'cobra' not found, but can be installed with:
sudo apt install cobra

My setup:

I have go 1.16 installed

$ go version 
go version go1.16.3 linux/amd64

my $GOPATH is set to ~/go

$ go env
...
GOPATH="/home/***/go"

What I have done: I used the command

$ go get -u github.com/spf13/cobra/cobra

In my bin's dir I now find cobra

***@pop-os:~/go/bin$ ls
cobra
...

What am I doing wrong?

Conceptualism answered 5/4, 2021 at 11:44 Comment(2)
easiest thing to do is just create an alias in .zshrc or .bashrc alias cobra="~/go/bin/cobra" since it is already in the pathStateroom
For anyone that's coming here with no issue in establishing their path environment variable or with pointing to the binary - simply with the binary installing. If you happened to have an issue with cannot find package "github.com/hashicorp/hcl/hcl/printer, then simply enable Go modules and try again via env GO111MODULE=on go get github.com/spf13/cobra/cobra.Transfuse
T
29

Use

go mod init <MODNAME>
~/go/bin/cobra-cli init

or keep reading to learn how to to make the cobra-cli command available on the command line.

This seems to be an issue of $PATH configuration. Because the cobra-cli command is not found, the path ~/go/bin is not part of the $PATH variable. One can add the path like this:

export PATH="~/go/bin:$PATH"

and then use the cobra-cli command on the command-line. One can add that export ... command to ~/.bashrc or a similar file to have the PATH configured properly on startup.

Tade answered 5/4, 2021 at 11:47 Comment(2)
Thank you, explaining what is wrong is so helpful. The concept of $PATH is still not familiar to me. I will try to learn more about it.Conceptualism
Thank you, most times it's a path issue.Omalley
S
7

The cobra command is now become cobra-cli. see https://github.com/spf13/cobra#usage

Saritasarkaria answered 27/5, 2022 at 15:54 Comment(0)
D
0

had same issue, it is on path but still same. The solution that worked for me was updating my .bashrc file

nano ~/.bashrc

add this alias in this file

alias cobra-cli="~/go/bin/cobra-cli"

you might want to reload your terminal. run this command to check if it works

cobra-cli init

you should have a response different from what you had before

Defamatory answered 23/7 at 13:44 Comment(0)
L
-1

For Mac

`nano ~/.zshrc`
alias cobra-cli="~/go/bin/cobra-cli"
Loretaloretta answered 25/8 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.