Skip to content

Commit a4aecb2

Browse files
committed
v0.1.0 teams features for lcl
1 parent caba8dc commit a4aecb2

File tree

83 files changed

+6225
-3760
lines changed

Some content is hidden

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

83 files changed

+6225
-3760
lines changed

api/apitest/apitest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s *Server) IsProxy() bool {
5050
}
5151

5252
func (s *Server) RecreateUser(username string) error {
53-
if !s.IsProxy() {
53+
if s.IsMock() {
5454
return nil
5555
}
5656

auth/testdata/TestCmdAuth/--help.golden

+5
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ Available Commands:
1212
Flags:
1313
-h, --help help for auth
1414

15+
Global Flags:
16+
--api-token string Anchor API personal access token (PAT).
17+
--config string Service configuration file. (default "anchor.toml")
18+
--skip-config Skip loading configuration file.
19+
1520
Use "anchor auth [command] --help" for more information about a command.

auth/testdata/TestCmdAuth/auth.golden

+5
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ Available Commands:
1212
Flags:
1313
-h, --help help for auth
1414

15+
Global Flags:
16+
--api-token string Anchor API personal access token (PAT).
17+
--config string Service configuration file. (default "anchor.toml")
18+
--skip-config Skip loading configuration file.
19+
1520
Use "anchor auth [command] --help" for more information about a command.

auth/testdata/TestCmdAuthSignin/--help.golden

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ Usage:
88

99
Flags:
1010
-h, --help help for signin
11+
12+
Global Flags:
13+
--api-token string Anchor API personal access token (PAT).
14+
--config string Service configuration file. (default "anchor.toml")
15+
--skip-config Skip loading configuration file.

auth/testdata/TestCmdAuthSignout/--help.golden

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ Usage:
88

99
Flags:
1010
-h, --help help for signout
11+
12+
Global Flags:
13+
--api-token string Anchor API personal access token (PAT).
14+
--config string Service configuration file. (default "anchor.toml")
15+
--skip-config Skip loading configuration file.

auth/testdata/TestCmdAuthWhoAmI/--help.golden

+5
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ Usage:
55

66
Flags:
77
-h, --help help for whoami
8+
9+
Global Flags:
10+
--api-token string Anchor API personal access token (PAT).
11+
--config string Service configuration file. (default "anchor.toml")
12+
--skip-config Skip loading configuration file.

cli.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/anchordotdev/cli/models"
14-
"github.com/anchordotdev/cli/stacktrace"
15-
"github.com/anchordotdev/cli/ui"
1613
"github.com/cli/browser"
14+
"github.com/mcuadros/go-defaults"
1715
"github.com/spf13/cobra"
1816
"github.com/spf13/pflag"
17+
18+
"github.com/anchordotdev/cli/models"
19+
"github.com/anchordotdev/cli/stacktrace"
20+
"github.com/anchordotdev/cli/ui"
1921
)
2022

2123
var Executable string
@@ -48,22 +50,49 @@ func VersionString() string {
4850
return fmt.Sprintf("%s (%s/%s) Commit: %s BuildDate: %s", Version.Version, Version.Os, Version.Arch, Version.Commit, Version.Date)
4951
}
5052

53+
var Defaults = defaultConfig()
54+
55+
func defaultConfig() *Config {
56+
var cfg Config
57+
defaults.SetDefaults(&cfg)
58+
return &cfg
59+
}
60+
5161
type UI struct {
5262
RunTUI func(context.Context, *ui.Driver) error
5363
}
5464

55-
type ContextKey string
65+
type contextKey int
66+
67+
const (
68+
configKey contextKey = iota
69+
calledAsKey
70+
)
71+
72+
func CalledAsFromContext(ctx context.Context) string {
73+
if calledAs, ok := ctx.Value(calledAsKey).(string); ok {
74+
return calledAs
75+
}
76+
return ""
77+
}
5678

5779
func ConfigFromContext(ctx context.Context) *Config {
58-
return ctx.Value(ContextKey("Config")).(*Config)
80+
if v := ctx.Value(configKey); v != nil {
81+
return v.(*Config)
82+
}
83+
return nil
5984
}
6085

6186
func ConfigFromCmd(cmd *cobra.Command) *Config {
6287
return ConfigFromContext(cmd.Context())
6388
}
6489

90+
func ContextWithCalledAs(ctx context.Context, calledAs string) context.Context {
91+
return context.WithValue(ctx, calledAsKey, calledAs)
92+
}
93+
6594
func ContextWithConfig(ctx context.Context, cfg *Config) context.Context {
66-
return context.WithValue(ctx, ContextKey("Config"), cfg)
95+
return context.WithValue(ctx, configKey, cfg)
6796
}
6897

6998
func ReportError(ctx context.Context, err error, drv *ui.Driver, cmd *cobra.Command, args []string) {

clitest/clitest.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package clitest
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
"testing/fstest"
7+
)
8+
9+
type TestFS fstest.MapFS
10+
11+
func (t TestFS) Open(name string) (fs.File, error) { return (fstest.MapFS)(t).Open(name) }
12+
func (t TestFS) ReadDir(name string) ([]fs.DirEntry, error) { return (fstest.MapFS)(t).ReadDir(name) }
13+
func (t TestFS) Stat(name string) (fs.FileInfo, error) { return (fstest.MapFS)(t).Stat(name) }
14+
15+
func (t TestFS) WriteFile(name string, data []byte, perm os.FileMode) error {
16+
t[name] = &fstest.MapFile{
17+
Data: data,
18+
Mode: perm,
19+
}
20+
21+
return nil
22+
}

cmd.go

+43-25
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import (
55
"errors"
66

77
"github.com/MakeNowJust/heredoc"
8+
"github.com/anchordotdev/cli/models"
89
"github.com/anchordotdev/cli/stacktrace"
910
"github.com/anchordotdev/cli/ui"
10-
"github.com/joeshaw/envdecode"
11-
"github.com/mcuadros/go-defaults"
1211
"github.com/spf13/cobra"
1312
)
1413

1514
type CmdDef struct {
1615
Name string
1716

18-
Use string
19-
Args cobra.PositionalArgs
20-
Short string
21-
Long string
17+
Aliases []string
18+
Args cobra.PositionalArgs
19+
Long string
20+
Short string
21+
Use string
2222

2323
SubDefs []CmdDef
2424
}
@@ -98,18 +98,19 @@ var rootDef = CmdDef{
9898
Short: "Audit lcl.host HTTPS Local Development Environment",
9999
},
100100
{
101-
Name: "clean",
101+
Name: "bootstrap",
102102

103-
Use: "clean [flags]",
104-
Args: cobra.NoArgs,
105-
Short: "Clean lcl.host CA Certificates from the Local Trust Store(s)",
103+
Aliases: []string{"config"},
104+
Use: "bootstrap [flags]",
105+
Args: cobra.NoArgs,
106+
Short: "Initial System Configuration for lcl.host Local Development",
106107
},
107108
{
108-
Name: "config",
109+
Name: "clean",
109110

110-
Use: "config [flags]",
111+
Use: "clean [flags]",
111112
Args: cobra.NoArgs,
112-
Short: "Configure System for lcl.host Local Development",
113+
Short: "Clean lcl.host CA Certificates from the Local Trust Store(s)",
113114
},
114115
{
115116
Name: "env",
@@ -132,6 +133,13 @@ var rootDef = CmdDef{
132133
Args: cobra.NoArgs,
133134
Short: "Setup lcl.host Application",
134135
},
136+
{
137+
Name: "trust",
138+
139+
Use: "trust [flags]",
140+
Args: cobra.NoArgs,
141+
Short: "Install CA Certificates for lcl.host Local Development",
142+
},
135143
},
136144
},
137145
{
@@ -232,33 +240,37 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
232240
}
233241

