Skip to content

Commit

Permalink
v0.1.2 toml ux refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Sep 17, 2024
1 parent da1adb8 commit 021bf29
Show file tree
Hide file tree
Showing 36 changed files with 654 additions and 176 deletions.
5 changes: 3 additions & 2 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"context"
"os"
"testing"

"github.com/anchordotdev/cli/api/apitest"
Expand All @@ -17,9 +18,9 @@ func TestMain(m *testing.M) {
if err := srv.Start(context.Background()); err != nil {
panic(err)
}
defer srv.Close()
defer os.Exit(m.Run())

m.Run()
srv.Close()
}

func TestCmdAuth(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"github.com/anchordotdev/cli/ui"
)

type Clipboard interface {
ReadAll() (string, error)
WriteAll(text string) error
}

var Executable string

var Version = struct {
Expand Down
35 changes: 33 additions & 2 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/models"
"github.com/anchordotdev/cli/stacktrace"
_ "github.com/anchordotdev/cli/testflags"
"github.com/anchordotdev/cli/ui"
Expand Down Expand Up @@ -92,8 +93,6 @@ func TestError(t *testing.T) {
})
}

var CmdPanic = cli.NewCmd[PanicCommand](nil, "error", func(cmd *cobra.Command) {})

type PanicCommand struct{}

func (c PanicCommand) UI() cli.UI {
Expand Down Expand Up @@ -170,3 +169,35 @@ func (m *TestHint) View() string {
}

var Timestamp, _ = time.Parse(time.RFC3339Nano, "2024-01-02T15:04:05.987654321Z")

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

cfg := new(cli.Config)
cfg.File.Path = "anchor.toml"
cfg.Via.TOML = new(cli.Config)

ctx = cli.ContextWithConfig(ctx, cfg)

cmd := tomlCommand{}

uitest.TestTUIOutput(ctx, t, cmd.UI())
}

type tomlCommand struct{}

func (c tomlCommand) UI() cli.UI {
return cli.UI{
RunTUI: c.run,
}
}

func (*tomlCommand) run(ctx context.Context, drv *ui.Driver) error {
cfg := cli.ConfigFromContext(ctx)
if cfg.Via.TOML != nil {
drv.Activate(ctx, models.ConfigLoaded(cfg.File.Path))
}

return nil
}
29 changes: 29 additions & 0 deletions clipboard/clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package clipboard

import (
"sync/atomic"

"github.com/atotto/clipboard"
)

type Mock atomic.Pointer[string]

func (m *Mock) ReadAll() (string, error) {
if ptr := ((*atomic.Pointer[string])(m)).Load(); ptr != nil {
return *ptr, nil
}
return "", nil
}

func (m *Mock) WriteAll(text string) error {
((*atomic.Pointer[string])(m)).Store(&text)
return nil
}

var System = system{}

type system struct{}

func (system) ReadAll() (string, error) { return clipboard.ReadAll() }

func (system) WriteAll(text string) error { return clipboard.WriteAll(text) }
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
errc <- err
}()

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

Expand Down
7 changes: 0 additions & 7 deletions cmdtest/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ func TestCfg(t *testing.T, cmd *cobra.Command, args ...string) *cli.Config {
return cfg
}

func TestCmd(t *testing.T, cmd *cobra.Command, args ...string) *cobra.Command {
_, err := executeSkip(cmd, args...)
require.NoError(t, err)

return cmd
}

func TestError(t *testing.T, cmd *cobra.Command, args ...string) error {
_, err := executeSkip(cmd, args...)
require.Error(t, err)
Expand Down
105 changes: 105 additions & 0 deletions component/config_via_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package component_test

import (
"context"
"testing"
"testing/fstest"

"github.com/MakeNowJust/heredoc"
"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/clitest"
"github.com/anchordotdev/cli/cmdtest"
"github.com/anchordotdev/cli/component/models"
"github.com/anchordotdev/cli/ui"
"github.com/anchordotdev/cli/ui/uitest"
)

func TestConfigVia(t *testing.T) {

cmd := configViaCommand{}

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

t.Setenv("ORG", "env-org")
cfg := cmdtest.Config(ctx)
ctx = cli.ContextWithConfig(ctx, cfg)

uitest.TestTUIOutput(ctx, t, cmd.UI())
})

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

