diff --git a/cmd/target.go b/cmd/target.go index f2f5a55..f24378f 100644 --- a/cmd/target.go +++ b/cmd/target.go @@ -45,11 +45,12 @@ func UpdateTargetCmd(cfg config.Config, newTarget string, log cli.Logger) error } cfg.AddTarget(target) - api := GetUnauthenticatedAPIFromConfig(cfg) - _, err := api.GetInfo() - - if err != nil { - return errors.New(fmt.Sprintf("The target %s could not be set: %v", newTarget, err.Error())) + if !forceTarget { + api := GetUnauthenticatedAPIFromConfig(cfg) + _, err := api.GetInfo() + if err != nil { + return errors.New(fmt.Sprintf("The target %s could not be set: %v", newTarget, err.Error())) + } } config.WriteConfig(cfg) @@ -77,9 +78,12 @@ var targetCmd = &cobra.Command{ }, } +var forceTarget bool + func init() { RootCmd.AddCommand(targetCmd) targetCmd.Flags().BoolVarP(&skipSSLValidation, "skip-ssl-validation", "k", false, "Disable security validation on requests to this target") + targetCmd.Flags().BoolVarP(&forceTarget, "force", "f", false, "Set target without verifying connectivity") targetCmd.Annotations = make(map[string]string) targetCmd.Annotations[INTRO_CATEGORY] = "true" } diff --git a/cmd/target_test.go b/cmd/target_test.go index 2c18bd3..6ef2b1b 100644 --- a/cmd/target_test.go +++ b/cmd/target_test.go @@ -118,6 +118,32 @@ var _ = Describe("Target", func() { }) }) + Describe("when --force is set", func() { + It("saves the target without making a connectivity check", func() { + // No handler registered — any real request would cause the test server to fail. + // We verify no /info call is made by not registering a handler and still expecting success. + session := runCommand("target", server.URL(), "--force") + + Eventually(session).Should(Exit(0)) + Eventually(session.Out).Should(Say("Target set to " + server.URL())) + Expect(config.ReadConfig().GetActiveTarget().BaseUrl).To(Equal(server.URL())) + }) + + It("saves the target even when the UAA is unreachable", func() { + session := runCommand("target", "http://does-not-exist.invalid", "--force") + + Eventually(session).Should(Exit(0)) + Eventually(session.Out).Should(Say("Target set to http://does-not-exist.invalid")) + Expect(config.ReadConfig().GetActiveTarget().BaseUrl).To(Equal("http://does-not-exist.invalid")) + }) + + It("respects --skip-ssl-validation together with --force", func() { + runCommand("target", server.URL(), "--force", "--skip-ssl-validation") + + Expect(config.ReadConfig().GetActiveTarget().SkipSSLValidation).To(BeTrue()) + }) + }) + Describe("when the UAA cannot be reached", func() { BeforeEach(func() { server.RouteToHandler("GET", "/info", diff --git a/docs/commands/target.md b/docs/commands/target.md index 979f0a8..8131703 100644 --- a/docs/commands/target.md +++ b/docs/commands/target.md @@ -20,6 +20,7 @@ When called with no arguments, displays the currently targeted UAA URL and its s | Flag | Short | Default | Description | |------|-------|---------|-------------| | `--skip-ssl-validation` | `-k` | `false` | Disable SSL certificate validation for requests to this target | +| `--force` | `-f` | `false` | Save the target without verifying connectivity (skip the `/info` check) | ## Global Flags @@ -36,6 +37,9 @@ uaa target https://uaa.example.com # Set a target, skipping SSL validation uaa target https://uaa.example.com --skip-ssl-validation +# Set a target without checking connectivity (useful for unreachable or not-yet-running UAAs) +uaa target https://uaa.example.com --force + # Display the current target uaa target ```