Read cointop config environment variables

pull/133/head
Miguel Mota 3 years ago
parent 9fab6bd5b9
commit 0e778052a1
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -2,6 +2,8 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"strconv"
"github.com/miguelmota/cointop/cointop" "github.com/miguelmota/cointop/cointop"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -10,24 +12,24 @@ import (
// RootCmd ... // RootCmd ...
func RootCmd() *cobra.Command { func RootCmd() *cobra.Command {
var version bool 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 refreshRate uint
var config string test := getEnvBool("COINTOP_TEST")
var cmcAPIKey string clean := getEnvBool("COINTOP_CLEAN")
var apiChoice string reset := getEnvBool("COINTOP_RESET")
var colorscheme string hideMarketbar := getEnvBool("COINTOP_HIDE_MARKETBAR")
var perPage = cointop.DefaultPerPage hideChart := getEnvBool("COINTOP_HIDE_CHART")
var maxPages = cointop.DefaultMaxPages hideStatusbar := getEnvBool("COINTOP_HIDE_STATUSBAR")
var cacheDir string onlyTable := getEnvBool("COINTOP_ONLY_TABLE")
var colorsDir string 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{ rootCmd := &cobra.Command{
Use: "cointop", Use: "cointop",
@ -77,6 +79,13 @@ See git.io/cointop for more info.`,
if cmd.Flags().Changed("refresh-rate") { if cmd.Flags().Changed("refresh-rate") {
refreshRateP = &refreshRate refreshRateP = &refreshRate
} }
if refreshRateP == nil {
value, ok := getEnvInt("COINTOP_REFRESH_RATE")
if ok {
uv := uint(value)
refreshRateP = &uv
}
}
ct, err := cointop.NewCointop(&cointop.Config{ ct, err := cointop.NewCointop(&cointop.Config{
CacheDir: cacheDir, 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(&version, "version", "v", version, "Display current version")
rootCmd.Flags().BoolVarP(&test, "test", "", false, "Run test (for Homebrew)") rootCmd.Flags().BoolVarP(&test, "test", "", test, "Run test (for Homebrew)")
rootCmd.Flags().BoolVarP(&clean, "clean", "", false, "Wipe clean the cache") rootCmd.Flags().BoolVarP(&clean, "clean", "", clean, "Wipe clean the cache")
rootCmd.Flags().BoolVarP(&reset, "reset", "", false, "Reset the config. Make sure to backup any relevant changes first!") rootCmd.Flags().BoolVarP(&reset, "reset", "", reset, "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(&hideMarketbar, "hide-marketbar", "", hideMarketbar, "Hide the top marketbar")
rootCmd.Flags().BoolVarP(&hideChart, "hide-chart", "", false, "Hide the chart view") rootCmd.Flags().BoolVarP(&hideChart, "hide-chart", "", hideChart, "Hide the chart view")
rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", false, "Hide the bottom statusbar") rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", hideStatusbar, "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(&onlyTable, "only-table", "", onlyTable, "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(&silent, "silent", "s", silent, "Silence log ouput")
rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache") 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(&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(&perPage, "per-page", "", perPage, "Per page")
rootCmd.Flags().UintVarP(&maxPages, "max-pages", "", maxPages, "Max number of pages") 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(&config, "config", "c", config, fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key") rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", cmcAPIKey, "Set the CoinMarketCap API key")
rootCmd.Flags().StringVarP(&apiChoice, "api", "", "", "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"") rootCmd.Flags().StringVarP(&apiChoice, "api", "", apiChoice, "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(&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(&cacheDir, "cache-dir", "", cacheDir, fmt.Sprintf("Cache directory (default %s)", cointop.DefaultCacheDir))
rootCmd.Flags().StringVarP(&colorsDir, "colors-dir", "", colorsDir, "Colorschemes directory") rootCmd.Flags().StringVarP(&colorsDir, "colors-dir", "", colorsDir, "Colorschemes directory")
return rootCmd 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
}

Loading…
Cancel
Save