Skip to content

Commit

Permalink
Improve infra version when not logged in
Browse files Browse the repository at this point in the history
- Do not log being unauthenticated as an error
- Flush the output no matter what
  • Loading branch information
BruceMacD committed Aug 5, 2021
1 parent 3affc54 commit 9786d62
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions internal/cmd/cmd.go
Expand Up @@ -51,6 +51,12 @@ type Config struct {
SkipTLSVerify bool `json:"skip-tls-verify"`
}

type ErrUnauthenticated struct{}

func (e *ErrUnauthenticated) Error() string {
return "could not read local credentials. Are you logged in? To login, use \"infra login\""
}

func readConfig() (config *Config, err error) {
config = &Config{}

Expand All @@ -61,7 +67,7 @@ func readConfig() (config *Config, err error) {

contents, err := ioutil.ReadFile(filepath.Join(homeDir, ".infra", "config"))
if os.IsNotExist(err) {
return nil, errors.New("could not read local credentials. Are you logged in? To login, use \"infra login\"")
return nil, &ErrUnauthenticated{}
}

if err != nil {
Expand Down Expand Up @@ -854,26 +860,31 @@ var versionCmd = &cobra.Command{
Short: "Display the Infra build version",
RunE: func(cmd *cobra.Command, args []string) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
defer w.Flush()
fmt.Fprintln(w)
fmt.Fprintln(w, "Client:\t", version.Version)

client, close, err := clientFromConfig()
if err != nil {
return err
switch err.(type) {
case *ErrUnauthenticated:
fmt.Fprintln(w, "Registry:\t", "not connected")
return nil
default:
return err
}
}
defer close()

// Note that we use the client to get this version, but it is in fact the server version
res, err := client.Version(context.Background(), &emptypb.Empty{})
if err != nil {
fmt.Fprintln(w, blue("✕")+" Could not retrieve registry version")
w.Flush()
return err
}

fmt.Fprintln(w, "Registry:\t", res.Version)
fmt.Fprintln(w)
w.Flush()

return nil
},
Expand Down

0 comments on commit 9786d62

Please sign in to comment.