Skip to content

Commit

Permalink
v0.1.0 teams features for lcl
Browse files Browse the repository at this point in the history
  • Loading branch information
benburkert committed Aug 12, 2024
1 parent caba8dc commit a4aecb2
Show file tree
Hide file tree
Showing 83 changed files with 6,225 additions and 3,760 deletions.
2 changes: 1 addition & 1 deletion api/apitest/apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Server) IsProxy() bool {
}

func (s *Server) RecreateUser(username string) error {
if !s.IsProxy() {
if s.IsMock() {
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions auth/testdata/TestCmdAuth/--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ Available Commands:
Flags:
-h, --help help for auth

Global Flags:
--api-token string Anchor API personal access token (PAT).
--config string Service configuration file. (default "anchor.toml")
--skip-config Skip loading configuration file.

Use "anchor auth [command] --help" for more information about a command.
5 changes: 5 additions & 0 deletions auth/testdata/TestCmdAuth/auth.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ Available Commands:
Flags:
-h, --help help for auth

Global Flags:
--api-token string Anchor API personal access token (PAT).
--config string Service configuration file. (default "anchor.toml")
--skip-config Skip loading configuration file.

Use "anchor auth [command] --help" for more information about a command.
5 changes: 5 additions & 0 deletions auth/testdata/TestCmdAuthSignin/--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ Usage:

Flags:
-h, --help help for signin

Global Flags:
--api-token string Anchor API personal access token (PAT).
--config string Service configuration file. (default "anchor.toml")
--skip-config Skip loading configuration file.
5 changes: 5 additions & 0 deletions auth/testdata/TestCmdAuthSignout/--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ Usage:

Flags:
-h, --help help for signout

Global Flags:
--api-token string Anchor API personal access token (PAT).
--config string Service configuration file. (default "anchor.toml")
--skip-config Skip loading configuration file.
5 changes: 5 additions & 0 deletions auth/testdata/TestCmdAuthWhoAmI/--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ Usage:

Flags:
-h, --help help for whoami

Global Flags:
--api-token string Anchor API personal access token (PAT).
--config string Service configuration file. (default "anchor.toml")
--skip-config Skip loading configuration file.
41 changes: 35 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"strings"
"time"

"github.com/anchordotdev/cli/models"
"github.com/anchordotdev/cli/stacktrace"
"github.com/anchordotdev/cli/ui"
"github.com/cli/browser"
"github.com/mcuadros/go-defaults"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/anchordotdev/cli/models"
"github.com/anchordotdev/cli/stacktrace"
"github.com/anchordotdev/cli/ui"
)

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

var Defaults = defaultConfig()

func defaultConfig() *Config {
var cfg Config
defaults.SetDefaults(&cfg)
return &cfg
}

type UI struct {
RunTUI func(context.Context, *ui.Driver) error
}

type ContextKey string
type contextKey int

const (
configKey contextKey = iota
calledAsKey
)

func CalledAsFromContext(ctx context.Context) string {
if calledAs, ok := ctx.Value(calledAsKey).(string); ok {
return calledAs
}
return ""
}

func ConfigFromContext(ctx context.Context) *Config {
return ctx.Value(ContextKey("Config")).(*Config)
if v := ctx.Value(configKey); v != nil {
return v.(*Config)
}
return nil
}

func ConfigFromCmd(cmd *cobra.Command) *Config {
return ConfigFromContext(cmd.Context())
}

func ContextWithCalledAs(ctx context.Context, calledAs string) context.Context {
return context.WithValue(ctx, calledAsKey, calledAs)
}

func ContextWithConfig(ctx context.Context, cfg *Config) context.Context {
return context.WithValue(ctx, ContextKey("Config"), cfg)
return context.WithValue(ctx, configKey, cfg)
}

func ReportError(ctx context.Context, err error, drv *ui.Driver, cmd *cobra.Command, args []string) {
Expand Down
22 changes: 22 additions & 0 deletions clitest/clitest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package clitest

import (
"io/fs"
"os"
"testing/fstest"
)

type TestFS fstest.MapFS

func (t TestFS) Open(name string) (fs.File, error) { return (fstest.MapFS)(t).Open(name) }
func (t TestFS) ReadDir(name string) ([]fs.DirEntry, error) { return (fstest.MapFS)(t).ReadDir(name) }
func (t TestFS) Stat(name string) (fs.FileInfo, error) { return (fstest.MapFS)(t).Stat(name) }

func (t TestFS) WriteFile(name string, data []byte, perm os.FileMode) error {
t[name] = &fstest.MapFile{
Data: data,
Mode: perm,
}

return nil
}
68 changes: 43 additions & 25 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"errors"

"github.com/MakeNowJust/heredoc"
"github.com/anchordotdev/cli/models"
"github.com/anchordotdev/cli/stacktrace"
"github.com/anchordotdev/cli/ui"
"github.com/joeshaw/envdecode"
"github.com/mcuadros/go-defaults"
"github.com/spf13/cobra"
)

type CmdDef struct {
Name string

Use string
Args cobra.PositionalArgs
Short string
Long string
Aliases []string
Args cobra.PositionalArgs
Long string
Short string
Use string

SubDefs []CmdDef
}
Expand Down Expand Up @@ -98,18 +98,19 @@ var rootDef = CmdDef{
Short: "Audit lcl.host HTTPS Local Development Environment",
},
{
Name: "clean",
Name: "bootstrap",

Use: "clean [flags]",
Args: cobra.NoArgs,
Short: "Clean lcl.host CA Certificates from the Local Trust Store(s)",
Aliases: []string{"config"},
Use: "bootstrap [flags]",
Args: cobra.NoArgs,
Short: "Initial System Configuration for lcl.host Local Development",
},
{
Name: "config",
Name: "clean",

Use: "config [flags]",
Use: "clean [flags]",
Args: cobra.NoArgs,
Short: "Configure System for lcl.host Local Development",
Short: "Clean lcl.host CA Certificates from the Local Trust Store(s)",
},
{
Name: "env",
Expand All @@ -132,6 +133,13 @@ var rootDef = CmdDef{
Args: cobra.NoArgs,
Short: "Setup lcl.host Application",
},
{
Name: "trust",

Use: "trust [flags]",
Args: cobra.NoArgs,
Short: "Install CA Certificates for lcl.host Local Development",
},
},
},
{
Expand Down Expand Up @@ -232,33 +240,37 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
}

constructor := func() *cobra.Command {
cfg := new(Config)
defaults.SetDefaults(cfg)
if err := envdecode.Decode(cfg); err != nil && err != envdecode.ErrNoTargetFieldsAreSet {
panic(err)
}

cmd := &cobra.Command{
Use: def.Use,
Aliases: def.Aliases,
Args: def.Args,
Short: def.Short,
Long: def.Long,
Short: def.Short,
Use: def.Use,
SilenceUsage: true,
}

ctx := ContextWithConfig(context.Background(), cfg)
cmd.SetContext(ctx)

cmd.SetErrPrefix(ui.Danger("Error!"))

ctx := ContextWithConfig(context.Background(), defaultConfig())
cmd.SetContext(ctx)

fn(cmd)

cmd.RunE = func(cmd *cobra.Command, args []string) (returnedError error) {
cfg := ConfigFromCmd(cmd)
ctx := cmd.Context()

cfg := new(Config)
if err := cfg.Load(ctx); err != nil {
return err
}

if cfg.Test.SkipRunE {
return nil
}

ctx = ContextWithConfig(cmd.Context(), cfg)
cmd.SetContext(ctx)

var t T

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

ctx = ContextWithCalledAs(ctx, cmd.CalledAs())

drv, prg := ui.NewDriverTUI(ctx)
defer func() {
// release/restore
Expand All @@ -288,6 +302,10 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
errc <- err
}()

if cfg.TOML != nil {
drv.Activate(ctx, models.ConfigLoaded(cfg.File.Path))
}

if err := stacktrace.CapturePanic(func() error { return t.UI().RunTUI(ctx, drv) }); err != nil {
var uierr ui.Error
if errors.As(err, &uierr) {
Expand Down
16 changes: 14 additions & 2 deletions cmdtest/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ package cmdtest

import (
"bytes"
"context"
"io"
"testing"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/ui/uitest"
"github.com/joeshaw/envdecode"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

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

func Config(ctx context.Context) *cli.Config {
cfg := new(cli.Config)
cfg.Test.SystemFS = clitest.TestFS{}
if err := cfg.Load(ctx); err != nil {
panic(err)
}
return cfg
}

func TestCfg(t *testing.T, cmd *cobra.Command, args ...string) *cli.Config {
cmd = cli.NewTestCmd(cmd)
cfg := cli.ConfigFromCmd(cmd)
Expand Down
10 changes: 7 additions & 3 deletions component/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestSelector(t *testing.T) {

cfg.Test.Prefer = map[string]cli.ConfigTestPrefer{
"/v0/orgs": {
Example: "double",
Example: "anky_team",
},
}
ctx = cli.ContextWithConfig(ctx, cfg)
Expand Down Expand Up @@ -127,8 +127,12 @@ func TestSelector(t *testing.T) {

org := <-choicec

if want, got := "second-org-slug", org.Slug; want != got {
errc <- fmt.Errorf("Want org choice: %q, Got: %q", want, got)
if want, got := "ankyco", org.Slug; want != got {
t.Errorf("Want org choice: %q, Got: %q", want, got)
}

if err := <-errc; err != nil {
t.Error(err)
}

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second*3))
Expand Down
10 changes: 5 additions & 5 deletions component/testdata/TestSelector/orgs_double.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Fetching organizations…⠋
─── Selector[github.com/anchordotdev/cli/api.Organization] ─────────────────────
? Which organization do you want for this test?
> First Org Slug (first-org-slug)
Second Org Slug (second-org-slug)
> [email protected] (ankydotdev)
AnkyCo (ankyco)
─── Selector[github.com/anchordotdev/cli/api.Organization] ─────────────────────
? Which organization do you want for this test?
First Org Slug (first-org-slug)
> Second Org Slug (second-org-slug)
[email protected] (ankydotdev)
> AnkyCo (ankyco)
─── Selector[github.com/anchordotdev/cli/api.Organization] ─────────────────────
- Selected second-org-slug organization. You can also use `--org second-org-slug`.
- Selected ankyco organization. You can also use `--org ankyco`.
Loading

0 comments on commit a4aecb2

Please sign in to comment.