Skip to content

Commit

Permalink
fix: improve messages to users (api7#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
pottekkat authored Sep 15, 2023
1 parent 8044afd commit 4dd5702
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 49 deletions.
14 changes: 7 additions & 7 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
func newConfigureCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "configure",
Short: "Configure the connection of API7",
Long: `The ping command can be used to configure the connection to the API7.`,
Short: "Configure ADC with APISIX instance",
Long: `Configures ADC with APISIX's server address and token.`,
RunE: func(cmd *cobra.Command, args []string) error {
return saveConfiguration()
},
Expand All @@ -30,20 +30,20 @@ func newConfigureCmd() *cobra.Command {

func saveConfiguration() error {
if rootConfig.Server != "" && rootConfig.Token != "" {
color.Yellow("adc has been configured, you can use `adc ping` to test the connection")
color.Yellow("ADC configured. Run `adc ping` to test the configuration.")
return nil
}

reader := bufio.NewReader(os.Stdin)
if rootConfig.Server == "" {
fmt.Println("Please input the server address: ")
fmt.Println("Please enter the APISIX server address: ")
server, _ := reader.ReadString('\n')
rootConfig.Server = strings.TrimSpace(server)
}

if rootConfig.Token == "" {

fmt.Println("Please input the Token: ")
fmt.Println("Please enter the token: ")
token, _ := reader.ReadString('\n')
rootConfig.Token = strings.TrimSpace(token)
}
Expand All @@ -52,11 +52,11 @@ func saveConfiguration() error {
viper.Set("server", rootConfig.Server)
viper.Set("token", rootConfig.Token)
if err := viper.SafeWriteConfig(); err != nil {
color.Red("failed configure ADC")
color.Red("Failed to configure ADC")
return err
}

color.Green("Successfully configure ADC")
color.Green("ADC configured successfully!")

return pingAPISIX()
}
4 changes: 2 additions & 2 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
func newDiffCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "diff",
Short: "Diff the configurations between local and API7",
Long: `The diff command can be used to diff the configurations between local and API7.`,
Short: "Show the differences between the local and existing APISIX configuration",
Long: `Shows the differences in the configuration between the local confguration file and the connected APISIX instance.`,
RunE: func(cmd *cobra.Command, args []string) error {
checkConfig()

Expand Down
8 changes: 4 additions & 4 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
func newDumpCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dump",
Short: "Dump the configurations of API7",
Long: `The dump command can be used to dump the configurations to the API7.`,
Short: "Dump the APISIX configuration",
Long: `Dumps the configuration of the connected APISIX instance to a local file.`,
RunE: func(cmd *cobra.Command, args []string) error {
checkConfig()

Expand All @@ -41,11 +41,11 @@ func newDumpCmd() *cobra.Command {
func dumpConfiguration(cmd *cobra.Command) error {
path, err := cmd.Flags().GetString("output")
if err != nil {
color.Red("Get file path failed: %v", err)
color.Red("Failed to get output file path: %v", err)
return err
}
if path == "" {
color.Red("Output path is empty. Example: adc dump -o config.yaml")
color.Red("Output file path is empty. Please specify a file path: adc dump -o config.yaml")
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
func newPingCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ping",
Short: "Verify the connection to the APISIX",
Long: `The ping command can be used to verify the connection to the APISIX.`,
Short: "Verify connectivity with APISIX",
Long: `Pings the configured APISIX instance to verify connectivity.`,
RunE: func(cmd *cobra.Command, args []string) error {
checkConfig()

Expand All @@ -40,9 +40,9 @@ func pingAPISIX() error {
UpstreamId: "abcd",
})
if err != nil {
color.Red("failed to ping APISIX: %v", err.Error())
color.Red("Failed to ping APISIX: %v", err.Error())
} else {
color.Green("Successfully connected to APISIX")
color.Green("Connected to APISIX successfully!")
}
return nil
}
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ var (
func newRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "adc",
Short: "API7 Declarative CLI",
Long: `A CLI tool for API7 Declarative configurations.
Short: "APISIX Declarative CLI",
Long: `A command line interface for configuring APISIX declaratively.
It can be used to dump, diff, sync configurations to API7 server.
It can be used to validate, dump, diff, and sync configurations with an APISIX instance.
`,
}
cobra.OnInitialize(initConfig)
Expand Down Expand Up @@ -66,15 +66,15 @@ func initConfig() {
viper.AddConfigPath("$HOME/")
err := viper.ReadInConfig()
if err != nil {
color.Red("Fatal to read config file, please run `adc configure` to configure the client first.")
color.Red("Failed to read configuration file, please run `adc configure` first to configure ADC.")
return
}

rootConfig.Server = viper.GetString("server")
rootConfig.Token = viper.GetString("token")
cluser := apisix.NewCluster(context.Background(), rootConfig.Server, rootConfig.Token)
if err != nil {
color.RedString("Fatal to create cluster: %v", err)
color.RedString("Failed to create a new cluster: %v", err)
return
}
rootConfig.APISIXCluster = cluser
Expand Down
18 changes: 9 additions & 9 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
func newSyncCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sync",
Short: "Sync the configurations from local to API7",
Long: `The sync command can be used to sync the configurations from local to API7.`,
Short: "Sync local configuration to APISIX",
Long: `Syncs the configuration in adc.yaml (or other provided file) to APISIX.`,
RunE: func(cmd *cobra.Command, args []string) error {
checkConfig()

Expand All @@ -39,31 +39,31 @@ func newSyncCmd() *cobra.Command {
func sync(cmd *cobra.Command, dryRun bool) error {
file, err := cmd.Flags().GetString("file")
if err != nil {
color.Red("Get file path failed: %v", err)
color.Red("Failed to get the configuration file: %v", err)
return err
}

config, err := common.GetContentFromFile(file)
if err != nil {
color.Red("Get file content failed: %v", err)
color.Red("Failed to read configuration file: %v", err)
return err
}

remoteConfig, err := common.GetContentFromRemote(rootConfig.APISIXCluster)
if err != nil {
color.Red("Failed to get remote config: %v", err)
color.Red("Failed to get remote configuration: %v", err)
return err
}

d, err := differ.NewDiffer(config, remoteConfig)
if err != nil {
color.Red("Failed to create differ: %v", err)
color.Red("Failed to create a Differ object: %v", err)
return err
}

events, err := d.Diff()
if err != nil {
color.Red("Failed to diff: %v", err)
color.Red("Failed to compare local and remote configuration: %v", err)
return err
}

Expand All @@ -84,14 +84,14 @@ func sync(cmd *cobra.Command, dryRun bool) error {

str, err := event.Output()
if err != nil {
color.Red("Failed to get output: %v", err)
color.Red("Failed to get output of the event: %v", err)
return err
}

if !dryRun {
err = event.Apply(rootConfig.APISIXCluster)
if err != nil {
color.Red("Failed to apply: %v", err)
color.Red("Failed to apply configuration: %v", err)
return err
}
time.Sleep(100 * time.Millisecond)
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func checkConfig() {
if rootConfig.Server == "" || rootConfig.Token == "" {
color.Yellow("adc hasn't been configured, you can use `adc configure` to configure adc")
color.Yellow("ADC isn't configured, run `adc configure` to configure ADC.")
os.Exit(0)
}
}
16 changes: 8 additions & 8 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ import (
func newValidateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validate",
Short: "Validate the configurations",
Long: `The validate command can be used to validate the configurations.`,
Short: "Validate the provided configuration file",
Long: `Validates the provided configuration file with the connected APISIX instance.`,
RunE: func(cmd *cobra.Command, args []string) error {
checkConfig()

file, err := cmd.Flags().GetString("file")
if err != nil {
color.Red("Get file path failed: %v", err)
color.Red("Failed to get file path: %v", err)
return err
}
if file == "" {
color.Red("Input path is empty. Example: adc validate -f config.yaml")
color.Red("File path is empty. Please specify a file path: adc validate -f config.yaml")
return nil
}

d, err := common.GetContentFromFile(file)
if err != nil {
color.Red("Get file content failed: %v", err)
color.Red("Failed to read configuration file: %v", err)
return err
}

msg := fmt.Sprintf("Get file content success: config name: %v, version: %v", d.Name, d.Version)
msg := fmt.Sprintf("Read configuration file successfully: config name: %v, version: %v", d.Name, d.Version)
changed := false
if len(d.Routes) > 0 {
msg += fmt.Sprintf(", routes: %v", len(d.Routes))
Expand Down Expand Up @@ -84,7 +84,7 @@ func newValidateCmd() *cobra.Command {

err = validateContent(d)
if err != nil {
color.Red("Command failed: %v", err)
color.Red("Failed to validate configuration file: %v", err)
return err
}
return nil
Expand All @@ -111,7 +111,7 @@ func validateContent(c *types.Configuration) error {
color.Red(err.Error())
}
} else {
color.Green("Validate file content success")
color.Green("Successfully validated configuration file!")
}
return nil
}
6 changes: 3 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ var (
func newVersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version of adc",
Long: `The version command can be used to print the version of adc.`,
Short: "Print the version of ADC",
Long: `Prints the version of ADC. See https://github.com/api7/adc for details on how to update.`,
Run: func(cmd *cobra.Command, args []string) {
color.Green("adc version: %s - %s\n", VERSION, GitRevision)
color.Green("ADC version: %s - %s\n", VERSION, GitRevision)
},
}

Expand Down
2 changes: 1 addition & 1 deletion test/cli/suites-consumer-group/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc validate` consumer group tests", func() {
ginkgo.It("should validate consumer group schema", func() {
validateOutput, err := s.Validate("suites-consumer-group/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(validateOutput).To(gomega.Equal("Get file content success: config name: , version: , consumers: 1, consumer_groups: 1.\nValidate file content success\n"))
gomega.Expect(validateOutput).To(gomega.Equal("Read configuration file successfully: config name: , version: , consumers: 1, consumer_groups: 1.\nSuccessfully validated configuration file!\n"))
})
})
})
2 changes: 1 addition & 1 deletion test/cli/suites-consumer/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc validate` consumer tests", func() {
ginkgo.It("should validate consumer schema", func() {
validateOutput, err := s.Validate("suites-consumer/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(validateOutput).To(gomega.Equal("Get file content success: config name: , version: , consumers: 1.\nValidate file content success\n"))
gomega.Expect(validateOutput).To(gomega.Equal("Read configuration file successfully: config name: , version: , consumers: 1.\nSuccessfully validated configuration file!\n"))
})
})
})
2 changes: 1 addition & 1 deletion test/cli/suites-global-rule/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc validate` global rule tests", func() {
ginkgo.It("should validate global rule schema", func() {
validateOutput, err := s.Validate("suites-global-rule/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(validateOutput).To(gomega.Equal("Get file content success: config name: , version: , global_rules: 1.\nValidate file content success\n"))
gomega.Expect(validateOutput).To(gomega.Equal("Read configuration file successfully: config name: , version: , global_rules: 1.\nSuccessfully validated configuration file!\n"))
})
})
})
2 changes: 1 addition & 1 deletion test/cli/suites-plugin-config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc validate` plugin config tests", func() {
ginkgo.It("should validate plugin config schema", func() {
validateOutput, err := s.Validate("suites-plugin-config/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(validateOutput).To(gomega.Equal("Get file content success: config name: , version: , plugin_configs: 1.\nValidate file content success\n"))
gomega.Expect(validateOutput).To(gomega.Equal("Read configuration file successfully: config name: , version: , plugin_configs: 1.\nSuccessfully validated configuration file!\n"))
})
})
})
2 changes: 1 addition & 1 deletion test/cli/suites/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("Successfully connected to APISIX\n"))
gomega.Expect(output).To(gomega.Equal("Connected to APISIX successfully!\n"))
})
})
})
2 changes: 1 addition & 1 deletion test/cli/suites/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc validate` tests", func() {
ginkgo.It("should validate schema", func() {
validateOutput, err := s.Validate("testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(validateOutput).To(gomega.Equal("Get file content success: config name: test, version: 1.0.0, routes: 2, services: 2.\nValidate file content success\n"))
gomega.Expect(validateOutput).To(gomega.Equal("Read configuration file successfully: config name: test, version: 1.0.0, routes: 2, services: 2.\nSuccessfully validated configuration file!\n"))
})
})
})

0 comments on commit 4dd5702

Please sign in to comment.