-
Notifications
You must be signed in to change notification settings - Fork 13
Add targets command
#287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add targets command
#287
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)) | ||
| } | ||
| } | ||
|
|
||
| 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" | ||
| } | ||
| 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
|
||
|
|
||
| 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
|
||
| }) | ||
| }) | ||
| 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
|
||
|
|
||
| ## 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) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor:
cli.Loggeralready providesInfof, so you can avoid the extrafmt.Sprintfallocation/import here by usinglog.Infof(...)directly.