Skip to content

Commit 021bf29

Browse files
committed
v0.1.2 toml ux refinements
1 parent da1adb8 commit 021bf29

36 files changed

+654
-176
lines changed

auth/auth_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package auth
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

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

22-
m.Run()
23+
srv.Close()
2324
}
2425

2526
func TestCmdAuth(t *testing.T) {

cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import (
2222
"github.com/anchordotdev/cli/ui"
2323
)
2424

25+
type Clipboard interface {
26+
ReadAll() (string, error)
27+
WriteAll(text string) error
28+
}
29+
2530
var Executable string
2631

2732
var Version = struct {

cli_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/anchordotdev/cli"
14+
"github.com/anchordotdev/cli/models"
1415
"github.com/anchordotdev/cli/stacktrace"
1516
_ "github.com/anchordotdev/cli/testflags"
1617
"github.com/anchordotdev/cli/ui"
@@ -92,8 +93,6 @@ func TestError(t *testing.T) {
9293
})
9394
}
9495

95-
var CmdPanic = cli.NewCmd[PanicCommand](nil, "error", func(cmd *cobra.Command) {})
96-
9796
type PanicCommand struct{}
9897

9998
func (c PanicCommand) UI() cli.UI {
@@ -170,3 +169,35 @@ func (m *TestHint) View() string {
170169
}
171170

172171
var Timestamp, _ = time.Parse(time.RFC3339Nano, "2024-01-02T15:04:05.987654321Z")
172+
173+
func TestConfigLoadTOMLGolden(t *testing.T) {
174+
ctx, cancel := context.WithCancel(context.Background())
175+
defer cancel()
176+
177+
cfg := new(cli.Config)
178+
cfg.File.Path = "anchor.toml"
179+
cfg.Via.TOML = new(cli.Config)
180+
181+
ctx = cli.ContextWithConfig(ctx, cfg)
182+
183+
cmd := tomlCommand{}
184+
185+
uitest.TestTUIOutput(ctx, t, cmd.UI())
186+
}
187+
188+
type tomlCommand struct{}
189+
190+
func (c tomlCommand) UI() cli.UI {
191+
return cli.UI{
192+
RunTUI: c.run,
193+
}
194+
}
195+
196+
func (*tomlCommand) run(ctx context.Context, drv *ui.Driver) error {
197+
cfg := cli.ConfigFromContext(ctx)
198+
if cfg.Via.TOML != nil {
199+
drv.Activate(ctx, models.ConfigLoaded(cfg.File.Path))
200+
}
201+
202+
return nil
203+
}

clipboard/clipboard.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package clipboard
2+
3+
import (
4+
"sync/atomic"
5+
6+
"github.com/atotto/clipboard"
7+
)
8+
9+
type Mock atomic.Pointer[string]
10+
11+
func (m *Mock) ReadAll() (string, error) {
12+
if ptr := ((*atomic.Pointer[string])(m)).Load(); ptr != nil {
13+
return *ptr, nil
14+
}
15+
return "", nil
16+
}
17+
18+
func (m *Mock) WriteAll(text string) error {
19+
((*atomic.Pointer[string])(m)).Store(&text)
20+
return nil
21+
}
22+
23+
var System = system{}
24+
25+
type system struct{}
26+
27+
func (system) ReadAll() (string, error) { return clipboard.ReadAll() }
28+
29+
func (system) WriteAll(text string) error { return clipboard.WriteAll(text) }

cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
302302
errc <- err
303303
}()
304304

