Skip to content

Commit

Permalink
v0.0.10: refine lcl command
Browse files Browse the repository at this point in the history
  • Loading branch information
benburkert committed Feb 28, 2024
1 parent 695de14 commit fdaadba
Show file tree
Hide file tree
Showing 29 changed files with 432 additions and 257 deletions.
16 changes: 12 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func getServicePath(orgSlug, serviceSlug string) string {
func (s *Session) GetService(ctx context.Context, orgSlug, serviceSlug string) (*Service, error) {
var svc Service
if err := s.get(ctx, getServicePath(orgSlug, serviceSlug), &svc); err != nil {
if err == NotFoundErr {
if errors.Is(err, NotFoundErr) {
return nil, nil
}
return nil, err
Expand All @@ -248,7 +248,11 @@ func (s *Session) get(ctx context.Context, path string, out any) error {
return err
}
if res.StatusCode != http.StatusOK {
return StatusCodeError(res.StatusCode)
var errorsRes *Error
if err = json.NewDecoder(res.Body).Decode(&errorsRes); err != nil {
return err
}
return fmt.Errorf("%w: %s", StatusCodeError(res.StatusCode), errorsRes.Title)
}
return json.NewDecoder(res.Body).Decode(out)
}
Expand All @@ -274,7 +278,11 @@ func (s *Session) post(ctx context.Context, path string, in, out any) error {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected response code: %d", res.StatusCode)
var errorsRes *Error
if err = json.NewDecoder(res.Body).Decode(&errorsRes); err != nil {
return err
}
return fmt.Errorf("%w: %s", StatusCodeError(res.StatusCode), errorsRes.Title)
}
return json.NewDecoder(res.Body).Decode(out)
}
Expand Down Expand Up @@ -351,4 +359,4 @@ type StatusCodeError int
const NotFoundErr = StatusCodeError(http.StatusNotFound)

func (err StatusCodeError) StatusCode() int { return int(err) }
func (err StatusCodeError) Error() string { return fmt.Sprintf("unexpected response code: %d", err) }
func (err StatusCodeError) Error() string { return fmt.Sprintf("unexpected %d status response", err) }
4 changes: 4 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/anchordotdev/cli/api/apitest"
)

var (
_ = flag.Bool("update", false, "ignored")
)

var srv = &apitest.Server{
Host: "api.anchor.lcl.host",
RootDir: "../..",
Expand Down
2 changes: 1 addition & 1 deletion auth/models/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type SignInChecker struct {
}

func (m *SignInChecker) Init() tea.Cmd {
m.spinner = ui.Spinner()
m.spinner = ui.WaitingSpinner()

return m.spinner.Tick
}
Expand Down
4 changes: 2 additions & 2 deletions auth/whoami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestWhoAmI(t *testing.T) {
cfg.Keyring.MockMode = true

var err error
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
t.Fatal(err)
}

Expand All @@ -47,7 +47,7 @@ func TestWhoAmI(t *testing.T) {
t.Fatal(err)
}

if want, got := "Hello [email protected]!\n", buf.String(); want != got {
if want, got := "Hello [email protected]!\n", buf.String(); want != got {
t.Errorf("want output %q, got %q", want, got)
}
})
Expand Down
2 changes: 1 addition & 1 deletion cert/models/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Provision struct {
}

func (m *Provision) Init() tea.Cmd {
m.spinner = ui.Spinner()
m.spinner = ui.WaitingSpinner()

return m.spinner.Tick
}
Expand Down
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Config struct {
Subdomain string `desc:"Subdomain for lcl.host diagnostic service." flag:"subdomain" env:"SUBDOMAIN" json:"subdomain" toml:"subdomain"`

DiagnosticAddr string `default:":4433" desc:"Local server address" flag:"addr,a" env:"ADDR" json:"address" toml:"address"`
LclHostURL string `default:"https://lcl.host" env:"LCL_HOST_URL"`
LclHostURL string `default:"https://lcl.anchor.systems" env:"LCL_HOST_URL"`

Detect struct {
PackageManager string `desc:"Package manager to use for integrating Anchor." flag:"package-manager" env:"PACKAGE_MANAGER" json:"package_manager" toml:"package-manager"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/anchor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var (
},
},
{
UI: trust.Sync{Config: cfg}.UI(),
UI: trust.Command{Config: cfg}.UI(),

Name: "trust",
Use: "trust [org[/realm[/ca]]]",
Expand Down
11 changes: 6 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ func (c *Command) Execute(ctx context.Context, cfg *Config) error {

func (c *Command) cobraCommand(ctx context.Context, cfgv reflect.Value) *cobra.Command {
cmd := &cobra.Command{
Use: c.Use,
Short: c.Short,
Long: c.Long,
GroupID: c.Group,
Hidden: c.Hidden,
Use: c.Use,
Short: c.Short,
Long: c.Long,
GroupID: c.Group,
Hidden: c.Hidden,
SilenceUsage: true,
}

if c.Preflight != nil {
Expand Down
1 change: 1 addition & 0 deletions detection/detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var (
_ = flag.Bool("prism-verbose", false, "ignored")
_ = flag.Bool("prism-proxy", false, "ignored")
_ = flag.Bool("update", false, "ignored")
)

func TestScore_String(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions diagnostic/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var (
_ = flag.Bool("prism-verbose", false, "ignored")
_ = flag.Bool("prism-proxy", false, "ignored")
_ = flag.Bool("update", false, "ignored")
)

func TestServerSupportsDualProtocols(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/x/exp/teatest v0.0.0-20240202113029-6ff29cf0473e
github.com/charmbracelet/x/exp/teatest v0.0.0-20240222131549-03ee51df8bea
github.com/cli/browser v1.3.0
github.com/creack/pty v1.1.21
github.com/deepmap/oapi-codegen v1.16.2
Expand All @@ -34,6 +34,7 @@ require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
Expand Down Expand Up @@ -65,7 +66,7 @@ require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/charmbracelet/x/exp/teatest v0.0.0-20240202113029-6ff29cf0473e h1:cJmZ2KOD9/EzIX2ZQKR/VUNbs+rNfIg4x9GI9NAfbss=
github.com/charmbracelet/x/exp/teatest v0.0.0-20240202113029-6ff29cf0473e/go.mod h1:OZ61R8FnVvNSCTOzAprW6avzlYA4xd9h0PFi79QtMH0=
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 h1:kyT+aGp1z5jwlus3OY0cP6FuT05jYeeExx/4TYxnyrs=
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/teatest v0.0.0-20240222131549-03ee51df8bea h1:rMsCa4AcGApEidjhRpitA2HZds22ZSnAuVjx8SVF3yA=
github.com/charmbracelet/x/exp/teatest v0.0.0-20240222131549-03ee51df8bea/go.mod h1:SG24wGkG/mix5V2dZLXfQ6Bod43HGvk9CkTDxATwKN4=
github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo=
github.com/cli/browser v1.3.0/go.mod h1:HH8s+fOAxjhQoBUAsKuPCbqUuxZDhQ2/aD+SzsEfBTk=
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
Expand Down Expand Up @@ -181,8 +183,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
Expand Down
1 change: 1 addition & 0 deletions keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var (
_ = flag.Bool("prism-verbose", false, "ignored")
_ = flag.Bool("prism-proxy", false, "ignored")
_ = flag.Bool("update", false, "ignored")
)

func TestKeyring(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion lcl/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func (d Detect) run(ctx context.Context, drv *ui.Driver) error {
}

cmdCert := cert.Provision{
Cert: tlsCert,
Cert: tlsCert,
Config: d.Config,
}

if err := cmdCert.RunTUI(ctx, drv, domains...); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions lcl/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func (d *Diagnostic) runTUI(ctx context.Context, drv *ui.Driver, cert *tls.Certi
return nil
}

cmdTrustSync := &trust.Sync{
cmdTrust := &trust.Command{
Config: d.Config,
Anc: d.anc,
OrgSlug: d.orgSlug,
RealmSlug: d.realmSlug,
}

if err := cmdTrustSync.UI().RunTUI(ctx, drv); err != nil {
if err := cmdTrust.UI().RunTUI(ctx, drv); err != nil {
return err
}
}
Expand Down
17 changes: 12 additions & 5 deletions lcl/lcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,32 @@ func TestLcl(t *testing.T) {
cfg.API.URL = srv.URL
cfg.AnchorURL = "http://anchor.lcl.host:" + srv.RailsPort + "/"
cfg.Lcl.DiagnosticAddr = diagAddr
cfg.Lcl.Service = "hi-example"
cfg.Lcl.Subdomain = "hi-example"
cfg.Lcl.Service = "hi-ankydotdev"
cfg.Lcl.Subdomain = "hi-ankydotdev"
cfg.Trust.MockMode = true
cfg.Trust.NoSudo = true
cfg.Trust.Stores = []string{"mock"}

if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
t.Fatal(err)
}

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

drv, tm := uitest.TestTUI(ctx, t)

cmd := Command{
Config: cfg,
}

errc := make(chan error, 1)
go func() { errc <- cmd.UI().RunTUI(ctx, drv) }()
go func() {
errc <- cmd.UI().RunTUI(ctx, drv)

tm.Quit()
}()

// wait for prompt

Expand All @@ -100,7 +107,7 @@ func TestLcl(t *testing.T) {
})

if !srv.IsProxy() {
t.Skip("diagnostic unsupported in non-mock mode")
t.Skip("diagnostic unsupported in mock mode")
}

teatest.WaitFor(
Expand Down
2 changes: 1 addition & 1 deletion lcl/models/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DetectPreamble struct {
}

func (m *DetectPreamble) Init() tea.Cmd {
m.spinner = ui.Spinner()
m.spinner = ui.WaitingSpinner()

return m.spinner.Tick
}
Expand Down
6 changes: 3 additions & 3 deletions lcl/models/lcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type LclScan struct {
}

func (m *LclScan) Init() tea.Cmd {
m.spinner = ui.Spinner()
m.spinner = ui.WaitingSpinner()

return m.spinner.Tick
}
Expand All @@ -54,7 +54,7 @@ func (m *LclScan) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

func (m *LclScan) View() string {
var b strings.Builder
fmt.Fprintln(&b, ui.Header("Set Up lcl.host Local HTTPS Diagnostic"))
fmt.Fprintln(&b, ui.Header(fmt.Sprintf("Set Up lcl.host Local HTTPS Diagnostic %s", ui.Whisper("`anchor lcl`"))))
fmt.Fprintln(&b, ui.StepHint("We will start by determining your system's starting point for setup."))

if !m.finished {
Expand Down Expand Up @@ -149,7 +149,7 @@ type ProvisionService struct {
}

func (m *ProvisionService) Init() tea.Cmd {
m.spinner = ui.Spinner()
m.spinner = ui.WaitingSpinner()

return m.spinner.Tick
}
Expand Down
6 changes: 3 additions & 3 deletions lcl/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func TestProvision(t *testing.T) {
cfg := new(cli.Config)
cfg.API.URL = srv.URL
cfg.AnchorURL = "http://anchor.lcl.host:" + srv.RailsPort
cfg.Lcl.Service = "hi-example"
cfg.Lcl.Subdomain = "hi-example"
cfg.Lcl.Service = "hi-ankydotdev"
cfg.Lcl.Subdomain = "hi-ankydotdev"
cfg.Trust.MockMode = true
cfg.Trust.NoSudo = true

var err error
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
t.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion trust/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAudit(t *testing.T) {
cfg.Trust.Stores = []string{"mock"}

var err error
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
if cfg.API.Token, err = srv.GeneratePAT("[email protected]"); err != nil {
t.Fatal(err)
}

Expand Down
4 changes: 2 additions & 2 deletions trust/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (c *Clean) runTUI(ctx context.Context, drv *ui.Driver) error {
return err
}

handle, err := getHandle(ctx, anc)
userInfo, err := anc.UserInfo(ctx)
if err != nil {
return err
}
drv.Send(models.HandleMsg(handle))
drv.Send(models.HandleMsg(userInfo.PersonalOrg.Slug))

org, realm, err := fetchOrgAndRealm(ctx, c.Config, anc)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions trust/models/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"

"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/truststore"
"github.com/anchordotdev/cli/ui"
)
Expand All @@ -23,15 +24,15 @@ type CleanPreflight struct {
}

func (c *CleanPreflight) Init() tea.Cmd {
c.spinner = ui.Spinner()
c.spinner = ui.WaitingSpinner()

return c.spinner.Tick
}

func (c *CleanPreflight) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
c.spinner, cmd = c.spinner.Update(msg)
return c, cmd
var cmd tea.Cmd
c.spinner, cmd = c.spinner.Update(msg)
return c, cmd
}

func (c *CleanPreflight) View() string {
Expand All @@ -53,6 +54,8 @@ const (
)

type CleanCA struct {
Config *cli.Config

CA *truststore.CA
ConfirmCh chan<- struct{}

Expand All @@ -65,7 +68,7 @@ type CleanCA struct {
}

func (c *CleanCA) Init() tea.Cmd {
c.spinner = ui.Spinner()
c.spinner = ui.WaitingSpinner()

c.cleaned = make(map[truststore.Store]struct{})

Expand Down
Loading

0 comments on commit fdaadba

Please sign in to comment.