Rich Enhanced Shell History - Contextual shell history for zsh and bash
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
resh/pkg/collect/collect.go

78 lines
1.8 KiB

package collect
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/curusarn/resh/pkg/records"
)
// SendRecord to daemon
func SendRecord(r records.Record, port string) {
recJSON, err := json.Marshal(r)
if err != nil {
log.Fatal("send err 1", err)
}
req, err := http.NewRequest("POST", "http://localhost:"+port+"/record",
bytes.NewBuffer(recJSON))
if err != nil {
log.Fatal("send err 2", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.Fatal("resh-daemon is not running :(")
}
}
// ReadFileContent and return it as a string
func ReadFileContent(path string) string {
dat, err := ioutil.ReadFile(path)
if err != nil {
return ""
//log.Fatal("failed to open " + path)
}
return strings.TrimSuffix(string(dat), "\n")
}
// GetGitDirs based on result of git "cdup" command
func GetGitDirs(cdup string, exitCode int, pwd string) (string, string) {
if exitCode != 0 {
return "", ""
}
abspath := filepath.Clean(filepath.Join(pwd, cdup))
realpath, err := filepath.EvalSymlinks(abspath)
if err != nil {
log.Println("err while handling git dir paths:", err)
return "", ""
}
return abspath, realpath
}
// GetTimezoneOffsetInSeconds based on zone returned by date command
func GetTimezoneOffsetInSeconds(zone string) float64 {
// date +%z -> "+0200"
hoursStr := zone[:3]
minsStr := zone[3:]
hours, err := strconv.Atoi(hoursStr)
if err != nil {
log.Println("err while parsing hours in timezone offset:", err)
return -1
}
mins, err := strconv.Atoi(minsStr)
if err != nil {
log.Println("err while parsing mins in timezone offset:", err)
return -1
}
secs := ((hours * 60) + mins) * 60
return float64(secs)
}