mirror of https://github.com/curusarn/resh
parent
968c792f69
commit
15a431cfc3
@ -0,0 +1,84 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"encoding/json" |
||||||
|
"flag" |
||||||
|
"fmt" |
||||||
|
"io/ioutil" |
||||||
|
"log" |
||||||
|
"net/http" |
||||||
|
|
||||||
|
"github.com/BurntSushi/toml" |
||||||
|
"github.com/curusarn/resh/pkg/cfg" |
||||||
|
"github.com/curusarn/resh/pkg/msg" |
||||||
|
|
||||||
|
"os/user" |
||||||
|
"path/filepath" |
||||||
|
"strconv" |
||||||
|
) |
||||||
|
|
||||||
|
// Version from git set during build
|
||||||
|
var Version string |
||||||
|
|
||||||
|
// Revision from git set during build
|
||||||
|
var Revision string |
||||||
|
|
||||||
|
func main() { |
||||||
|
usr, _ := user.Current() |
||||||
|
dir := usr.HomeDir |
||||||
|
configPath := filepath.Join(dir, "/.config/resh.toml") |
||||||
|
|
||||||
|
var config cfg.Config |
||||||
|
if _, err := toml.DecodeFile(configPath, &config); err != nil { |
||||||
|
log.Fatal("Error reading config:", err) |
||||||
|
} |
||||||
|
|
||||||
|
sessionID := flag.String("sessionID", "", "resh generated session id") |
||||||
|
count := flag.Uint("count", 10, "Number of cmdLines to return") |
||||||
|
flag.Parse() |
||||||
|
|
||||||
|
if *sessionID == "" { |
||||||
|
fmt.Println("Error: you need to specify sessionId") |
||||||
|
} |
||||||
|
|
||||||
|
m := msg.InspectMsg{SessionID: *sessionID, Count: *count} |
||||||
|
resp := SendInspectMsg(m, strconv.Itoa(config.Port)) |
||||||
|
for _, cmdLine := range resp.CmdLines { |
||||||
|
fmt.Println("`" + cmdLine + "'") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// SendInspectMsg to daemon
|
||||||
|
func SendInspectMsg(m msg.InspectMsg, port string) msg.MultiResponse { |
||||||
|
recJSON, err := json.Marshal(m) |
||||||
|
if err != nil { |
||||||
|
log.Fatal("send err 1", err) |
||||||
|
} |
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "http://localhost:"+port+"/inspect", |
||||||
|
bytes.NewBuffer(recJSON)) |
||||||
|
if err != nil { |
||||||
|
log.Fatal("send err 2", err) |
||||||
|
} |
||||||
|
req.Header.Set("Content-Type", "application/json") |
||||||
|
|
||||||
|
client := &http.Client{} |
||||||
|
resp, err := client.Do(req) |
||||||
|
if err != nil { |
||||||
|
log.Fatal("resh-daemon is not running :(") |
||||||
|
} |
||||||
|
|
||||||
|
defer resp.Body.Close() |
||||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||||
|
if err != nil { |
||||||
|
log.Fatal("read response error") |
||||||
|
} |
||||||
|
// log.Println(string(body))
|
||||||
|
response := msg.MultiResponse{} |
||||||
|
err = json.Unmarshal(body, &response) |
||||||
|
if err != nil { |
||||||
|
log.Fatal("unmarshal resp error: ", err) |
||||||
|
} |
||||||
|
return response |
||||||
|
} |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
package histlist |
||||||
|
|
||||||
|
// Histlist is a deduplicated list of cmdLines
|
||||||
|
type Histlist struct { |
||||||
|
// list of commands lines (deduplicated)
|
||||||
|
List []string |
||||||
|
// lookup: cmdLine -> last index
|
||||||
|
LastIndex map[string]int |
||||||
|
} |
||||||
|
|
||||||
|
// New Histlist
|
||||||
|
func New() Histlist { |
||||||
|
return Histlist{LastIndex: make(map[string]int)} |
||||||
|
} |
||||||
|
|
||||||
|
// Copy Histlist
|
||||||
|
func Copy(hl Histlist) Histlist { |
||||||
|
newHl := New() |
||||||
|
// copy list
|
||||||
|
newHl.List = make([]string, len(hl.List)) |
||||||
|
copy(newHl.List, hl.List) |
||||||
|
// copy map
|
||||||
|
for k, v := range hl.LastIndex { |
||||||
|
newHl.LastIndex[k] = v |
||||||
|
} |
||||||
|
return newHl |
||||||
|
} |
||||||
@ -0,0 +1,12 @@ |
|||||||
|
package msg |
||||||
|
|
||||||
|
// InspectMsg struct
|
||||||
|
type InspectMsg struct { |
||||||
|
SessionID string `json:"sessionId"` |
||||||
|
Count uint `json:"count"` |
||||||
|
} |
||||||
|
|
||||||
|
// MultiResponse struct
|
||||||
|
type MultiResponse struct { |
||||||
|
CmdLines []string `json:"cmdlines"` |
||||||
|
} |
||||||
Loading…
Reference in new issue