Skip to content

Commit

Permalink
add reshctl status and reshctl update
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Dec 16, 2019
1 parent 75f4695 commit 8de4f4f
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 10 deletions.
32 changes: 31 additions & 1 deletion cmd/control/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,45 @@ package cmd

import (
"fmt"
"log"
"os/user"
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/curusarn/resh/cmd/control/status"
"github.com/curusarn/resh/pkg/cfg"
"github.com/spf13/cobra"
)

// globals
var exitCode status.Code
var version string
var commit string
var debug = false
var config cfg.Config

var rootCmd = &cobra.Command{
Use: "reshctl",
Short: "Reshctl (RESH control) - enable/disable RESH features and more.",
}

// Execute reshctl
func Execute() status.Code {
func Execute(ver, com string) status.Code {
version = ver
commit = com

usr, _ := user.Current()
dir := usr.HomeDir
configPath := filepath.Join(dir, ".config/resh.toml")
if _, err := toml.DecodeFile(configPath, &config); err != nil {
log.Println("Error reading config", err)
return status.Fail
}
if config.Debug {
debug = true
// log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}

rootCmd.AddCommand(disableCmd)
disableCmd.AddCommand(disableArrowKeyBindingsCmd)
disableCmd.AddCommand(disableArrowKeyBindingsGlobalCmd)
Expand All @@ -32,6 +57,11 @@ func Execute() status.Code {
debugCmd.AddCommand(debugReloadCmd)
debugCmd.AddCommand(debugInspectCmd)
debugCmd.AddCommand(debugOutputCmd)

rootCmd.AddCommand(statusCmd)

rootCmd.AddCommand(updateCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
return status.Fail
Expand Down
80 changes: 80 additions & 0 deletions cmd/control/cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"

"github.com/curusarn/resh/cmd/control/status"
"github.com/curusarn/resh/pkg/msg"
"github.com/spf13/cobra"
)

var statusCmd = &cobra.Command{
Use: "status",
Short: "show RESH status (aka systemctl status)",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("resh " + version)
fmt.Println()
fmt.Println("Resh versions ...")
fmt.Println(" * installed: " + version + " (" + commit + ")")
resp, err := getDaemonStatus(config.Port)
if err != nil {
fmt.Println(" * daemon: NOT RUNNING!")
} else {
fmt.Println(" * daemon: " + resp.Version + " (" + resp.Commit + ")")
}
versionEnv, found := os.LookupEnv("__RESH_VERSION")
if found == false {
versionEnv = "UNKNOWN!"
}
commitEnv, found := os.LookupEnv("__RESH_REVISION")
if found == false {
commitEnv = "unknown"
}
fmt.Println(" * this session: " + versionEnv + " (" + commitEnv + ")")
if version != resp.Version || version != versionEnv {
fmt.Println(" * THERE IS A MISMATCH BETWEEN VERSIONS!")
fmt.Println(" * Please REPORT this here: https://github.com/curusarn/resh/issues")
fmt.Println(" * Please RESTART this terminal window")
}

fmt.Println()
fmt.Println("Arrow key bindnigs ...")
if config.BindArrowKeysBash {
fmt.Println(" * bash future sessions: ENABLED (not recommended)")
} else {
fmt.Println(" * bash future sessions: DISABLED (recommended)")
}
if config.BindArrowKeysZsh {
fmt.Println(" * zsh future sessions: ENABLED (recommended)")
} else {
fmt.Println(" * zsh future sessions: DISABLED (not recommended)")
}
exitCode = status.ReshStatus
},
}

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 {
log.Println("Daemon is not running!", err)
return mess, err
}
defer resp.Body.Close()
jsn, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error while reading 'daemon /status' response:", err)
}
err = json.Unmarshal(jsn, &mess)
if err != nil {
log.Fatal("Error while decoding 'daemon /status' response:", err)
}
return mess, nil
}
24 changes: 24 additions & 0 deletions cmd/control/cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"os"
"os/exec"

"github.com/curusarn/resh/cmd/control/status"
"github.com/spf13/cobra"
)

var updateCmd = &cobra.Command{
Use: "update",
Short: "checks for updates and updates RESH",
Run: func(cmd *cobra.Command, args []string) {
url := "https://raw.githubusercontent.com/curusarn/resh/master/scripts/rawinstall.sh"
execCmd := exec.Command("bash", "-c", "curl -fsSL "+url+" | bash")
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr
err := execCmd.Run()
if err == nil {
exitCode = status.Success
}
},
}
2 changes: 1 addition & 1 deletion cmd/control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ var version string
var commit string

