Skip to content

Commit

Permalink
v0.0.21: nss detection fix and misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Apr 11, 2024
1 parent c11d743 commit f26a350
Show file tree
Hide file tree
Showing 50 changed files with 769 additions and 218 deletions.
11 changes: 11 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package auth

import (
"github.com/spf13/cobra"

"github.com/anchordotdev/cli"
)

var CmdAuth = cli.NewCmd[cli.ShowHelp](cli.CmdRoot, "auth", func(cmd *cobra.Command) {
cmd.Args = cobra.NoArgs
})
18 changes: 14 additions & 4 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"testing"

"github.com/anchordotdev/cli/api/apitest"
)

var (
_ = flag.Bool("update", false, "ignored")
"github.com/anchordotdev/cli/cmdtest"
)

var srv = &apitest.Server{
Expand All @@ -27,3 +24,16 @@ func TestMain(m *testing.M) {

m.Run()
}

func TestCmdAuth(t *testing.T) {
cmd := CmdAuth
root := cmd.Root()

t.Run("auth", func(t *testing.T) {
cmdtest.TestOutput(t, root, "auth")
})

t.Run("--help", func(t *testing.T) {
cmdtest.TestOutput(t, root, "auth", "--help")
})
}
10 changes: 5 additions & 5 deletions auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
)

type Client struct {
Config *cli.Config

Anc *api.Session
Hint tea.Model
Source string
}

