Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd/targets.go
Original file line number Diff line number Diff line change
@@ -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))
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: cli.Logger already provides Infof, so you can avoid the extra fmt.Sprintf allocation/import here by using log.Infof(...) directly.

Copilot uses AI. Check for mistakes.
}
}

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"
}
78 changes: 78 additions & 0 deletions cmd/targets_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
Comment on lines +61 to +67
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test description says the active marker is on the second target, but the assertions only check that both URLs appear. Please also assert that the * marker (and index) are printed on the active target’s line (e.g., matching the full line containing * + index + second URL).

Copilot uses AI. Check for mistakes.

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))
})
Comment on lines +73 to +76
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks that the output contains "* ", but it doesn’t verify that only one target is marked active (the test would still pass if multiple lines had *). Consider asserting the count of active markers is exactly 1, and that it appears on the active target’s line.

Copilot uses AI. Check for mistakes.
})
})
45 changes: 45 additions & 0 deletions docs/commands/targets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# targets

[← Command Reference](../commands.md)

List all registered targets. The active target is marked with `*`.
Comment on lines +1 to +5
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targets.md is added, but the command isn’t discoverable from the command index (docs/commands.md) yet. Add a targets row under "Getting Started" (near target) so the new documentation page is reachable from the reference list.

Copilot uses AI. Check for mistakes.

## 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)
Loading