From 531e84e7c79eeb1f76eb1271d52ada33d4c47a16 Mon Sep 17 00:00:00 2001 From: geemus Date: Fri, 4 Oct 2024 13:46:42 +0000 Subject: [PATCH] v0.1.4 misc --- package.json | 2 +- service/env.go | 15 +- service/env_test.go | 39 +++++ service/models/env.go | 4 +- .../basics_unattached_service_display.golden | 137 ++++++++++++++++++ 5 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 service/testdata/TestServiceEnv/basics_unattached_service_display.golden diff --git a/package.json b/package.json index 5d44b51..e197a68 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,6 @@ "dependencies": {}, "scripts": {}, "devDependencies": { - "@stoplight/prism-cli": "5.9.0" + "@stoplight/prism-cli": "5.10.0" } } diff --git a/service/env.go b/service/env.go index 83d4b77..1bf830b 100644 --- a/service/env.go +++ b/service/env.go @@ -183,14 +183,17 @@ func (c *Env) Perform(ctx context.Context, drv *ui.Driver) error { return fmt.Errorf("Unknown method: %s. Please choose either `export` or `dotenv`.", envOutput) } - var lclDomain string - for _, d := range attachment.Domains { - if strings.HasSuffix(d, ".lcl.host") { - lclDomain = d - break + var lclUrl string + if attachment != nil { + var lclDomain string + for _, d := range attachment.Domains { + if strings.HasSuffix(d, ".lcl.host") { + lclDomain = d + break + } } + lclUrl = fmt.Sprintf("https://%s:%d", lclDomain, *service.LocalhostPort) } - lclUrl := fmt.Sprintf("https://%s:%d", lclDomain, *service.LocalhostPort) drv.Activate(ctx, &models.EnvNextSteps{ LclUrl: lclUrl, diff --git a/service/env_test.go b/service/env_test.go index 691e0d5..a8f5849 100644 --- a/service/env_test.go +++ b/service/env_test.go @@ -197,4 +197,43 @@ func TestServiceEnv(t *testing.T) { uitest.TestGolden(t, drv.Golden()) }) + + t.Run("basics unattached service display", func(t *testing.T) { + if srv.IsProxy() { + t.Skip("service env unsupported in proxy mode") + } + + cfg.Test.Prefer = map[string]cli.ConfigTestPrefer{ + "/v0/orgs/org-slug/services/service-name/attachments": { + Example: "empty", + }, + } + ctx = cli.ContextWithConfig(ctx, cfg) + + drv, tm := uitest.TestTUI(ctx, t) + cmd := Env{ + Clipboard: new(clipboard.Mock), + } + errc := make(chan error, 1) + + go func() { + errc <- cmd.UI().RunTUI(ctx, drv) + errc <- tm.Quit() + }() + + uitest.WaitForGoldenContains(t, drv, errc, + "? How would you like to manage your environment variables?", + ) + + tm.Send(tea.KeyMsg{Type: tea.KeyDown}) + tm.Send(tea.KeyMsg{Type: tea.KeyDown}) + tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) + + tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second*3)) + if err := <-errc; err != nil { + t.Fatal(err) + } + + uitest.TestGolden(t, drv.Golden()) + }) } diff --git a/service/models/env.go b/service/models/env.go index 2b2073c..2f91bf5 100644 --- a/service/models/env.go +++ b/service/models/env.go @@ -223,7 +223,9 @@ func (m *EnvNextSteps) View() string { fmt.Fprintln(&b, ui.Header("Next Steps")) fmt.Fprintln(&b, ui.StepAlert(ui.Action("(Re)Start your server."))) - fmt.Fprintln(&b, ui.StepAlert(fmt.Sprintf("%s: %s", ui.Action("Check out your encrypted site"), ui.URL(m.LclUrl)))) + if m.LclUrl != "" { + fmt.Fprintln(&b, ui.StepAlert(fmt.Sprintf("%s: %s", ui.Action("Check out your encrypted site"), ui.URL(m.LclUrl)))) + } fmt.Fprintln(&b, ui.StepHint("These certificates will renew automatically, time to enjoy effortless encryption.")) return b.String() diff --git a/service/testdata/TestServiceEnv/basics_unattached_service_display.golden b/service/testdata/TestServiceEnv/basics_unattached_service_display.golden new file mode 100644 index 0000000..c111efe --- /dev/null +++ b/service/testdata/TestServiceEnv/basics_unattached_service_display.golden @@ -0,0 +1,137 @@ +─── Client ───────────────────────────────────────────────────────────────────── + * Checking authentication: probing credentials locally…⠋ +─── Client ───────────────────────────────────────────────────────────────────── + * Checking authentication: testing credentials remotely…⠋ +─── ServiceEnvHeader ─────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` +─── ServiceEnvHint ───────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. +─── Fetcher[github.com/anchordotdev/cli/api.Organization] ────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + * Fetching organizations…⠋ +─── Fetcher[github.com/anchordotdev/cli/api.Organization] ────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. +─── Fetcher[github.com/anchordotdev/cli/api.Realm] ───────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + * Fetching realms…⠋ +─── Fetcher[github.com/anchordotdev/cli/api.Realm] ───────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. +─── Fetcher[github.com/anchordotdev/cli/api.Service] ─────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + * Fetching services…⠋ +─── Fetcher[github.com/anchordotdev/cli/api.Service] ─────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. +─── EnvFetch ─────────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + * Fetching service-name environment variables…⠋ +─── EnvFetch ─────────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. +─── EnvMethod ────────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + ? How would you like to manage your environment variables? + > Add export commands to your clipboard. + Add dotenv contents to your clipboard. + Display export commands. ! WARNING: could be observed by others. +─── EnvMethod ────────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + ? How would you like to manage your environment variables? + Add export commands to your clipboard. + > Add dotenv contents to your clipboard. + Display export commands. ! WARNING: could be observed by others. +─── EnvMethod ────────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + ? How would you like to manage your environment variables? + Add export commands to your clipboard. + Add dotenv contents to your clipboard. + > Display export commands. ! WARNING: could be observed by others. +─── EnvMethod ────────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + - Selected display environment variable method. You can also use `--method display`. +─── EnvDisplay ───────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + - Selected display environment variable method. You can also use `--method display`. + +export ACME_CONTACT="anky@anchor.dev" +export ACME_DIRECTORY_URL="http://anchor.lcl.host/org-slug/realm-slug/x509/ca/acme" +export HTTPS_PORT="4433" + + - Displayed service-name export commands. + ! Be sure to load these into your environment. +─── EnvNextSteps ─────────────────────────────────────────────────────────────── + +# Fetch Environment Variables for Service `anchor service env` + | We'll set your environment variables to provide configuration and credentials. + - Using org-slug, the only available organization. You can also use `--org org-slug`. + - Using realm-slug, the only available realm. You can also use `--realm realm-slug`. + - Using service-name, the only available service. You can also use `--service service-name`. + - Selected display environment variable method. You can also use `--method display`. + +export ACME_CONTACT="anky@anchor.dev" +export ACME_DIRECTORY_URL="http://anchor.lcl.host/org-slug/realm-slug/x509/ca/acme" +export HTTPS_PORT="4433" + + - Displayed service-name export commands. + ! Be sure to load these into your environment. + +# Next Steps + ! (Re)Start your server. + | These certificates will renew automatically, time to enjoy effortless encryption.