cfg := new(cli.Config)
cfg.Test.SystemFS = clitest.TestFS{
"anchor.toml": &fstest.MapFile{
Data: []byte(heredoc.Doc(`
[org]
apid = "toml-org"
`)),
},
}
if err := cfg.Load(ctx); err != nil {
panic(err)
}
ctx = cli.ContextWithConfig(ctx, cfg)

uitest.TestTUIOutput(ctx, t, cmd.UI())
})

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

cfg := cmdtest.Config(ctx)
ctx = cli.ContextWithConfig(ctx, cfg)

uitest.TestTUIOutput(ctx, t, cmd.UI())
})

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

cfg := cmdtest.Config(ctx)
cfg.Org.APID = "flag-org"
ctx = cli.ContextWithConfig(ctx, cfg)

uitest.TestTUIOutput(ctx, t, cmd.UI())
})
}

type configViaCommand struct{}

func (c configViaCommand) UI() cli.UI {
return cli.UI{
RunTUI: c.run,
}
}

func (*configViaCommand) run(ctx context.Context, drv *ui.Driver) error {
cfg := cli.ConfigFromContext(ctx)

if cfg.Org.APID != "" {
drv.Activate(ctx, &models.ConfigVia{
Config: cfg,
ConfigFetchFn: func(cfg *cli.Config) any { return cfg.Org.APID },
Flag: "--org",
Singular: "organization",
})
return nil
}
if cfg.API.URL != "" {
drv.Activate(ctx, &models.ConfigVia{
Config: cfg,
ConfigFetchFn: func(cfg *cli.Config) any { return cfg.API.URL },
Flag: "API_URL=",
Singular: "api url",
})
}

return nil
}
37 changes: 37 additions & 0 deletions component/models/config_via.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package models

import (
"fmt"
"strings"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/ui"
tea "github.com/charmbracelet/bubbletea"
)

type ConfigVia struct {
Config *cli.Config
ConfigFetchFn cli.ConfigFetchFunc

Flag, Singular string
}

func (m *ConfigVia) Init() tea.Cmd { return nil }

func (m *ConfigVia) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil }

func (m *ConfigVia) View() string {
var b strings.Builder

source := m.Config.ViaSource(m.ConfigFetchFn)
value := fmt.Sprintf("%+v", m.ConfigFetchFn(m.Config))

fmt.Fprintln(&b, ui.StepDone(fmt.Sprintf("Using %s %s from %s. %s",
ui.Emphasize(value),
m.Singular,
source,
ui.Whisper(fmt.Sprintf("You can also use `%s %s`.", m.Flag, value)),
)))

return b.String()
}
2 changes: 2 additions & 0 deletions component/testdata/TestConfigVia/default.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
─── ConfigVia ──────────────────────────────────────────────────────────────────
- Using https://api.anchor.dev/v0 api url from default. You can also use `API_URL= https://api.anchor.dev/v0`.
2 changes: 2 additions & 0 deletions component/testdata/TestConfigVia/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
─── ConfigVia ──────────────────────────────────────────────────────────────────
- Using env-org organization from env. You can also use `--org env-org`.
2 changes: 2 additions & 0 deletions component/testdata/TestConfigVia/flag.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
─── ConfigVia ──────────────────────────────────────────────────────────────────
- Using flag-org organization from flag. You can also use `--org flag-org`.
2 changes: 2 additions & 0 deletions component/testdata/TestConfigVia/toml.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
─── ConfigVia ──────────────────────────────────────────────────────────────────
- Using toml-org organization from anchor.toml. You can also use `--org toml-org`.
Loading

0 comments on commit 021bf29

Please sign in to comment.