Skip to content

Commit

Permalink
Use cobra to print from the commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Sep 17, 2023
1 parent 9bcb251 commit b872519
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
26 changes: 13 additions & 13 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func generateConfig(
if exists && forceRewriteFile {
verb = "overwritten"
}
logger.Printf("Config file '%s' was %s successfully.", configFile, verb)
cmd.Printf("Config file '%s' was %s successfully.", configFile, verb)
}

func lintConfig(cmd *cobra.Command, fileType configFileType, configFile string) {
Expand Down Expand Up @@ -163,37 +163,37 @@ func lintConfig(cmd *cobra.Command, fileType configFileType, configFile string)
logger.Fatalf("Error validating %s config: %s\n", string(fileType), err)
}

logger.Printf("%s config is valid\n", fileType)
cmd.Printf("%s config is valid\n", fileType)
}

func listPlugins(cmd *cobra.Command, pluginConfigFile string, onlyEnabled bool) {
logger := log.New(cmd.OutOrStdout(), "", 0)

// Load the plugin config file.
conf := config.NewConfig(context.TODO(), "", pluginConfigFile)
conf.LoadDefaults(context.TODO())
conf.LoadPluginConfigFile(context.TODO())
conf.UnmarshalPluginConfig(context.TODO())

if len(conf.Plugin.Plugins) != 0 {
logger.Printf("Total plugins: %d\n", len(conf.Plugin.Plugins))
logger.Println("Plugins:")
cmd.Printf("Total plugins: %d\n", len(conf.Plugin.Plugins))
cmd.Println("Plugins:")
} else {
cmd.Println("No plugins found")
}

// Print the list of plugins.
for _, plugin := range conf.Plugin.Plugins {
if onlyEnabled && !plugin.Enabled {
continue
}
logger.Printf(" Name: %s\n", plugin.Name)
logger.Printf(" Enabled: %t\n", plugin.Enabled)
logger.Printf(" Path: %s\n", plugin.LocalPath)
logger.Printf(" Args: %s\n", strings.Join(plugin.Args, " "))
logger.Println(" Env:")
cmd.Printf(" Name: %s\n", plugin.Name)
cmd.Printf(" Enabled: %t\n", plugin.Enabled)
cmd.Printf(" Path: %s\n", plugin.LocalPath)
cmd.Printf(" Args: %s\n", strings.Join(plugin.Args, " "))
cmd.Println(" Env:")
for _, env := range plugin.Env {
logger.Printf(" %s\n", env)
cmd.Printf(" %s\n", env)
}
logger.Printf(" Checksum: %s\n", plugin.Checksum)
cmd.Printf(" Checksum: %s\n", plugin.Checksum)
}
}

Expand Down
6 changes: 2 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/gatewayd-io/gatewayd/config"
"github.com/spf13/cobra"
)
Expand All @@ -11,8 +9,8 @@ import (
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version information",
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(config.VersionInfo()) //nolint:forbidigo
Run: func(cmd *cobra.Command, _ []string) {
cmd.Println(config.VersionInfo()) //nolint:forbidigo
},
}

Expand Down

0 comments on commit b872519

Please sign in to comment.