-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
654 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
Oops, something went wrong.