305-
if cfg.TOML != nil {
305+
if cfg.Via.TOML != nil {
306306
drv.Activate(ctx, models.ConfigLoaded(cfg.File.Path))
307307
}
308308

cmdtest/cmdtest.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ func TestCfg(t *testing.T, cmd *cobra.Command, args ...string) *cli.Config {
3838
return cfg
3939
}
4040

41-
func TestCmd(t *testing.T, cmd *cobra.Command, args ...string) *cobra.Command {
42-
_, err := executeSkip(cmd, args...)
43-
require.NoError(t, err)
44-
45-
return cmd
46-
}
47-
4841
func TestError(t *testing.T, cmd *cobra.Command, args ...string) error {
4942
_, err := executeSkip(cmd, args...)
5043
require.Error(t, err)

component/config_via_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package component_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
"testing/fstest"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/anchordotdev/cli"
10+
"github.com/anchordotdev/cli/clitest"
11+
"github.com/anchordotdev/cli/cmdtest"
12+
"github.com/anchordotdev/cli/component/models"
13+
"github.com/anchordotdev/cli/ui"
14+
"github.com/anchordotdev/cli/ui/uitest"
15+
)
16+
17+
func TestConfigVia(t *testing.T) {
18+
19+
cmd := configViaCommand{}
20+
21+
t.Run("env", func(t *testing.T) {
22+
ctx, cancel := context.WithCancel(context.Background())
23+
defer cancel()
24+
25+
t.Setenv("ORG", "env-org")
26+
cfg := cmdtest.Config(ctx)
27+
ctx = cli.ContextWithConfig(ctx, cfg)
28+
29+
uitest.TestTUIOutput(ctx, t, cmd.UI())
30+
})
31+
32+
t.Run("toml", func(t *testing.T) {
33+
ctx, cancel := context.WithCancel(context.Background())
34+
defer cancel()
35+
36+
cfg := new(cli.Config)
37+
cfg.Test.SystemFS = clitest.TestFS{
38+
"anchor.toml": &fstest.MapFile{
39+
Data: []byte(heredoc.Doc(`
40+
[org]
41+
apid = "toml-org"
42+
`)),
43+
},
44+
}
45+
if err := cfg.Load(ctx); err != nil {
46+
panic(err)
47+
}
48+
ctx = cli.ContextWithConfig(ctx, cfg)
49+
50+
uitest.TestTUIOutput(ctx, t, cmd.UI())
51+
})
52+
53+
t.Run("default", func(t *testing.T) {
54+
ctx, cancel := context.WithCancel(context.Background())
55+
defer cancel()
56+
57+
cfg := cmdtest.Config(ctx)
58+
ctx = cli.ContextWithConfig(ctx, cfg)
59+
60+
uitest.TestTUIOutput(ctx, t, cmd.UI())
61+
})
62+
63+
t.Run("flag", func(t *testing.T) {
64+
ctx, cancel := context.WithCancel(context.Background())
65+
defer cancel()
66+
67+
cfg := cmdtest.Config(ctx)
68+
cfg.Org.APID = "flag-org"
69+
ctx = cli.ContextWithConfig(ctx, cfg)
70+
71+
uitest.TestTUIOutput(ctx, t, cmd.UI())
72+
})
73+
}
74+
75+
type configViaCommand struct{}
76+
77+
func (c configViaCommand) UI() cli.UI {
78+
return cli.UI{
79+
RunTUI: c.run,
80+
}
81+
}
82+
83+
func (*configViaCommand) run(ctx context.Context, drv *ui.Driver) error {
84+
cfg := cli.ConfigFromContext(ctx)
85+
86+
if cfg.Org.APID != "" {
87+
drv.Activate(ctx, &models.ConfigVia{
88+
Config: cfg,
89+
ConfigFetchFn: func(cfg *cli.Config) any { return cfg.Org.APID },
90+
Flag: "--org",
91+
Singular: "organization",
92+
})
93+
return nil
94+
}
95+
if cfg.API.URL != "" {
96+
drv.Activate(ctx, &models.ConfigVia{
97+
Config: cfg,
98+
ConfigFetchFn: func(cfg *cli.Config) any { return cfg.API.URL },
99+
Flag: "API_URL=",
100+
Singular: "api url",
101+
})
102+
}
103+
104+
return nil
105+
}

component/models/config_via.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package models
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/anchordotdev/cli"
8+
"github.com/anchordotdev/cli/ui"
9+
tea "github.com/charmbracelet/bubbletea"
10+
)
11+
12+
type ConfigVia struct {
13+
Config *cli.Config
14+
ConfigFetchFn cli.ConfigFetchFunc
15+
16+
Flag, Singular string
17+
}
18+
19+
func (m *ConfigVia) Init() tea.Cmd { return nil }
20+
21+
func (m *ConfigVia) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil }
22+
23+
func (m *ConfigVia) View() string {
24+
var b strings.Builder
25+
26+
source := m.Config.ViaSource(m.ConfigFetchFn)
27+
value := fmt.Sprintf("%+v", m.ConfigFetchFn(m.Config))
28+
29+
fmt.Fprintln(&b, ui.StepDone(fmt.Sprintf("Using %s %s from %s. %s",
30+
ui.Emphasize(value),
31+
m.Singular,
32+
source,
33+
ui.Whisper(fmt.Sprintf("You can also use `%s %s`.", m.Flag, value)),
34+
)))
35+
36+
return b.String()
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
─── ConfigVia ──────────────────────────────────────────────────────────────────
2+
- Using https://api.anchor.dev/v0 api url from default. You can also use `API_URL= https://api.anchor.dev/v0`.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
─── ConfigVia ──────────────────────────────────────────────────────────────────
2+
- Using env-org organization from env. You can also use `--org env-org`.

0 commit comments

Comments
 (0)