func (c Client) Perform(ctx context.Context, drv *ui.Driver) (*api.Session, error) {
cfg := cli.ConfigFromContext(ctx)

var newClientErr, userInfoErr error

drv.Activate(ctx, &models.Client{})

if c.Anc == nil {
c.Anc, newClientErr = api.NewClient(c.Config)
c.Anc, newClientErr = api.NewClient(cfg)
if newClientErr != nil && !errors.Is(newClientErr, api.ErrSignedOut) {
return nil, newClientErr
}
Expand All @@ -47,16 +47,16 @@ func (c Client) Perform(ctx context.Context, drv *ui.Driver) (*api.Session, erro
c.Hint = &models.SignInHint{}
}
cmd := &SignIn{
Config: c.Config,
Hint: c.Hint,
Source: c.Source,
}
ctx = cli.ContextWithConfig(ctx, cfg)
err := cmd.RunTUI(ctx, drv)
if err != nil {
return nil, err
}

c.Anc, err = api.NewClient(c.Config)
c.Anc, err = api.NewClient(cfg)
if err != nil {
return nil, err
}
Expand Down
35 changes: 22 additions & 13 deletions auth/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cli/browser"
"github.com/mattn/go-isatty"
"github.com/muesli/termenv"
"github.com/spf13/cobra"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/api"
Expand All @@ -22,12 +23,14 @@ import (
)

var (
CmdAuthSignin = cli.NewCmd[SignIn](CmdAuth, "signin", func(cmd *cobra.Command) {
cmd.Args = cobra.NoArgs
})

ErrSigninFailed = errors.New("sign in failed")
)

type SignIn struct {
Config *cli.Config

Source string

Hint tea.Model
Expand All @@ -41,14 +44,16 @@ func (s SignIn) UI() cli.UI {
}

func (s *SignIn) runTTY(ctx context.Context, tty termenv.File) error {
cfg := cli.ConfigFromContext(ctx)

output := termenv.DefaultOutput()
cp := output.ColorProfile()

fmt.Fprintln(tty,
output.String("# Run `anchor auth signin`").Bold(),
)

anc, err := api.NewClient(s.Config)
anc, err := api.NewClient(cfg)
if err != nil && err != api.ErrSignedOut {
return err
}
Expand Down Expand Up @@ -114,15 +119,15 @@ func (s *SignIn) runTTY(ctx context.Context, tty termenv.File) error {
time.Sleep(time.Duration(codes.Interval) * time.Second)
}
}
s.Config.API.Token = patToken
cfg.API.Token = patToken

userInfo, err := fetchUserInfo(s.Config)
userInfo, err := fetchUserInfo(cfg)
if err != nil {
return err
}

kr := keyring.Keyring{Config: s.Config}
if err := kr.Set(keyring.APIToken, s.Config.API.Token); err != nil {
kr := keyring.Keyring{Config: cfg}
if err := kr.Set(keyring.APIToken, cfg.API.Token); err != nil {
return err
}

Expand All @@ -136,15 +141,19 @@ func (s *SignIn) runTTY(ctx context.Context, tty termenv.File) error {
return nil
}

// FIXME: dedup mostly identical RunTUI/RunTTY

func (s *SignIn) RunTUI(ctx context.Context, drv *ui.Driver) error {
cfg := cli.ConfigFromContext(ctx)

drv.Activate(ctx, &models.SignInHeader{})

if s.Hint == nil {
s.Hint = &models.SignInHint{}
}
drv.Activate(ctx, s.Hint)

anc, err := api.NewClient(s.Config)
anc, err := api.NewClient(cfg)
if err != nil && err != api.ErrSignedOut {
return err
}
Expand All @@ -165,7 +174,7 @@ func (s *SignIn) RunTUI(ctx context.Context, drv *ui.Driver) error {
VerificationURL: codes.VerificationUri,
})

if !s.Config.NonInteractive {
if !cfg.NonInteractive {
select {
case <-confirmc:
case <-ctx.Done():
Expand All @@ -189,15 +198,15 @@ func (s *SignIn) RunTUI(ctx context.Context, drv *ui.Driver) error {
time.Sleep(time.Duration(codes.Interval) * time.Second)
}
}
s.Config.API.Token = patToken
cfg.API.Token = patToken

userInfo, err := fetchUserInfo(s.Config)
userInfo, err := fetchUserInfo(cfg)
if err != nil {
return err
}

kr := keyring.Keyring{Config: s.Config}
if err := kr.Set(keyring.APIToken, s.Config.API.Token); err != nil {
kr := keyring.Keyring{Config: cfg}
if err := kr.Set(keyring.APIToken, cfg.API.Token); err != nil {
return err
}

Expand Down
11 changes: 11 additions & 0 deletions auth/signin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ package auth

import (
"testing"

"github.com/anchordotdev/cli/cmdtest"
)

func TestCmdAuthSignin(t *testing.T) {
cmd := CmdAuthSignin
root := cmd.Root()

t.Run("--help", func(t *testing.T) {
cmdtest.TestOutput(t, root, "auth", "signin", "--help")
})
}

func TestSignIn(t *testing.T) {
t.Run("cli-auth-success", func(t *testing.T) {
t.Skip("cli auth test not yet implemented")
Expand Down
13 changes: 9 additions & 4 deletions auth/signout.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"github.com/anchordotdev/cli/auth/models"
"github.com/anchordotdev/cli/keyring"
"github.com/anchordotdev/cli/ui"
"github.com/spf13/cobra"
)

type SignOut struct {
Config *cli.Config
}
var CmdAuthSignout = cli.NewCmd[SignOut](CmdAuth, "signout", func(cmd *cobra.Command) {
cmd.Args = cobra.NoArgs
})

type SignOut struct{}

func (s SignOut) UI() cli.UI {
return cli.UI{
Expand All @@ -20,9 +23,11 @@ func (s SignOut) UI() cli.UI {
}

func (s *SignOut) runTUI(ctx context.Context, drv *ui.Driver) error {
cfg := cli.ConfigFromContext(ctx)

drv.Activate(ctx, &models.SignOutPreamble{})

kr := keyring.Keyring{Config: s.Config}
kr := keyring.Keyring{Config: cfg}
err := kr.Delete(keyring.APIToken)

if err == nil {
Expand Down
42 changes: 42 additions & 0 deletions auth/signout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package auth

import (
"context"
"testing"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/cmdtest"
"github.com/anchordotdev/cli/ui/uitest"
)

func TestCmdAuthSignout(t *testing.T) {
cmd := CmdAuthSignin
cfg := cli.ConfigFromCmd(cmd)
cfg.Test.SkipRunE = true
root := cmd.Root()

t.Run("--help", func(t *testing.T) {
cmdtest.TestOutput(t, root, "auth", "signout", "--help")
})
}

func TestSignout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cfg := new(cli.Config)
cfg.Keyring.MockMode = true
ctx = cli.ContextWithConfig(ctx, cfg)

t.Run("signed-out", func(t *testing.T) {
uitest.TestTUIError(ctx, t, new(SignOut).UI(), "secret not found in keyring")
})

t.Run("signed-in", func(t *testing.T) {
t.Skip("pending singleton keyring")
// kr := keyring.Keyring{}
// if err := kr.Set(keyring.APIToken, "secret"); err != nil {
// t.Fatal(err)
// }
})
}
15 changes: 15 additions & 0 deletions auth/testdata/TestCmdAuth/--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Manage Anchor.dev Authentication

Usage:
anchor auth <subcommand> [flags]
anchor auth [command]

Available Commands:
signin Authenticate with your account
signout Invalidate your local Anchor account session
whoami Identify current account

Flags:
-h, --help help for auth

Use "anchor auth [command] --help" for more information about a command.
15 changes: 15 additions & 0 deletions auth/testdata/TestCmdAuth/auth.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Manage Anchor.dev Authentication

Usage:
anchor auth <subcommand> [flags]
anchor auth [command]

Available Commands:
signin Authenticate with your account
signout Invalidate your local Anchor account session
whoami Identify current account

Flags:
-h, --help help for auth

Use "anchor auth [command] --help" for more information about a command.
10 changes: 10 additions & 0 deletions auth/testdata/TestCmdAuthSignin/--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Sign into your Anchor account for your local system user.

Generate a new Personal Access Token (PAT) and store it in the system keychain
for the local system user.

Usage:
anchor auth signin [flags]

Flags:
-h, --help help for signin
10 changes: 10 additions & 0 deletions auth/testdata/TestCmdAuthSignout/--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Sign out of your Anchor account for your local system user.

Remove your Personal Access Token (PAT) from the system keychain for your local
system user.

Usage:
anchor auth signout [flags]

Flags:
-h, --help help for signout
7 changes: 7 additions & 0 deletions auth/testdata/TestCmdAuthWhoAmI/--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Print the details of the Anchor account for your local system user.

Usage:
anchor auth whoami [flags]

Flags:
-h, --help help for whoami
13 changes: 9 additions & 4 deletions auth/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"net/http"

"github.com/muesli/termenv"
"github.com/spf13/cobra"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/api"
)

type WhoAmI struct {
Config *cli.Config
}
var CmdAuthWhoami = cli.NewCmd[WhoAmI](CmdAuth, "whoami", func(cmd *cobra.Command) {
cmd.Args = cobra.NoArgs
})

type WhoAmI struct{}

func (w WhoAmI) UI() cli.UI {
return cli.UI{
Expand All @@ -24,7 +27,9 @@ func (w WhoAmI) UI() cli.UI {
}

func (w *WhoAmI) run(ctx context.Context, tty termenv.File) error {
anc, err := api.NewClient(w.Config)
cfg := cli.ConfigFromContext(ctx)

anc, err := api.NewClient(cfg)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit f26a350

Please sign in to comment.