From 0e778052a1bc7980bcb58da9061e6b407bafec8a Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sun, 25 Apr 2021 12:51:01 -0700 Subject: [PATCH] Read cointop config environment variables --- cmd/commands/root.go | 99 ++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 31 deletions(-) diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 98832ef..53c129d 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -2,6 +2,8 @@ package cmd import ( "fmt" + "os" + "strconv" "github.com/miguelmota/cointop/cointop" "github.com/spf13/cobra" @@ -10,24 +12,24 @@ import ( // RootCmd ... func RootCmd() *cobra.Command { var version bool - var test bool - var clean bool - var reset bool - var hideMarketbar bool - var hideChart bool - var hideStatusbar bool - var onlyTable bool - var silent bool - var noCache bool var refreshRate uint - var config string - var cmcAPIKey string - var apiChoice string - var colorscheme string - var perPage = cointop.DefaultPerPage - var maxPages = cointop.DefaultMaxPages - var cacheDir string - var colorsDir string + test := getEnvBool("COINTOP_TEST") + clean := getEnvBool("COINTOP_CLEAN") + reset := getEnvBool("COINTOP_RESET") + hideMarketbar := getEnvBool("COINTOP_HIDE_MARKETBAR") + hideChart := getEnvBool("COINTOP_HIDE_CHART") + hideStatusbar := getEnvBool("COINTOP_HIDE_STATUSBAR") + onlyTable := getEnvBool("COINTOP_ONLY_TABLE") + silent := getEnvBool("COINTOP_SILENT") + noCache := getEnvBool("COINTOP_NO_CACHE") + colorscheme := os.Getenv("COINTOP_COLORSCHEME") + cacheDir := os.Getenv("COINTOP_CACHE_DIR") + colorsDir := os.Getenv("COINTOP_COLORS_DIR") + config := os.Getenv("COINTOP_CONFIG") + apiChoice := os.Getenv("COINTOP_API") + cmcAPIKey := os.Getenv("CMC_PRO_API_KEY") + perPage := cointop.DefaultPerPage + maxPages := cointop.DefaultMaxPages rootCmd := &cobra.Command{ Use: "cointop", @@ -77,6 +79,13 @@ See git.io/cointop for more info.`, if cmd.Flags().Changed("refresh-rate") { refreshRateP = &refreshRate } + if refreshRateP == nil { + value, ok := getEnvInt("COINTOP_REFRESH_RATE") + if ok { + uv := uint(value) + refreshRateP = &uv + } + } ct, err := cointop.NewCointop(&cointop.Config{ CacheDir: cacheDir, @@ -102,25 +111,53 @@ See git.io/cointop for more info.`, }, } - rootCmd.Flags().BoolVarP(&version, "version", "v", false, "Display current version") - rootCmd.Flags().BoolVarP(&test, "test", "", false, "Run test (for Homebrew)") - rootCmd.Flags().BoolVarP(&clean, "clean", "", false, "Wipe clean the cache") - rootCmd.Flags().BoolVarP(&reset, "reset", "", false, "Reset the config. Make sure to backup any relevant changes first!") - rootCmd.Flags().BoolVarP(&hideMarketbar, "hide-marketbar", "", false, "Hide the top marketbar") - rootCmd.Flags().BoolVarP(&hideChart, "hide-chart", "", false, "Hide the chart view") - rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", false, "Hide the bottom statusbar") - rootCmd.Flags().BoolVarP(&onlyTable, "only-table", "", false, "Show only the table. Hides the chart and top and bottom bars") - rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silence log ouput") - rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache") + rootCmd.Flags().BoolVarP(&version, "version", "v", version, "Display current version") + rootCmd.Flags().BoolVarP(&test, "test", "", test, "Run test (for Homebrew)") + rootCmd.Flags().BoolVarP(&clean, "clean", "", clean, "Wipe clean the cache") + rootCmd.Flags().BoolVarP(&reset, "reset", "", reset, "Reset the config. Make sure to backup any relevant changes first!") + rootCmd.Flags().BoolVarP(&hideMarketbar, "hide-marketbar", "", hideMarketbar, "Hide the top marketbar") + rootCmd.Flags().BoolVarP(&hideChart, "hide-chart", "", hideChart, "Hide the chart view") + rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", hideStatusbar, "Hide the bottom statusbar") + rootCmd.Flags().BoolVarP(&onlyTable, "only-table", "", onlyTable, "Show only the table. Hides the chart and top and bottom bars") + rootCmd.Flags().BoolVarP(&silent, "silent", "s", silent, "Silence log ouput") + rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", noCache, "No cache") rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh") rootCmd.Flags().UintVarP(&perPage, "per-page", "", perPage, "Per page") rootCmd.Flags().UintVarP(&maxPages, "max-pages", "", maxPages, "Max number of pages") - rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) - rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key") - rootCmd.Flags().StringVarP(&apiChoice, "api", "", "", "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"") - rootCmd.Flags().StringVarP(&colorscheme, "colorscheme", "", "", fmt.Sprintf("Colorscheme to use (default \"cointop\").\n%s", cointop.ColorschemeHelpString())) + rootCmd.Flags().StringVarP(&config, "config", "c", config, fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) + rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", cmcAPIKey, "Set the CoinMarketCap API key") + rootCmd.Flags().StringVarP(&apiChoice, "api", "", apiChoice, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"") + rootCmd.Flags().StringVarP(&colorscheme, "colorscheme", "", colorscheme, fmt.Sprintf("Colorscheme to use (default \"cointop\").\n%s", cointop.ColorschemeHelpString())) rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, fmt.Sprintf("Cache directory (default %s)", cointop.DefaultCacheDir)) rootCmd.Flags().StringVarP(&colorsDir, "colors-dir", "", colorsDir, "Colorschemes directory") return rootCmd } + +func getEnvBool(key string) bool { + val := os.Getenv(key) + if val == "" { + return false + } + + v, err := strconv.ParseBool(val) + if err != nil { + return false + } + + return v +} + +func getEnvInt(key string) (int, bool) { + val := os.Getenv(key) + if val == "" { + return 0, false + } + + v, err := strconv.Atoi(val) + if err != nil { + return 0, false + } + + return v, true +}