diff --git a/cmd/configure.go b/cmd/configure.go index be09264..c4b9163 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -147,20 +147,32 @@ func saveConfiguration(cmd *cobra.Command) error { reader := bufio.NewReader(os.Stdin) if rootConfig.Server == "" { fmt.Println("Please enter the APISIX server address: ") - server, _ := reader.ReadString('\n') + server, err := reader.ReadString('\n') + if err != nil { + return err + } rootConfig.Server = strings.TrimSpace(server) } + if !strings.HasPrefix(rootConfig.Server, "http://") && !strings.HasPrefix(rootConfig.Server, "https://") { + color.Yellow("APISIX address " + rootConfig.Server + " is configured without protocol, using HTTP") + rootConfig.Server = "http://" + rootConfig.Server + } + rootConfig.Server = strings.TrimSuffix(rootConfig.Server, "/") + _, err = url.Parse(rootConfig.Server) if err != nil { color.Red("Parse APISIX server address failed: %v", err) return err } - if rootConfig.Token == "" { + if rootConfig.Token == "" || overwrite { fmt.Println("Please enter the APISIX token: ") - token, _ := reader.ReadString('\n') - rootConfig.Token = strings.TrimSpace(token) + token, err := reader.ReadString('\n') + if err != nil { + return err + } + rootConfig.Token = strings.TrimSpace(string(token)) } // use viper to save the configuration diff --git a/cmd/ping.go b/cmd/ping.go index 45c27c0..cfc2d90 100644 --- a/cmd/ping.go +++ b/cmd/ping.go @@ -10,7 +10,6 @@ import ( "github.com/spf13/cobra" "github.com/api7/adc/pkg/api/apisix" - "github.com/api7/adc/pkg/api/apisix/types" ) // newPingCmd represents the ping command @@ -36,16 +35,11 @@ func pingAPISIX() error { return err } - err = cluster.Route().Validate(context.Background(), &types.Route{ - ID: "test", - Name: "test", - Uri: "*", - UpstreamID: "abcd", - }) + err = cluster.Ping() if err != nil { - color.Red("Failed to ping APISIX: %v", err.Error()) + color.Red("Failed to ping backend, response: %s", err.Error()) } else { - color.Green("Connected to APISIX successfully!") + color.Green("Connected to backend successfully!") } return nil } diff --git a/cmd/root.go b/cmd/root.go index 05e1873..44cf5e9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -64,7 +64,7 @@ func initConfig() { if cfgFile == "" { home, err := homedir.Dir() if err != nil { - color.Red("Failed to get home dir: %s", err) + color.Red("Failed to get home dir: %s", err.Error()) os.Exit(1) } viper.AddConfigPath(home) @@ -79,7 +79,7 @@ func initConfig() { color.Yellow("Config file not found at %s. Creating...", cfgFile) _, err := os.Create(os.ExpandEnv(cfgFile)) if err != nil { - color.Red("Failed to initialize configuration file: %s", err) + color.Red("Failed to initialize configuration file: %s", err.Error()) } } @@ -97,7 +97,7 @@ func initConfig() { rootConfig.Insecure = viper.GetBool("insecure") cluster, err := apisix.NewCluster(context.Background(), rootConfig.ClientConfig) if err != nil { - color.RedString("Failed to create a new cluster: %v", err) + color.RedString("Failed to create a new cluster: %v", err.Error()) return } rootConfig.APISIXCluster = cluster diff --git a/pkg/api/apisix/apisix.go b/pkg/api/apisix/apisix.go index 78b7116..56ced3b 100644 --- a/pkg/api/apisix/apisix.go +++ b/pkg/api/apisix/apisix.go @@ -17,6 +17,7 @@ type Cluster interface { PluginMetadata() PluginMetadata StreamRoute() StreamRoute Upstream() Upstream + Ping() error } type ResourceClient[T any] interface { diff --git a/pkg/api/apisix/cluster.go b/pkg/api/apisix/cluster.go index 1cbe4a0..89effab 100644 --- a/pkg/api/apisix/cluster.go +++ b/pkg/api/apisix/cluster.go @@ -142,3 +142,8 @@ func (c *cluster) StreamRoute() StreamRoute { func (c *cluster) Upstream() Upstream { return c.upstream } + +func (c *cluster) Ping() error { + _, err := c.Route().List(context.Background()) + return err +} diff --git a/test/cli/suites-basic/ping.go b/test/cli/suites-basic/ping.go index 22e51a2..f4a461a 100644 --- a/test/cli/suites-basic/ping.go +++ b/test/cli/suites-basic/ping.go @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc ping` tests", func() { ginkgo.It("should connect to APISIX", func() { output, err := s.Ping() gomega.Expect(err).To(gomega.BeNil()) - gomega.Expect(output).To(gomega.Equal("Connected to APISIX successfully!\n")) + gomega.Expect(output).To(gomega.Equal("Connected to backend successfully!\n")) }) }) }) diff --git a/test/mtls/suites-basic/ping.go b/test/mtls/suites-basic/ping.go index 66669ce..f3db278 100644 --- a/test/mtls/suites-basic/ping.go +++ b/test/mtls/suites-basic/ping.go @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc ping` tests", func() { ginkgo.It("should connect to APISIX", func() { output, err := s.Ping() gomega.Expect(err).To(gomega.BeNil()) - gomega.Expect(output).To(gomega.Equal("Connected to APISIX successfully!\n")) + gomega.Expect(output).To(gomega.Equal("Connected to backend successfully!\n")) }) }) })