Skip to content

Commit f26a350

Browse files
committed
v0.0.21: nss detection fix and misc cleanup
1 parent c11d743 commit f26a350

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+769
-218
lines changed

auth/auth.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package auth
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/anchordotdev/cli"
7+
)
8+
9+
var CmdAuth = cli.NewCmd[cli.ShowHelp](cli.CmdRoot, "auth", func(cmd *cobra.Command) {
10+
cmd.Args = cobra.NoArgs
11+
})

auth/auth_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import (
66
"testing"
77

88
"github.com/anchordotdev/cli/api/apitest"
9-
)
10-
11-
var (
12-
_ = flag.Bool("update", false, "ignored")
9+
"github.com/anchordotdev/cli/cmdtest"
1310
)
1411

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

2825
m.Run()
2926
}
27+
28+
func TestCmdAuth(t *testing.T) {
29+
cmd := CmdAuth
30+
root := cmd.Root()
31+
32+
t.Run("auth", func(t *testing.T) {
33+
cmdtest.TestOutput(t, root, "auth")
34+
})
35+
36+
t.Run("--help", func(t *testing.T) {
37+
cmdtest.TestOutput(t, root, "auth", "--help")
38+
})
39+
}

auth/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import (
1212
)
1313

1414
type Client struct {
15-
Config *cli.Config
16-
1715
Anc *api.Session
1816
Hint tea.Model
1917
Source string
2018
}
2119

