From 56fc26879605c9ecd3e98430d97d8228c0c0e645 Mon Sep 17 00:00:00 2001 From: Duane May Date: Fri, 24 Apr 2026 15:32:07 -0400 Subject: [PATCH] Add `targets` command --- cmd/targets.go | 46 ++++++++++++++++++++++++ cmd/targets_test.go | 78 ++++++++++++++++++++++++++++++++++++++++ docs/commands/targets.md | 45 +++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 cmd/targets.go create mode 100644 cmd/targets_test.go create mode 100644 docs/commands/targets.md diff --git a/cmd/targets.go b/cmd/targets.go new file mode 100644 index 0000000..fd2adc7 --- /dev/null +++ b/cmd/targets.go @@ -0,0 +1,46 @@ +package cmd + +import ( + "fmt" + "sort" + + "code.cloudfoundry.org/uaa-cli/config" + "github.com/spf13/cobra" +) + +func ListTargetsCmd(cfg config.Config) { + if len(cfg.Targets) == 0 { + log.Info("No targets set.") + return + } + + keys := make([]string, 0, len(cfg.Targets)) + for k := range cfg.Targets { + keys = append(keys, k) + } + sort.Strings(keys) + + for i, k := range keys { + target := cfg.Targets[k] + marker := " " + if k == cfg.ActiveTargetName { + marker = "* " + } + log.Info(fmt.Sprintf("%s%d: %s", marker, i+1, target.BaseUrl)) + } +} + +var targetsCmd = &cobra.Command{ + Use: "targets", + Short: "List all registered targets", + Run: func(cmd *cobra.Command, args []string) { + cfg := GetSavedConfig() + ListTargetsCmd(cfg) + }, +} + +func init() { + RootCmd.AddCommand(targetsCmd) + targetsCmd.Annotations = make(map[string]string) + targetsCmd.Annotations[INTRO_CATEGORY] = "true" +} diff --git a/cmd/targets_test.go b/cmd/targets_test.go new file mode 100644 index 0000000..03e829d --- /dev/null +++ b/cmd/targets_test.go @@ -0,0 +1,78 @@ +package cmd_test + +import ( + "code.cloudfoundry.org/uaa-cli/config" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gbytes" + . "github.com/onsi/gomega/gexec" +) + +var _ = Describe("Targets", func() { + Describe("when no targets have been set", func() { + BeforeEach(func() { + c := config.NewConfig() + Expect(config.WriteConfig(c)).Error().ShouldNot(HaveOccurred()) + }) + + It("exits 0 and indicates no targets", func() { + session := runCommand("targets") + + Eventually(session).Should(Exit(0)) + Expect(session.Out).To(Say("No targets set.")) + }) + }) + + Describe("when one target has been set", func() { + BeforeEach(func() { + c := config.NewConfigWithServerURL(server.URL()) + Expect(config.WriteConfig(c)).Error().ShouldNot(HaveOccurred()) + }) + + It("exits 0, lists the target URL, and marks it as active", func() { + session := runCommand("targets") + + Eventually(session).Should(Exit(0)) + Expect(session.Out).To(Say(`\*`)) + Expect(session.Out).To(Say(server.URL())) + }) + + It("exits 0 with --verbose flag", func() { + session := runCommand("targets", "--verbose") + + Eventually(session).Should(Exit(0)) + Expect(session.Out).To(Say(server.URL())) + }) + }) + + Describe("when multiple targets have been set", func() { + var secondURL string + + BeforeEach(func() { + secondURL = "http://second-uaa.example.com" + + c := config.NewConfigWithServerURL(server.URL()) + t2 := config.NewTarget() + t2.BaseUrl = secondURL + c.AddTarget(t2) + Expect(config.WriteConfig(c)).Error().ShouldNot(HaveOccurred()) + }) + + It("exits 0 and lists all targets with active marker on the second", func() { + session := runCommand("targets") + + Eventually(session).Should(Exit(0)) + Expect(session.Out).To(Say(server.URL())) + Expect(session.Out).To(Say(secondURL)) + }) + + It("marks only the active target with *", func() { + session := runCommand("targets") + + Eventually(session).Should(Exit(0)) + output := string(session.Out.Contents()) + Expect(output).To(ContainSubstring("* ")) + Expect(output).To(ContainSubstring(secondURL)) + }) + }) +}) diff --git a/docs/commands/targets.md b/docs/commands/targets.md new file mode 100644 index 0000000..e82212e --- /dev/null +++ b/docs/commands/targets.md @@ -0,0 +1,45 @@ +# targets + +[← Command Reference](../commands.md) + +List all registered targets. The active target is marked with `*`. + +## Usage + +``` +uaa targets +``` + +## Global Flags + +| Flag | Short | Description | +|------|-------|-------------| +| `--verbose` | `-v` | Print additional info on HTTP requests | + +## Output + +Each target is printed on its own line with a 1-based index. The currently active target is prefixed with `*`: + +``` +* 1: https://uaa.example.com + 2: http://localhost:8080/uaa +``` + +If no targets have been registered, the command prints `No targets set.` and exits 0. + +## Examples + +```bash +# Register two targets and list them +uaa target https://uaa.example.com +uaa target http://localhost:8080/uaa --skip-ssl-validation +uaa targets +``` + +## See Also + +- [target](target.md) — set or display the current target + +--- + +[← Command Reference](../commands.md)