mirror of https://github.com/miguelmota/cointop
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
7.4 KiB
Go
220 lines
7.4 KiB
Go
6 years ago
|
package cmd
|
||
|
|
||
|
import (
|
||
4 years ago
|
"fmt"
|
||
4 years ago
|
"time"
|
||
4 years ago
|
|
||
6 years ago
|
"github.com/miguelmota/cointop/cointop"
|
||
4 years ago
|
"github.com/miguelmota/cointop/cointop/common/filecache"
|
||
4 years ago
|
cssh "github.com/miguelmota/cointop/cointop/ssh"
|
||
5 years ago
|
"github.com/spf13/cobra"
|
||
6 years ago
|
)
|
||
|
|
||
5 years ago
|
// Execute executes the program
|
||
|
func Execute() {
|
||
4 years ago
|
var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent, noCache bool
|
||
5 years ago
|
var refreshRate uint
|
||
5 years ago
|
var config, cmcAPIKey, apiChoice, colorscheme, coin, currency string
|
||
4 years ago
|
cacheDir := filecache.DefaultCacheDir
|
||
5 years ago
|
|
||
4 years ago
|
rootCmd := &cobra.Command{
|
||
5 years ago
|
Use: "cointop",
|
||
|
Short: "Cointop is an interactive terminal based app for tracking cryptocurrencies",
|
||
|
Long: `
|
||
|
_ _
|
||
|
___ ___ (_)_ __ | |_ ___ _ __
|
||
|
/ __/ _ \| | '_ \| __/ _ \| '_ \
|
||
|
| (_| (_) | | | | | || (_) | |_) |
|
||
|
\___\___/|_|_| |_|\__\___/| .__/
|
||
|
|_|
|
||
|
|
||
|
Cointop is a fast and lightweight interactive terminal based UI application for tracking and monitoring cryptocurrency coin stats in real-time.
|
||
|
|
||
|
For more information, visit: https://github.com/miguelmota/cointop`,
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
if version {
|
||
|
cointop.PrintVersion()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
if test {
|
||
|
// TODO: deprecate test flag, only have test command
|
||
|
doTest()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// NOTE: if reset flag enabled, reset and run cointop
|
||
|
if reset {
|
||
4 years ago
|
if err := cointop.Reset(&cointop.ResetConfig{
|
||
|
Log: !silent,
|
||
|
}); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// NOTE: if clean flag enabled, clean and run cointop
|
||
|
if clean {
|
||
4 years ago
|
if err := cointop.Clean(&cointop.CleanConfig{
|
||
|
Log: !silent,
|
||
|
}); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
var refreshRateP *uint
|
||
|
if cmd.Flags().Changed("refresh-rate") {
|
||
|
refreshRateP = &refreshRate
|
||
|
}
|
||
|
|
||
|
ct, err := cointop.NewCointop(&cointop.Config{
|
||
4 years ago
|
CacheDir: cacheDir,
|
||
4 years ago
|
NoCache: noCache,
|
||
5 years ago
|
ConfigFilepath: config,
|
||
|
CoinMarketCapAPIKey: cmcAPIKey,
|
||
|
APIChoice: apiChoice,
|
||
|
Colorscheme: colorscheme,
|
||
|
HideMarketbar: hideMarketbar,
|
||
|
HideChart: hideChart,
|
||
|
HideStatusbar: hideStatusbar,
|
||
|
OnlyTable: onlyTable,
|
||
|
RefreshRate: refreshRateP,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return ct.Run()
|
||
|
},
|
||
|
}
|
||
|
|
||
|
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")
|
||
4 years ago
|
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silence log ouput")
|
||
4 years ago
|
rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache")
|
||
5 years ago
|
rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh")
|
||
4 years ago
|
rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
|
||
5 years ago
|
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key")
|
||
|
rootCmd.Flags().StringVarP(&apiChoice, "api", "", cointop.CoinGecko, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")
|
||
4 years ago
|
rootCmd.Flags().StringVarP(&colorscheme, "colorscheme", "", "", "Colorscheme to use (default \"cointop\"). To install standard themes, do:\n\ngit clone git@github.com:cointop-sh/colors.git ~/.config/cointop/colors\n\nFor additional instructions, visit: https://github.com/cointop-sh/colors")
|
||
4 years ago
|
rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", "/tmp", "Cache directory")
|
||
5 years ago
|
|
||
4 years ago
|
versionCmd := &cobra.Command{
|
||
5 years ago
|
Use: "version",
|
||
|
Short: "Displays the current version",
|
||
|
Long: `The version command displays the current version`,
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
cointop.PrintVersion()
|
||
|
},
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
cleanCmd := &cobra.Command{
|
||
5 years ago
|
Use: "clean",
|
||
|
Short: "Clear the cache",
|
||
|
Long: `The clean command clears the cache`,
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
// NOTE: if clean command, clean but don't run cointop
|
||
4 years ago
|
return cointop.Clean(&cointop.CleanConfig{
|
||
4 years ago
|
Log: true,
|
||
|
CacheDir: cacheDir,
|
||
4 years ago
|
})
|
||
5 years ago
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
cleanCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, "Cache directory")
|
||
|
|
||
4 years ago
|
resetCmd := &cobra.Command{
|
||
5 years ago
|
Use: "reset",
|
||
|
Short: "Resets the config and clear the cache",
|
||
|
Long: `The reset command resets the config and clears the cache`,
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
// NOTE: if reset command, reset but don't run cointop
|
||
4 years ago
|
return cointop.Reset(&cointop.ResetConfig{
|
||
4 years ago
|
Log: true,
|
||
|
CacheDir: cacheDir,
|
||
4 years ago
|
})
|
||
5 years ago
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
resetCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, "Cache directory")
|
||
|
|
||
4 years ago
|
priceCmd := &cobra.Command{
|
||
5 years ago
|
Use: "price",
|
||
|
Short: "Displays the current price of a coin",
|
||
|
Long: `The price command display the current price of a coin`,
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
return cointop.PrintPrice(&cointop.PriceConfig{
|
||
|
Coin: coin,
|
||
|
Currency: currency,
|
||
|
APIChoice: apiChoice,
|
||
|
})
|
||
|
},
|
||
|
}
|
||
|
|
||
4 years ago
|
testCmd := &cobra.Command{
|
||
5 years ago
|
Use: "test",
|
||
|
Short: "Runs tests",
|
||
|
Long: `The test command runs tests for Homebrew`,
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
doTest()
|
||
|
},
|
||
|
}
|
||
|
|
||
|
priceCmd.Flags().StringVarP(&coin, "coin", "c", "bitcoin", "Full name of the coin (default \"bitcoin\")")
|
||
|
priceCmd.Flags().StringVarP(¤cy, "currency", "f", "USD", "The currency to convert to (default \"USD\")")
|
||
|
priceCmd.Flags().StringVarP(&apiChoice, "api", "a", cointop.CoinGecko, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")
|
||
|
|
||
4 years ago
|
var port uint = 22
|
||
|
var address string = "0.0.0.0"
|
||
|
var idleTimeout uint = 60
|
||
|
var executableBinary string = "cointop"
|
||
4 years ago
|
var hostKeyFile string = cssh.DefaultHostKeyFile
|
||
4 years ago
|
|
||
|
serverCmd := &cobra.Command{
|
||
|
Use: "server",
|
||
|
Short: "Run cintop SSH Server",
|
||
|
Long: `Run cointop SSH server`,
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
server := cssh.NewServer(&cssh.Config{
|
||
|
Address: address,
|
||
|
Port: port,
|
||
|
IdleTimeout: time.Duration(int(idleTimeout)) * time.Second,
|
||
|
ExecutableBinary: executableBinary,
|
||
4 years ago
|
HostKeyFile: hostKeyFile,
|
||
4 years ago
|
})
|
||
|
|
||
|
fmt.Printf("Running SSH server on port %v\n", port)
|
||
|
return server.ListenAndServe()
|
||
|
},
|
||
|
}
|
||
|
|
||
|
serverCmd.Flags().UintVarP(&port, "port", "p", port, "Port")
|
||
|
serverCmd.Flags().StringVarP(&address, "address", "a", address, "Address")
|
||
|
serverCmd.Flags().UintVarP(&idleTimeout, "idle-timeout", "t", idleTimeout, "Idle timeout in seconds")
|
||
4 years ago
|
serverCmd.Flags().StringVarP(&executableBinary, "binary", "b", executableBinary, "Executable binary path")
|
||
|
serverCmd.Flags().StringVarP(&hostKeyFile, "host-key-file", "k", hostKeyFile, "Host key file")
|
||
4 years ago
|
|
||
|
rootCmd.AddCommand(versionCmd, cleanCmd, resetCmd, priceCmd, testCmd, serverCmd)
|
||
5 years ago
|
|
||
|
if err := rootCmd.Execute(); err != nil {
|
||
5 years ago
|
panic(err)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func doTest() {
|
||
5 years ago
|
ct, err := cointop.NewCointop(&cointop.Config{
|
||
6 years ago
|
NoPrompts: true,
|
||
5 years ago
|
})
|
||
5 years ago
|
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
panic(err)
|
||
5 years ago
|
}
|
||
|
|
||
|
ct.Exit()
|
||
6 years ago
|
}
|