10

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?

herrwolken
  • 389
  • 1
  • 4
  • 12
  • easiest thing to do is just create an alias in .zshrc or .bashrc `alias cobra="~/go/bin/cobra"` since it is already in the path – Spencer Davis Jun 20 '21 at 18:01
  • 2
    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`. – Rik Aug 20 '21 at 02:32

2 Answers2

21

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.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
jkr
  • 17,119
  • 2
  • 42
  • 68
5

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

Mahesh
  • 1,583
  • 13
  • 18