Skip to content

Commit

Permalink
Print helpful text to know how to login (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahlunar authored Jan 2, 2024
1 parent 510ac04 commit a354376
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.19.0
go-version: 1.21.4
- name: Cache Go modules
uses: actions/cache@v3
with:
Expand Down
11 changes: 11 additions & 0 deletions cmd/hamctl/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/lunarway/release-manager/cmd/hamctl/command"
"github.com/lunarway/release-manager/internal/http"
)

var (
Expand All @@ -17,8 +19,17 @@ func main() {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
c.SilenceUsage = true
c.SilenceErrors = true

err = c.Execute()
if err != nil {
if errors.Is(err, http.ErrLoginRequired) {
fmt.Printf("You are not logged in. To log in, please run the following command:\n 'hamctl login'\n")
} else {
fmt.Printf("Error: %v\n", err)
}

os.Exit(1)
}
}
7 changes: 6 additions & 1 deletion internal/http/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -13,6 +14,10 @@ import (
"golang.org/x/oauth2/clientcredentials"
)

var (
ErrLoginRequired = errors.New("login required")
)

type UserAuthenticator struct {
conf *oauth2.Config
}
Expand Down Expand Up @@ -52,7 +57,7 @@ func (g *UserAuthenticator) Login(ctx context.Context) error {
func (g *UserAuthenticator) Access(ctx context.Context) (*http.Client, error) {
token, err := readAccessToken()
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %w", ErrLoginRequired, err)
}
return g.conf.Client(ctx, token), nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *Client) Do(method string, path string, requestBody, responseBody interf
ctx := context.Background()
client, err := c.Auth.Access(ctx)
if err != nil {
return errors.Wrap(err, "please log in again to refresh the token")
return err
}
client.Timeout = c.Timeout

Expand Down

0 comments on commit a354376

Please sign in to comment.