Add option flag to silence log output

pull/58/head
Miguel Mota 4 years ago
parent e33854bd9f
commit 2d54de72c7

@ -11,7 +11,7 @@ import (
// Execute executes the program // Execute executes the program
func Execute() { func Execute() {
var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable bool var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent bool
var refreshRate uint var refreshRate uint
var config, cmcAPIKey, apiChoice, colorscheme, coin, currency string var config, cmcAPIKey, apiChoice, colorscheme, coin, currency string
@ -43,14 +43,18 @@ For more information, visit: https://github.com/miguelmota/cointop`,
// NOTE: if reset flag enabled, reset and run cointop // NOTE: if reset flag enabled, reset and run cointop
if reset { if reset {
if err := cointop.Reset(); err != nil { if err := cointop.Reset(&cointop.ResetConfig{
Log: !silent,
}); err != nil {
return err return err
} }
} }
// NOTE: if clean flag enabled, clean and run cointop // NOTE: if clean flag enabled, clean and run cointop
if clean { if clean {
if err := cointop.Clean(); err != nil { if err := cointop.Clean(&cointop.CleanConfig{
Log: !silent,
}); err != nil {
return err return err
} }
} }
@ -87,6 +91,7 @@ For more information, visit: https://github.com/miguelmota/cointop`,
rootCmd.Flags().BoolVarP(&hideChart, "hide-chart", "", false, "Hide the chart view") 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(&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(&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().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().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) 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(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key")
@ -108,7 +113,9 @@ For more information, visit: https://github.com/miguelmota/cointop`,
Long: `The clean command clears the cache`, Long: `The clean command clears the cache`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
// NOTE: if clean command, clean but don't run cointop // NOTE: if clean command, clean but don't run cointop
return cointop.Clean() return cointop.Clean(&cointop.CleanConfig{
Log: true,
})
}, },
} }
@ -118,7 +125,9 @@ For more information, visit: https://github.com/miguelmota/cointop`,
Long: `The reset command resets the config and clears the cache`, Long: `The reset command resets the config and clears the cache`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
// NOTE: if reset command, reset but don't run cointop // NOTE: if reset command, reset but don't run cointop
return cointop.Reset() return cointop.Reset(&cointop.ResetConfig{
Log: true,
})
}, },
} }

@ -137,7 +137,10 @@ type APIKeys struct {
cmc string cmc string
} }
// DefaultColorscheme ...
var DefaultColorscheme = "cointop" var DefaultColorscheme = "cointop"
// DefaultConfigFilepath ...
var DefaultConfigFilepath = "~/.config/cointop/config.toml" var DefaultConfigFilepath = "~/.config/cointop/config.toml"
// NewCointop initializes cointop // NewCointop initializes cointop
@ -389,8 +392,12 @@ func PrintPrice(config *PriceConfig) error {
return nil return nil
} }
type CleanConfig struct {
Log bool
}
// Clean ... // Clean ...
func Clean() error { func Clean(config *CleanConfig) error {
tmpPath := "/tmp" tmpPath := "/tmp"
if _, err := os.Stat(tmpPath); !os.IsNotExist(err) { if _, err := os.Stat(tmpPath); !os.IsNotExist(err) {
files, err := ioutil.ReadDir(tmpPath) files, err := ioutil.ReadDir(tmpPath)
@ -401,7 +408,9 @@ func Clean() error {
for _, f := range files { for _, f := range files {
if strings.HasPrefix(f.Name(), "fcache.") { if strings.HasPrefix(f.Name(), "fcache.") {
file := fmt.Sprintf("%s/%s", tmpPath, f.Name()) file := fmt.Sprintf("%s/%s", tmpPath, f.Name())
fmt.Printf("removing %s\n", file) if config.Log {
fmt.Printf("removing %s\n", file)
}
if err := os.Remove(file); err != nil { if err := os.Remove(file); err != nil {
return err return err
} }
@ -409,25 +418,39 @@ func Clean() error {
} }
} }
fmt.Println("cointop cache has been cleaned") if config.Log {
fmt.Println("cointop cache has been cleaned")
}
return nil return nil
} }
type ResetConfig struct {
Log bool
}
// Reset ... // Reset ...
func Reset() error { func Reset(config *ResetConfig) error {
if err := Clean(); err != nil { if err := Clean(&CleanConfig{
Log: config.Log,
}); err != nil {
return err return err
} }
// default config path // default config path
configPath := fmt.Sprintf("%s%s", UserPreferredHomeDir(), "/.cointop") configPath := fmt.Sprintf("%s%s", UserPreferredHomeDir(), "/.cointop")
if _, err := os.Stat(configPath); !os.IsNotExist(err) { if _, err := os.Stat(configPath); !os.IsNotExist(err) {
fmt.Printf("removing %s\n", configPath) if config.Log {
fmt.Printf("removing %s\n", configPath)
}
if err := os.RemoveAll(configPath); err != nil { if err := os.RemoveAll(configPath); err != nil {
return err return err
} }
} }
fmt.Println("cointop has been reset") if config.Log {
fmt.Println("cointop has been reset")
}
return nil return nil
} }

Loading…
Cancel
Save