Skip to content

Commit

Permalink
get resh to compile and run
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Jan 19, 2023
1 parent e620a4a commit c897c09
Show file tree
Hide file tree
Showing 40 changed files with 447 additions and 1,134 deletions.
9 changes: 9 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ builds:
- amd64
- arm
- arm64
-
id: "install-utils"
main: ./cmd/install-utils
binary: bin/resh-install-utils
goarch:
- 386
- amd64
- arm
- arm64

# signs:
# - artifacts: checksum
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GOFLAGS=-ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.de

build: submodules bin/resh-session-init bin/resh-collect bin/resh-postcollect\
bin/resh-daemon bin/resh-control bin/resh-config bin/resh-cli\
bin/installutil
bin/resh-install-utils

install: build
scripts/install.sh
Expand Down
10 changes: 5 additions & 5 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/curusarn/resh/internal/logger"
"github.com/curusarn/resh/internal/msg"
"github.com/curusarn/resh/internal/output"
"github.com/curusarn/resh/internal/records"
"github.com/curusarn/resh/internal/recordint"
"github.com/curusarn/resh/internal/searchapp"
"go.uber.org/zap"

Expand Down Expand Up @@ -96,7 +96,7 @@ func runReshCli(out *output.Output, config cfg.Config) (string, int) {

st := state{
// lock sync.Mutex
cliRecords: resp.CliRecords,
cliRecords: resp.Records,
initialQuery: *query,
}

Expand All @@ -106,7 +106,7 @@ func runReshCli(out *output.Output, config cfg.Config) (string, int) {
sessionID: *sessionID,
host: *host,
pwd: *pwd,
gitOriginRemote: records.NormalizeGitRemote(*gitOriginRemote),
gitOriginRemote: *gitOriginRemote,
s: &st,
}
g.SetManager(layout)
Expand Down Expand Up @@ -159,7 +159,7 @@ func runReshCli(out *output.Output, config cfg.Config) (string, int) {

type state struct {
lock sync.Mutex
cliRecords []records.CliRecord
cliRecords []recordint.SearchApp
data []searchapp.Item
rawData []searchapp.RawItem
highlightedItem int
Expand Down Expand Up @@ -600,7 +600,7 @@ func SendCliMsg(out *output.Output, m msg.CliMsg, port string) msg.CliResponse {
out.Fatal("Failed decode response", err)
}
sugar.Debugw("Recieved records from daemon",
"recordCount", len(response.CliRecords),
"recordCount", len(response.Records),
)
return response
}
180 changes: 40 additions & 140 deletions cmd/collect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/curusarn/resh/internal/collect"
"github.com/curusarn/resh/internal/logger"
"github.com/curusarn/resh/internal/output"
"github.com/curusarn/resh/internal/records"
"github.com/curusarn/resh/internal/record"
"github.com/curusarn/resh/internal/recordint"
"go.uber.org/zap"

// "os/exec"
Expand All @@ -32,14 +33,6 @@ func main() {
}
out := output.New(logger, "resh-collect ERROR")

homeDir, err := os.UserHomeDir()
if err != nil {
out.Fatal("Could not get user home dir", err)
}

reshUUIDPath := filepath.Join(homeDir, "/.resh/resh-uuid")
machineIDPath := "/etc/machine-id"

// version
showVersion := flag.Bool("version", false, "Show version and exit")
showRevision := flag.Bool("revision", false, "Show git revision and exit")
Expand All @@ -49,55 +42,27 @@ func main() {

// core
cmdLine := flag.String("cmdLine", "", "command line")
exitCode := flag.Int("exitCode", -1, "exit code")
shell := flag.String("shell", "", "actual shell")
uname := flag.String("uname", "", "uname")
sessionID := flag.String("sessionId", "", "resh generated session id")
recordID := flag.String("recordId", "", "resh generated record id")

// posix variables
cols := flag.String("cols", "-1", "$COLUMNS")
lines := flag.String("lines", "-1", "$LINES")

home := flag.String("home", "", "$HOME")
lang := flag.String("lang", "", "$LANG")
lcAll := flag.String("lcAll", "", "$LC_ALL")
login := flag.String("login", "", "$LOGIN")
// path := flag.String("path", "", "$PATH")
pwd := flag.String("pwd", "", "$PWD - present working directory")
shellEnv := flag.String("shellEnv", "", "$SHELL")
term := flag.String("term", "", "$TERM")

// FIXME: get device ID
deviceID := flag.String("deviceID", "", "RESH device ID")
sessionID := flag.String("sessionID", "", "resh generated session ID")
recordID := flag.String("recordID", "", "resh generated record ID")
sessionPID := flag.Int("sessionPID", -1, "PID at the start of the terminal session")

shell := flag.String("shell", "", "current shell")

logname := flag.String("logname", "", "$LOGNAME")
hostname := flag.String("hostname", "", "$HOSTNAME")

// non-posix
pid := flag.Int("pid", -1, "$$")
sessionPid := flag.Int("sessionPid", -1, "$$ at session start")
shlvl := flag.Int("shlvl", -1, "$SHLVL")

host := flag.String("host", "", "$HOSTNAME")
hosttype := flag.String("hosttype", "", "$HOSTTYPE")
ostype := flag.String("ostype", "", "$OSTYPE")
machtype := flag.String("machtype", "", "$MACHTYPE")
gitCdup := flag.String("gitCdup", "", "git rev-parse --show-cdup")
gitRemote := flag.String("gitRemote", "", "git remote get-url origin")

gitCdupExitCode := flag.Int("gitCdupExitCode", -1, "... $?")
gitRemoteExitCode := flag.Int("gitRemoteExitCode", -1, "... $?")

// before after
timezoneBefore := flag.String("timezoneBefore", "", "")

osReleaseID := flag.String("osReleaseId", "", "/etc/os-release ID")
osReleaseVersionID := flag.String("osReleaseVersionId", "",
"/etc/os-release ID")
osReleaseIDLike := flag.String("osReleaseIdLike", "", "/etc/os-release ID")
osReleaseName := flag.String("osReleaseName", "", "/etc/os-release ID")
osReleasePrettyName := flag.String("osReleasePrettyName", "",
"/etc/os-release ID")

rtb := flag.String("realtimeBefore", "-1", "before $EPOCHREALTIME")
rtsess := flag.String("realtimeSession", "-1",
"on session start $EPOCHREALTIME")
rtsessboot := flag.String("realtimeSessSinceBoot", "-1",
"on session start $EPOCHREALTIME")
time_ := flag.String("time", "-1", "$EPOCHREALTIME")
flag.Parse()

if *showVersion == true {
Expand All @@ -109,117 +74,52 @@ func main() {
os.Exit(0)
}
if *requireVersion != "" && *requireVersion != version {
fmt.Println("Please restart/reload this terminal session " +
"(resh version: " + version +
"; resh version of this terminal session: " + *requireVersion +
")")
os.Exit(3)
out.FatalVersionMismatch(version, *requireVersion)
}
if *requireRevision != "" && *requireRevision != commit {
fmt.Println("Please restart/reload this terminal session " +
"(resh revision: " + commit +
"; resh revision of this terminal session: " + *requireRevision +
")")
os.Exit(3)
// this is only relevant for dev versions so we can reuse FatalVersionMismatch()
out.FatalVersionMismatch("revision "+commit, "revision "+*requireVersion)
}

realtimeBefore, err := strconv.ParseFloat(*rtb, 64)
if err != nil {
out.Fatal("Error while parsing flag --realtimeBefore", err)
}
realtimeSessionStart, err := strconv.ParseFloat(*rtsess, 64)
time, err := strconv.ParseFloat(*time_, 64)
if err != nil {
out.Fatal("Error while parsing flag --realtimeSession", err)
out.Fatal("Error while parsing flag --time", err)
}
realtimeSessSinceBoot, err := strconv.ParseFloat(*rtsessboot, 64)
if err != nil {
out.Fatal("Error while parsing flag --realtimeSessSinceBoot", err)
}
realtimeSinceSessionStart := realtimeBefore - realtimeSessionStart
realtimeSinceBoot := realtimeSessSinceBoot + realtimeSinceSessionStart

timezoneBeforeOffset := collect.GetTimezoneOffsetInSeconds(logger, *timezoneBefore)
realtimeBeforeLocal := realtimeBefore + timezoneBeforeOffset

realPwd, err := filepath.EvalSymlinks(*pwd)
if err != nil {
logger.Error("Error while handling pwd realpath", zap.Error(err))
realPwd = ""
}

gitDir, gitRealDir := collect.GetGitDirs(logger, *gitCdup, *gitCdupExitCode, *pwd)
if *gitRemoteExitCode != 0 {
*gitRemote = ""
}
rec := recordint.Collect{
SessionID: *sessionID,
Shlvl: *shlvl,
SessionPID: *sessionPID,

Shell: *shell,

// if *osReleaseID == "" {
// *osReleaseID = "linux"
// }
// if *osReleaseName == "" {
// *osReleaseName = "Linux"
// }
// if *osReleasePrettyName == "" {
// *osReleasePrettyName = "Linux"
// }

rec := records.Record{
// posix
Cols: *cols,
Lines: *lines,
// core
BaseRecord: records.BaseRecord{
CmdLine: *cmdLine,
ExitCode: *exitCode,
Shell: *shell,
Uname: *uname,
Rec: record.V1{
DeviceID: *deviceID,
SessionID: *sessionID,
RecordID: *recordID,

CmdLine: *cmdLine,

// posix
Home: *home,
Lang: *lang,
LcAll: *lcAll,
Login: *login,
// Path: *path,
Pwd: *pwd,
ShellEnv: *shellEnv,
Term: *term,

// non-posix
RealPwd: realPwd,
Pid: *pid,
SessionPID: *sessionPid,
Host: *host,
Hosttype: *hosttype,
Ostype: *ostype,
Machtype: *machtype,
Shlvl: *shlvl,

// before after
TimezoneBefore: *timezoneBefore,

RealtimeBefore: realtimeBefore,
RealtimeBeforeLocal: realtimeBeforeLocal,

RealtimeSinceSessionStart: realtimeSinceSessionStart,
RealtimeSinceBoot: realtimeSinceBoot,

GitDir: gitDir,
GitRealDir: gitRealDir,
GitOriginRemote: *gitRemote,
MachineID: collect.ReadFileContent(out.Logger, machineIDPath),
Home: *home,
Pwd: *pwd,
RealPwd: realPwd,

OsReleaseID: *osReleaseID,
OsReleaseVersionID: *osReleaseVersionID,
OsReleaseIDLike: *osReleaseIDLike,
OsReleaseName: *osReleaseName,
OsReleasePrettyName: *osReleasePrettyName,
Logname: *logname,
Hostname: *hostname,

GitOriginRemote: *gitRemote,

PartOne: true,
Time: time,

ReshUUID: collect.ReadFileContent(out.Logger, reshUUIDPath),
ReshVersion: version,
ReshRevision: commit,
PartOne: true,
PartsNotMerged: true,
},
}
collect.SendRecord(out, rec, strconv.Itoa(config.Port), "/record")
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (h *dumpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sugar.Errorw("Error when getting records", "error", err)
}

resp := msg.CliResponse{CliRecords: fullRecords.List}
resp := msg.CliResponse{Records: fullRecords.List}
jsn, err = json.Marshal(&resp)
if err != nil {
sugar.Errorw("Error when marshaling", "error", err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ var developement bool

func main() {
config, errCfg := cfg.New()
logger, _ := logger.New("daemon", config.LogLevel, developement)
logger, err := logger.New("daemon", config.LogLevel, developement)
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))
Expand All @@ -34,9 +37,6 @@ func main() {
"commit", commit,
)

// xdgCacheHome := d.getEnvOrPanic("__RESH_XDG_CACHE_HOME")
// xdgDataHome := d.getEnvOrPanic("__RESH_XDG_DATA_HOME")

// TODO: rethink PID file and logs location
homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions cmd/daemon/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"io/ioutil"
"net/http"

"github.com/curusarn/resh/internal/records"
"github.com/curusarn/resh/internal/recordint"
"go.uber.org/zap"
)

func NewRecordHandler(sugar *zap.SugaredLogger, subscribers []chan records.Record) recordHandler {
func NewRecordHandler(sugar *zap.SugaredLogger, subscribers []chan recordint.Collect) recordHandler {
return recordHandler{
sugar: sugar.With(zap.String("endpoint", "/record")),
subscribers: subscribers,
Expand All @@ -18,7 +18,7 @@ func NewRecordHandler(sugar *zap.SugaredLogger, subscribers []chan records.Recor

type recordHandler struct {
sugar *zap.SugaredLogger
subscribers []chan records.Record
subscribers []chan recordint.Collect
}

func (h *recordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -34,8 +34,8 @@ func (h *recordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

sugar.Debugw("Unmarshaling record ...")
record := records.Record{}
err = json.Unmarshal(jsn, &record)
rec := recordint.Collect{}
err = json.Unmarshal(jsn, &rec)
if err != nil {
sugar.Errorw("Error during unmarshaling",
"error", err,
Expand All @@ -44,16 +44,16 @@ func (h *recordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
part := "2"
if record.PartOne {
if rec.Rec.PartOne {
part = "1"
}
sugar := sugar.With(
"cmdLine", record.CmdLine,
"cmdLine", rec.Rec.CmdLine,
"part", part,
)
sugar.Debugw("Got record, sending to subscribers ...")
for _, sub := range h.subscribers {
sub <- record
sub <- rec
}
sugar.Debugw("Record sent to subscribers")
}()
Expand Down
Loading

0 comments on commit c897c09

Please sign in to comment.