diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 1de3544..7d9a303 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -38,7 +38,10 @@ const exitCodeExecute = 111 func main() { config, errCfg := cfg.New() - logger, _ := logger.New("search-app", config.LogLevel, development) + logger, err := logger.New("search-app", config.LogLevel, development) + if err != nil { + fmt.Printf("Error while creating logger: %v", err) + } defer logger.Sync() // flushes buffer, if any if errCfg != nil { logger.Error("Error while getting configuration", zap.Error(errCfg)) diff --git a/cmd/collect/main.go b/cmd/collect/main.go index 6c4aae1..82775a7 100644 --- a/cmd/collect/main.go +++ b/cmd/collect/main.go @@ -26,7 +26,10 @@ var development string func main() { config, errCfg := cfg.New() - logger, _ := logger.New("collect", config.LogLevel, development) + logger, err := logger.New("collect", config.LogLevel, development) + if err != nil { + fmt.Printf("Error while creating logger: %v", err) + } defer logger.Sync() // flushes buffer, if any if errCfg != nil { logger.Error("Error while getting configuration", zap.Error(errCfg)) diff --git a/cmd/control/cmd/root.go b/cmd/control/cmd/root.go index 3f4cfbc..3c8cd45 100644 --- a/cmd/control/cmd/root.go +++ b/cmd/control/cmd/root.go @@ -1,6 +1,8 @@ package cmd import ( + "fmt" + "github.com/curusarn/resh/internal/cfg" "github.com/curusarn/resh/internal/logger" "github.com/curusarn/resh/internal/output" @@ -11,7 +13,6 @@ var version string var commit string // globals -var config cfg.Config var out *output.Output var rootCmd = &cobra.Command{ @@ -25,14 +26,22 @@ func Execute(ver, com, development string) { commit = com config, errCfg := cfg.New() - logger, _ := logger.New("reshctl", config.LogLevel, development) + logger, err := logger.New("reshctl", config.LogLevel, development) + if err != nil { + fmt.Printf("Error while creating logger: %v", err) + } defer logger.Sync() // flushes buffer, if any out = output.New(logger, "ERROR") if errCfg != nil { out.Error("Error while getting configuration", errCfg) } - rootCmd.AddCommand(versionCmd) + var versionCmd = cobra.Command{ + Use: "version", + Short: "show RESH version", + Run: versionCmdFunc(config), + } + rootCmd.AddCommand(&versionCmd) updateCmd.Flags().BoolVar(&betaFlag, "beta", false, "Update to latest version even if it's beta.") rootCmd.AddCommand(updateCmd) diff --git a/cmd/control/cmd/version.go b/cmd/control/cmd/version.go index bf6fafd..c6c7478 100644 --- a/cmd/control/cmd/version.go +++ b/cmd/control/cmd/version.go @@ -1,28 +1,23 @@ package cmd import ( - "encoding/json" "fmt" - "io" - "net/http" "os" - "strconv" - "github.com/curusarn/resh/internal/msg" + "github.com/curusarn/resh/internal/cfg" + "github.com/curusarn/resh/internal/status" "github.com/spf13/cobra" ) -var versionCmd = &cobra.Command{ - Use: "version", - Short: "show RESH version", - Run: func(cmd *cobra.Command, args []string) { +func versionCmdFunc(config cfg.Config) func(*cobra.Command, []string) { + return func(cmd *cobra.Command, args []string) { printVersion("Installed", version, commit) versionEnv := getEnvVarWithDefault("__RESH_VERSION", "") commitEnv := getEnvVarWithDefault("__RESH_REVISION", "") printVersion("This terminal session", versionEnv, commitEnv) - resp, err := getDaemonStatus(config.Port) + resp, err := status.GetDaemonStatus(config.Port) if err != nil { out.ErrorDaemonNotRunning(err) return @@ -37,8 +32,7 @@ var versionCmd = &cobra.Command{ out.ErrorTerminalVersionMismatch(version, versionEnv) return } - - }, + } } func printVersion(title, version, commit string) { @@ -52,22 +46,3 @@ func getEnvVarWithDefault(varName, defaultValue string) string { } return val } - -func getDaemonStatus(port int) (msg.StatusResponse, error) { - mess := msg.StatusResponse{} - url := "http://localhost:" + strconv.Itoa(port) + "/status" - resp, err := http.Get(url) - if err != nil { - return mess, err - } - defer resp.Body.Close() - jsn, err := io.ReadAll(resp.Body) - if err != nil { - out.Fatal("Error while reading 'daemon /status' response", err) - } - err = json.Unmarshal(jsn, &mess) - if err != nil { - out.Fatal("Error while decoding 'daemon /status' response", err) - } - return mess, nil -} diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index 5433419..cbdc0df 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -12,8 +12,8 @@ import ( "github.com/curusarn/resh/internal/cfg" "github.com/curusarn/resh/internal/datadir" "github.com/curusarn/resh/internal/device" - "github.com/curusarn/resh/internal/httpclient" "github.com/curusarn/resh/internal/logger" + "github.com/curusarn/resh/internal/status" "go.uber.org/zap" ) @@ -62,7 +62,7 @@ func main() { sugar = sugar.With(zap.Int("daemonPID", os.Getpid())) - res, err := d.isDaemonRunning(config.Port) + res, err := status.IsDaemonRunning(config.Port) if err != nil { sugar.Errorw("Error while checking daemon status - it's probably not running", "error", err) @@ -115,17 +115,6 @@ type daemon struct { sugar *zap.SugaredLogger } -func (d *daemon) isDaemonRunning(port int) (bool, error) { - url := "http://localhost:" + strconv.Itoa(port) + "/status" - client := httpclient.New() - resp, err := client.Get(url) - if err != nil { - return false, err - } - defer resp.Body.Close() - return true, nil -} - func (d *daemon) killDaemon(pidFile string) error { dat, err := ioutil.ReadFile(pidFile) if err != nil { diff --git a/cmd/postcollect/main.go b/cmd/postcollect/main.go index a87f4af..041e72f 100644 --- a/cmd/postcollect/main.go +++ b/cmd/postcollect/main.go @@ -25,7 +25,10 @@ var development string func main() { config, errCfg := cfg.New() - logger, _ := logger.New("postcollect", config.LogLevel, development) + logger, err := logger.New("postcollect", config.LogLevel, development) + if err != nil { + fmt.Printf("Error while creating logger: %v", err) + } defer logger.Sync() // flushes buffer, if any if errCfg != nil { logger.Error("Error while getting configuration", zap.Error(errCfg)) diff --git a/cmd/session-init/main.go b/cmd/session-init/main.go index 6580e4a..9a6449b 100644 --- a/cmd/session-init/main.go +++ b/cmd/session-init/main.go @@ -22,7 +22,10 @@ var development string func main() { config, errCfg := cfg.New() - logger, _ := logger.New("collect", config.LogLevel, development) + logger, err := logger.New("session-init", config.LogLevel, development) + if err != nil { + fmt.Printf("Error while creating logger: %v", err) + } defer logger.Sync() // flushes buffer, if any if errCfg != nil { logger.Error("Error while getting configuration", zap.Error(errCfg)) diff --git a/internal/status/status.go b/internal/status/status.go new file mode 100644 index 0000000..52324d4 --- /dev/null +++ b/internal/status/status.go @@ -0,0 +1,49 @@ +package status + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strconv" + + "github.com/curusarn/resh/internal/httpclient" + "github.com/curusarn/resh/internal/msg" +) + +func get(port int) (*http.Response, error) { + url := "http://localhost:" + strconv.Itoa(port) + "/status" + client := httpclient.New() + resp, err := client.Get(url) + if err != nil { + return nil, fmt.Errorf("error while GET'ing daemon /status: %w", err) + } + return resp, nil +} + +func IsDaemonRunning(port int) (bool, error) { + resp, err := get(port) + if err != nil { + return false, err + } + defer resp.Body.Close() + return true, nil +} + +func GetDaemonStatus(port int) (*msg.StatusResponse, error) { + resp, err := get(port) + if err != nil { + return nil, err + } + defer resp.Body.Close() + jsn, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("error while reading 'daemon /status' response: %w", err) + } + var msgResp msg.StatusResponse + err = json.Unmarshal(jsn, &msgResp) + if err != nil { + return nil, fmt.Errorf("error while decoding 'daemon /status' response: %w", err) + } + return &msgResp, nil +} diff --git a/scripts/install.sh b/scripts/install.sh index 62efed4..e501078 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -2,6 +2,10 @@ set -euo pipefail +# TODO: There is a lot of hardcoded stuff here (paths mostly) +# TODO: Split this into installation and setup because we want to suport package manager installation eventually +# TODO: "installation" should stay here and be simple, "setup" should be moved behind "reshctl setup" + echo echo "Checking your system ..." @@ -14,6 +18,9 @@ if [ "$login_shell" != bash ] && [ "$login_shell" != zsh ]; then fi echo " * Login shell: $login_shell - OK" +# TODO: Explicitly ask users if they want to enable RESH in shells +# Only offer shells with supported versions +# E.g. Enable RESH in: Zsh (your login shell), Bash, Both shells # check like we are not running bash bash_version=$(bash -c 'echo ${BASH_VERSION}') @@ -108,6 +115,23 @@ cp -f scripts/rawinstall.sh ~/.resh/ # Copy all executables. We don't really need to omit install-utils from the bin directory echo "Copying more files ..." cp -f bin/resh-* ~/.resh/bin/ +# rename reshctl +mv ~/.resh/bin/resh-control ~/.resh/bin/reshctl + +# Shutting down resh daemon ... +echo "Shutting down resh daemon ..." +pid_file="${XDG_DATA_HOME-~/.local/share}/resh/daemon.pid" +if [ ! -f "$pid_file" ]; then + # old pid file location + pid_file=~/.resh/resh.pid +fi + +if [ -f "$pid_file" ]; then + kill -SIGTERM "$pid_file" + rm "$pid_file" +else + pkill -SIGTERM "resh-daemon" +fi echo "Creating/updating config file ..." ./bin/resh-install-utils migrate-config @@ -141,13 +165,7 @@ fi # shellcheck source=util.sh . ~/.resh/util.sh -# Restarting resh daemon ... -if [ -f ~/.resh/resh.pid ]; then - kill -SIGTERM "$(cat ~/.resh/resh.pid)" || true - rm ~/.resh/resh.pid -else - pkill -SIGTERM "resh-daemon" || true -fi +echo "Launching resh daemon ..." __resh_run_daemon