func main() {
os.Exit(int(cmd.Execute()))
os.Exit(int(cmd.Execute(version, commit)))
}
8 changes: 8 additions & 0 deletions cmd/control/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ const (
Success Code = 0
// Fail exit code
Fail = 1
// EnableResh exit code - tells reshctl() wrapper to enable resh
// EnableResh = 100

// EnableArrowKeyBindings exit code - tells reshctl() wrapper to enable arrow key bindings
EnableArrowKeyBindings = 101
// DisableResh exit code - tells reshctl() wrapper to enable resh
// DisableResh = 110

// DisableArrowKeyBindings exit code - tells reshctl() wrapper to disable arrow key bindings
DisableArrowKeyBindings = 111
// ReloadRcFiles exit code - tells reshctl() wrapper to reload shellrc resh file
ReloadRcFiles = 200
// InspectSessionHistory exit code - tells reshctl() wrapper to take current sessionID and send /inspect request to daemon
InspectSessionHistory = 201
// ReshStatus exit code - tells reshctl() wrapper to show RESH status (aka systemctl status)
ReshStatus = 202
)
19 changes: 16 additions & 3 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (

//"flag"
"encoding/json"
"io/ioutil"
"log"
"net/http"
Expand All @@ -15,6 +16,7 @@ import (

"github.com/BurntSushi/toml"
"github.com/curusarn/resh/pkg/cfg"
"github.com/curusarn/resh/pkg/msg"
)

// version from git set during build
Expand Down Expand Up @@ -89,9 +91,20 @@ func main() {
}

func statusHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK; version: " + version +
"; commit: " + commit + "\n"))
log.Println("Status OK")
log.Println("/status START")
resp := msg.StatusResponse{
Status: true,
Version: version,
Commit: commit,
}
jsn, err := json.Marshal(&resp)
if err != nil {
log.Println("Encoding error:", err)
log.Println("Response:", resp)
return
}
w.Write(jsn)
log.Println("/status END")
}

func killDaemon(pidfile string) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ type InspectMsg struct {
type MultiResponse struct {
CmdLines []string `json:"cmdlines"`
}

// StatusResponse struct
type StatusResponse struct {
Status bool `json:"status"`
Version string `json:"version"`
Commit string `json:"commit"`
}
6 changes: 6 additions & 0 deletions scripts/rawinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ version="${tag:1}"
# TODO: check if version is numeric

echo " * Latest version: $version (git tag: $tag)"

if [ "${__RESH_VERSION-}" == "$version" ]; then
echo " * Resh is up to date - nothing to do - exiting."
exit 0
fi

echo

if [ "$(uname)" = "Darwin" ]; then
Expand Down
18 changes: 15 additions & 3 deletions scripts/reshctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ __resh_unbind_arrows() {
else
eval "$__RESH_bindfunc_revert_arrow_up_bind"
[ -z "${__RESH_bindfunc_revert_arrow_up_bind_vim+x}" ] || eval "$__RESH_bindfunc_revert_arrow_up_bind_vim"
echo "RESH arrow up binding successfully disabled"
echo "RESH arrow up binding successfully disabled"
__RESH_arrow_keys_bind_enabled=0
fi

Expand All @@ -47,7 +47,7 @@ __resh_unbind_arrows() {
else
eval "$__RESH_bindfunc_revert_arrow_down_bind"
[ -z "${__RESH_bindfunc_revert_arrow_down_bind_vim+x}" ] || eval "$__RESH_bindfunc_revert_arrow_down_bind_vim"
echo "RESH arrow down binding successfully disabled"
echo "RESH arrow down binding successfully disabled"
__RESH_arrow_keys_bind_enabled=0
fi
return 0
Expand Down Expand Up @@ -119,8 +119,20 @@ reshctl() {
resh-inspect --sessionID "$__RESH_SESSION_ID" --count "${3-10}"
return 0
;;
202)
# show status
if [ "${__RESH_arrow_keys_bind_enabled-0}" != 0 ]; then
echo " * this session: ENABLED"
else
echo " * this session: DISABLED"
fi
return 0
;;
*)
echo "reshctl() FATAL ERROR: unknown status" >&2
echo "reshctl() FATAL ERROR: unknown status ($_status)" >&2
echo "Possibly caused by version mismatch between installed resh and resh in this session." >&2
echo "Please REPORT this issue here: https://github.com/curusarn/resh/issues" >&2
echo "Please RESTART your terminal window." >&2
return "$_status"
;;
esac
Expand Down
6 changes: 4 additions & 2 deletions scripts/shellrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ elif [ $__RESH_MACOS -eq 1 ]; then
__RESH_RT_SESS_SINCE_BOOT=$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g')
fi

__RESH_VERSION=$(resh-collect -version)
__RESH_REVISION=$(resh-collect -revision)
# shellcheck disable=2155
export __RESH_VERSION=$(resh-collect -version)
# shellcheck disable=2155
export __RESH_REVISION=$(resh-collect -revision)

__resh_run_daemon

Expand Down

0 comments on commit 8de4f4f

Please sign in to comment.