234242
constructor := func() *cobra.Command {
235-
cfg := new(Config)
236-
defaults.SetDefaults(cfg)
237-
if err := envdecode.Decode(cfg); err != nil && err != envdecode.ErrNoTargetFieldsAreSet {
238-
panic(err)
239-
}
240-
241243
cmd := &cobra.Command{
242-
Use: def.Use,
244+
Aliases: def.Aliases,
243245
Args: def.Args,
244-
Short: def.Short,
245246
Long: def.Long,
247+
Short: def.Short,
248+
Use: def.Use,
246249
SilenceUsage: true,
247250
}
248251

249-
ctx := ContextWithConfig(context.Background(), cfg)
250-
cmd.SetContext(ctx)
251-
252252
cmd.SetErrPrefix(ui.Danger("Error!"))
253253

254+
ctx := ContextWithConfig(context.Background(), defaultConfig())
255+
cmd.SetContext(ctx)
256+
254257
fn(cmd)
255258

256259
cmd.RunE = func(cmd *cobra.Command, args []string) (returnedError error) {
257-
cfg := ConfigFromCmd(cmd)
260+
ctx := cmd.Context()
261+
262+
cfg := new(Config)
263+
if err := cfg.Load(ctx); err != nil {
264+
return err
265+
}
266+
258267
if cfg.Test.SkipRunE {
259268
return nil
260269
}
261270

271+
ctx = ContextWithConfig(cmd.Context(), cfg)
272+
cmd.SetContext(ctx)
273+
262274
var t T
263275

264276
switch any(t).(type) {
@@ -270,6 +282,8 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
270282
ctx, cancel := context.WithCancelCause(cmd.Context())
271283
defer cancel(nil)
272284

285+
ctx = ContextWithCalledAs(ctx, cmd.CalledAs())
286+
273287
drv, prg := ui.NewDriverTUI(ctx)
274288
defer func() {
275289
// release/restore
@@ -288,6 +302,10 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
288302
errc <- err
289303
}()
290304

305+
if cfg.TOML != nil {
306+
drv.Activate(ctx, models.ConfigLoaded(cfg.File.Path))
307+
}
308+
291309
if err := stacktrace.CapturePanic(func() error { return t.UI().RunTUI(ctx, drv) }); err != nil {
292310
var uierr ui.Error
293311
if errors.As(err, &uierr) {

cmdtest/cmdtest.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@ package cmdtest
22

33
import (
44
"bytes"
5+
"context"
56
"io"
67
"testing"
78

8-
"github.com/anchordotdev/cli"
9-
"github.com/anchordotdev/cli/ui/uitest"
109
"github.com/joeshaw/envdecode"
1110
"github.com/spf13/cobra"
1211
"github.com/stretchr/testify/require"
12+
13+
"github.com/anchordotdev/cli"
14+
"github.com/anchordotdev/cli/clitest"
15+
"github.com/anchordotdev/cli/ui/uitest"
1316
)
1417

18+
func Config(ctx context.Context) *cli.Config {
19+
cfg := new(cli.Config)
20+
cfg.Test.SystemFS = clitest.TestFS{}
21+
if err := cfg.Load(ctx); err != nil {
22+
panic(err)
23+
}
24+
return cfg
25+
}
26+
1527
func TestCfg(t *testing.T, cmd *cobra.Command, args ...string) *cli.Config {
1628
cmd = cli.NewTestCmd(cmd)
1729
cfg := cli.ConfigFromCmd(cmd)

component/selector_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestSelector(t *testing.T) {
8585

8686
cfg.Test.Prefer = map[string]cli.ConfigTestPrefer{
8787
"/v0/orgs": {
88-
Example: "double",
88+
Example: "anky_team",
8989
},
9090
}
9191
ctx = cli.ContextWithConfig(ctx, cfg)
@@ -127,8 +127,12 @@ func TestSelector(t *testing.T) {
127127

128128
org := <-choicec
129129

130-
if want, got := "second-org-slug", org.Slug; want != got {
131-
errc <- fmt.Errorf("Want org choice: %q, Got: %q", want, got)
130+
if want, got := "ankyco", org.Slug; want != got {
131+
t.Errorf("Want org choice: %q, Got: %q", want, got)
132+
}
133+
134+
if err := <-errc; err != nil {
135+
t.Error(err)
132136
}
133137

134138
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second*3))

component/testdata/TestSelector/orgs_double.golden

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Fetching organizations…⠋
33
─── Selector[github.com/anchordotdev/cli/api.Organization] ─────────────────────
44
? Which organization do you want for this test?
5-
> First Org Slug (first-org-slug)
6-
Second Org Slug (second-org-slug)
5+
> [email protected] (ankydotdev)
6+
AnkyCo (ankyco)
77
─── Selector[github.com/anchordotdev/cli/api.Organization] ─────────────────────
88
? Which organization do you want for this test?
9-
First Org Slug (first-org-slug)
10-
> Second Org Slug (second-org-slug)
9+
[email protected] (ankydotdev)
10+
> AnkyCo (ankyco)
1111
─── Selector[github.com/anchordotdev/cli/api.Organization] ─────────────────────
12-
- Selected second-org-slug organization. You can also use `--org second-org-slug`.
12+
- Selected ankyco organization. You can also use `--org ankyco`.

0 commit comments

Comments
 (0)