From 10c3f8d7f750ed884d32450ee7c549e68a965421 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Fri, 21 Aug 2020 16:19:08 -0700 Subject: [PATCH 1/2] wip --- cointop/ssh/server.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cointop/ssh/server.go b/cointop/ssh/server.go index 431192e..be189b7 100644 --- a/cointop/ssh/server.go +++ b/cointop/ssh/server.go @@ -66,6 +66,7 @@ func (s *Server) ListenAndServe() error { Addr: fmt.Sprintf("%s:%v", s.address, s.port), IdleTimeout: s.idleTimeout, Handler: func(sshSession ssh.Session) { + userCmd := sshSession.Command() ptyReq, winCh, isPty := sshSession.Pty() if !isPty { io.WriteString(sshSession, "Error: Non-interactive terminals are not supported") @@ -84,7 +85,24 @@ func (s *Server) ListenAndServe() error { cmdCtx, cancelCmd := context.WithCancel(sshSession.Context()) defer cancelCmd() - cmd := exec.CommandContext(cmdCtx, s.executableBinary, "--reset", "--silent", "--cache-dir", tempDir, "--config", configPath) + flags := []string{ + "--reset", + "--silent", + "--cache-dir", + tempDir, + "--config", + configPath, + } + + for _, arg := range userCmd { + if arg == "cointop" { + continue + } + + flags = append(flags, arg) + } + + cmd := exec.CommandContext(cmdCtx, s.executableBinary, flags...) cmd.Env = append(sshSession.Environ(), fmt.Sprintf("TERM=%s", ptyReq.Term)) f, err := pty.Start(cmd) From 903227fe785aa4ddf3dd2901dc7dfdc6645262da Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Fri, 21 Aug 2020 22:27:40 -0700 Subject: [PATCH 2/2] Add colors-dir flag --- Makefile | 3 +++ README.md | 6 ++++++ cointop/cmd/dominance.go | 3 ++- cointop/cmd/price.go | 4 +++- cointop/cmd/root.go | 23 +++++++++++++++++++---- cointop/cointop.go | 6 ++++++ cointop/common/pathutil/pathutil.go | 2 +- cointop/config.go | 7 ++++++- cointop/ssh/server.go | 9 ++++++--- 9 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 7c449e7..d9a10ef 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,9 @@ build-multiple: clean env GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop64 && upx bin/cointop64 && \ env GOARCH=386 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop32 && upx bin/cointop32 +install: build + sudo mv bin/cointop /usr/local/bin + clean-mac: go clean && \ rm -rf bin/mac diff --git a/README.md b/README.md index 3ac6e3d..4ca58f1 100644 --- a/README.md +++ b/README.md @@ -615,6 +615,12 @@ SSH demo: ssh cointop.sh ``` +Passing arguments to SSH server: + +```bash +ssh cointop.sh -t cointop --colorscheme synthwave +``` + Using docker to run SSH server: ```bash diff --git a/cointop/cmd/dominance.go b/cointop/cmd/dominance.go index bc5ef44..108c068 100644 --- a/cointop/cmd/dominance.go +++ b/cointop/cmd/dominance.go @@ -7,7 +7,8 @@ import ( // DominanceCmd ... func DominanceCmd() *cobra.Command { - var apiChoice, currency string + var apiChoice string + var currency string dominanceCmd := &cobra.Command{ Use: "dominance", diff --git a/cointop/cmd/price.go b/cointop/cmd/price.go index 3751cb1..4fa02f1 100644 --- a/cointop/cmd/price.go +++ b/cointop/cmd/price.go @@ -7,7 +7,9 @@ import ( // PriceCmd ... func PriceCmd() *cobra.Command { - var apiChoice, coin, currency string + var apiChoice string + var coin string + var currency string priceCmd := &cobra.Command{ Use: "price", diff --git a/cointop/cmd/root.go b/cointop/cmd/root.go index 35bddd9..609b86a 100644 --- a/cointop/cmd/root.go +++ b/cointop/cmd/root.go @@ -9,11 +9,24 @@ import ( // RootCmd ... func RootCmd() *cobra.Command { - var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent, noCache 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 config, cmcAPIKey, apiChoice, colorscheme string - perPage := cointop.DefaultPerPage - cacheDir := cointop.DefaultCacheDir + var config string + var cmcAPIKey string + var apiChoice string + var colorscheme string + var perPage = cointop.DefaultPerPage + var cacheDir = cointop.DefaultCacheDir + var colorsDir string rootCmd := &cobra.Command{ Use: "cointop", @@ -66,6 +79,7 @@ See git.io/cointop for more info.`, ct, err := cointop.NewCointop(&cointop.Config{ CacheDir: cacheDir, + ColorsDir: colorsDir, NoCache: noCache, ConfigFilepath: config, CoinMarketCapAPIKey: cmcAPIKey, @@ -103,6 +117,7 @@ See git.io/cointop for more info.`, 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(&cacheDir, "cache-dir", "", cacheDir, "Cache directory") + rootCmd.Flags().StringVarP(&colorsDir, "colors-dir", "", colorsDir, "Colorschemes directory") return rootCmd } diff --git a/cointop/cointop.go b/cointop/cointop.go index 67e12cb..411df7f 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -81,6 +81,7 @@ type Cointop struct { ActionsMap map[string]bool apiKeys *APIKeys cache *cache.Cache + colorsDir string config config // toml config configFilepath string api api.Interface @@ -124,6 +125,7 @@ type Portfolio struct { type Config struct { APIChoice string CacheDir string + ColorsDir string Colorscheme string ConfigFilepath string CoinMarketCapAPIKey string @@ -154,6 +156,9 @@ var DefaultConfigFilepath = pathutil.NormalizePath(":PREFERRED_CONFIG_HOME:/coin // DefaultCacheDir ... var DefaultCacheDir = filecache.DefaultCacheDir +// DefaultColorsDir ... +var DefaultColorsDir = fmt.Sprintf("%s/colors", DefaultConfigFilepath) + // NewCointop initializes cointop func NewCointop(config *Config) (*Cointop, error) { var debug bool @@ -189,6 +194,7 @@ func NewCointop(config *Config) (*Cointop, error) { maxTableWidth: 175, ActionsMap: ActionsMap(), cache: cache.New(1*time.Minute, 2*time.Minute), + colorsDir: config.ColorsDir, configFilepath: configFilepath, chartRanges: ChartRanges(), debug: debug, diff --git a/cointop/common/pathutil/pathutil.go b/cointop/common/pathutil/pathutil.go index 32e9e50..86b0832 100644 --- a/cointop/common/pathutil/pathutil.go +++ b/cointop/common/pathutil/pathutil.go @@ -46,5 +46,5 @@ func NormalizePath(path string) string { path = strings.Replace(path, ":PREFERRED_CONFIG_HOME:", userConfigHome, -1) path = strings.Replace(path, "/", string(filepath.Separator), -1) - return path + return filepath.Clean(path) } diff --git a/cointop/config.go b/cointop/config.go index f0446e5..8456f15 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -335,7 +335,12 @@ func (ct *Cointop) getColorschemeColors() (map[string]interface{}, error) { return nil, err } } else { - path := fmt.Sprintf("%s/colors/%s.toml", ct.ConfigDirPath(), ct.colorschemeName) + colorsDir := fmt.Sprintf("%s/colors", ct.ConfigDirPath()) + if ct.colorsDir != "" { + colorsDir = pathutil.NormalizePath(ct.colorsDir) + } + + path := fmt.Sprintf("%s/%s.toml", colorsDir, ct.colorschemeName) if _, err := os.Stat(path); os.IsNotExist(err) { // NOTE: case for when cointop is set as the theme but the colorscheme file doesn't exist if ct.colorschemeName == "cointop" { diff --git a/cointop/ssh/server.go b/cointop/ssh/server.go index be189b7..c53b611 100644 --- a/cointop/ssh/server.go +++ b/cointop/ssh/server.go @@ -66,7 +66,7 @@ func (s *Server) ListenAndServe() error { Addr: fmt.Sprintf("%s:%v", s.address, s.port), IdleTimeout: s.idleTimeout, Handler: func(sshSession ssh.Session) { - userCmd := sshSession.Command() + cmdUserArgs := sshSession.Command() ptyReq, winCh, isPty := sshSession.Pty() if !isPty { io.WriteString(sshSession, "Error: Non-interactive terminals are not supported") @@ -81,6 +81,7 @@ func (s *Server) ListenAndServe() error { } configPath := fmt.Sprintf("%s/config", tempDir) + colorsDir := pathutil.NormalizePath("~/.config/cointop/colors") cmdCtx, cancelCmd := context.WithCancel(sshSession.Context()) defer cancelCmd() @@ -92,10 +93,12 @@ func (s *Server) ListenAndServe() error { tempDir, "--config", configPath, + "--colors-dir", + colorsDir, } - for _, arg := range userCmd { - if arg == "cointop" { + for i, arg := range cmdUserArgs { + if i == 0 { continue }