2220
func (c Client) Perform(ctx context.Context, drv *ui.Driver) (*api.Session, error) {
21+
cfg := cli.ConfigFromContext(ctx)
22+
2323
var newClientErr, userInfoErr error
2424

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

2727
if c.Anc == nil {
28-
c.Anc, newClientErr = api.NewClient(c.Config)
28+
c.Anc, newClientErr = api.NewClient(cfg)
2929
if newClientErr != nil && !errors.Is(newClientErr, api.ErrSignedOut) {
3030
return nil, newClientErr
3131
}
@@ -47,16 +47,16 @@ func (c Client) Perform(ctx context.Context, drv *ui.Driver) (*api.Session, erro
4747
c.Hint = &models.SignInHint{}
4848
}
4949
cmd := &SignIn{
50-
Config: c.Config,
5150
Hint: c.Hint,
5251
Source: c.Source,
5352
}
53+
ctx = cli.ContextWithConfig(ctx, cfg)
5454
err := cmd.RunTUI(ctx, drv)
5555
if err != nil {
5656
return nil, err
5757
}
5858

59-
c.Anc, err = api.NewClient(c.Config)
59+
c.Anc, err = api.NewClient(cfg)
6060
if err != nil {
6161
return nil, err
6262
}

auth/signin.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/cli/browser"
1414
"github.com/mattn/go-isatty"
1515
"github.com/muesli/termenv"
16+
"github.com/spf13/cobra"
1617

1718
"github.com/anchordotdev/cli"
1819
"github.com/anchordotdev/cli/api"
@@ -22,12 +23,14 @@ import (
2223
)
2324

2425
var (
26+
CmdAuthSignin = cli.NewCmd[SignIn](CmdAuth, "signin", func(cmd *cobra.Command) {
27+
cmd.Args = cobra.NoArgs
28+
})
29+
2530
ErrSigninFailed = errors.New("sign in failed")
2631
)
2732

2833
type SignIn struct {
29-
Config *cli.Config
30-
3134
Source string
3235

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

4346
func (s *SignIn) runTTY(ctx context.Context, tty termenv.File) error {
47+
cfg := cli.ConfigFromContext(ctx)
48+
4449
output := termenv.DefaultOutput()
4550
cp := output.ColorProfile()
4651

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

51-
anc, err := api.NewClient(s.Config)
56+
anc, err := api.NewClient(cfg)
5257
if err != nil && err != api.ErrSignedOut {
5358
return err
5459
}
@@ -114,15 +119,15 @@ func (s *SignIn) runTTY(ctx context.Context, tty termenv.File) error {
114119
time.Sleep(time.Duration(codes.Interval) * time.Second)
115120
}
116121
}
117-
s.Config.API.Token = patToken
122+
cfg.API.Token = patToken
118123

119-
userInfo, err := fetchUserInfo(s.Config)
124+
userInfo, err := fetchUserInfo(cfg)
120125
if err != nil {
121126
return err
122127
}
123128

124-
kr := keyring.Keyring{Config: s.Config}
125-
if err := kr.Set(keyring.APIToken, s.Config.API.Token); err != nil {
129+
kr := keyring.Keyring{Config: cfg}
130+
if err := kr.Set(keyring.APIToken, cfg.API.Token); err != nil {
126131
return err
127132
}
128133

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

144+
// FIXME: dedup mostly identical RunTUI/RunTTY
145+
139146
func (s *SignIn) RunTUI(ctx context.Context, drv *ui.Driver) error {
147+
cfg := cli.ConfigFromContext(ctx)
148+
140149
drv.Activate(ctx, &models.SignInHeader{})
141150

142151
if s.Hint == nil {
143152
s.Hint = &models.SignInHint{}
144153
}
145154
drv.Activate(ctx, s.Hint)
146155

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

168-
if !s.Config.NonInteractive {
177+
if !cfg.NonInteractive {
169178
select {
170179
case <-confirmc:
171180
case <-ctx.Done():
@@ -189,15 +198,15 @@ func (s *SignIn) RunTUI(ctx context.Context, drv *ui.Driver) error {
189198
time.Sleep(time.Duration(codes.Interval) * time.Second)
190199
}
191200
}
192-
s.Config.API.Token = patToken
201+
cfg.API.Token = patToken
193202

194-
userInfo, err := fetchUserInfo(s.Config)
203+
userInfo, err := fetchUserInfo(cfg)
195204
if err != nil {
196205
return err
197206
}
198207

199-
kr := keyring.Keyring{Config: s.Config}
200-
if err := kr.Set(keyring.APIToken, s.Config.API.Token); err != nil {
208+
kr := keyring.Keyring{Config: cfg}
209+
if err := kr.Set(keyring.APIToken, cfg.API.Token); err != nil {
201210
return err
202211
}
203212

auth/signin_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@ package auth
22

33
import (
44
"testing"
5+
6+
"github.com/anchordotdev/cli/cmdtest"
57
)
68

9+
func TestCmdAuthSignin(t *testing.T) {
10+
cmd := CmdAuthSignin
11+
root := cmd.Root()
12+
13+
t.Run("--help", func(t *testing.T) {
14+
cmdtest.TestOutput(t, root, "auth", "signin", "--help")
15+
})
16+
}
17+
718
func TestSignIn(t *testing.T) {
819
t.Run("cli-auth-success", func(t *testing.T) {
920
t.Skip("cli auth test not yet implemented")

auth/signout.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"github.com/anchordotdev/cli/auth/models"
88
"github.com/anchordotdev/cli/keyring"
99
"github.com/anchordotdev/cli/ui"
10+
"github.com/spf13/cobra"
1011
)
1112

12-
type SignOut struct {
13-
Config *cli.Config
14-
}
13+
var CmdAuthSignout = cli.NewCmd[SignOut](CmdAuth, "signout", func(cmd *cobra.Command) {
14+
cmd.Args = cobra.NoArgs
15+
})
16+
17+
type SignOut struct{}
1518

1619
func (s SignOut) UI() cli.UI {
1720
return cli.UI{
@@ -20,9 +23,11 @@ func (s SignOut) UI() cli.UI {
2023
}
2124

2225
func (s *SignOut) runTUI(ctx context.Context, drv *ui.Driver) error {
26+
cfg := cli.ConfigFromContext(ctx)
27+
2328
drv.Activate(ctx, &models.SignOutPreamble{})
2429

25-
kr := keyring.Keyring{Config: s.Config}
30+
kr := keyring.Keyring{Config: cfg}
2631
err := kr.Delete(keyring.APIToken)
2732

2833
if err == nil {

auth/signout_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/anchordotdev/cli"
8+
"github.com/anchordotdev/cli/cmdtest"
9+
"github.com/anchordotdev/cli/ui/uitest"
10+
)
11+
12+
func TestCmdAuthSignout(t *testing.T) {
13+
cmd := CmdAuthSignin
14+
cfg := cli.ConfigFromCmd(cmd)
15+
cfg.Test.SkipRunE = true
16+
root := cmd.Root()
17+
18+
t.Run("--help", func(t *testing.T) {
19+
cmdtest.TestOutput(t, root, "auth", "signout", "--help")
20+
})
21+
}
22+
23+
func TestSignout(t *testing.T) {
24+
ctx, cancel := context.WithCancel(context.Background())
25+
defer cancel()
26+
27+
cfg := new(cli.Config)
28+
cfg.Keyring.MockMode = true
29+
ctx = cli.ContextWithConfig(ctx, cfg)
30+
31+
t.Run("signed-out", func(t *testing.T) {
32+
uitest.TestTUIError(ctx, t, new(SignOut).UI(), "secret not found in keyring")
33+
})
34+
35+
t.Run("signed-in", func(t *testing.T) {
36+
t.Skip("pending singleton keyring")
37+
// kr := keyring.Keyring{}
38+
// if err := kr.Set(keyring.APIToken, "secret"); err != nil {
39+
// t.Fatal(err)
40+
// }
41+
})
42+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Manage Anchor.dev Authentication
2+
3+
Usage:
4+
anchor auth <subcommand> [flags]
5+
anchor auth [command]
6+
7+
Available Commands:
8+
signin Authenticate with your account
9+
signout Invalidate your local Anchor account session
10+
whoami Identify current account
11+
12+
Flags:
13+
-h, --help help for auth
14+
15+
Use "anchor auth [command] --help" for more information about a command.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Manage Anchor.dev Authentication
2+
3+
Usage:
4+
anchor auth <subcommand> [flags]
5+
anchor auth [command]
6+
7+
Available Commands:
8+
signin Authenticate with your account
9+
signout Invalidate your local Anchor account session
10+
whoami Identify current account
11+
12+
Flags:
13+
-h, --help help for auth
14+
15+
Use "anchor auth [command] --help" for more information about a command.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Sign into your Anchor account for your local system user.
2+
3+
Generate a new Personal Access Token (PAT) and store it in the system keychain
4+
for the local system user.
5+
6+
Usage:
7+
anchor auth signin [flags]
8+
9+
Flags:
10+
-h, --help help for signin

0 commit comments

Comments
 (0)