Skip to content

Commit

Permalink
Merge pull request #18 from nathan-fiscaletti/dev
Browse files Browse the repository at this point in the history
Backend Updates
  • Loading branch information
nathan-fiscaletti authored Jul 31, 2024
2 parents b718f96 + 407251a commit bc2456c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 35 deletions.
12 changes: 0 additions & 12 deletions internal/config/editors/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ var AllTrackingTypes = []TrackingType{
TrackingTypeProcess,
}

type EditorRunType string

func (e EditorRunType) String() string {
return string(e)
}

const (
EditorRunTypeStart EditorRunType = "start"
EditorRunTypeRun EditorRunType = "run"
)

func GetTrackingType(value string) (TrackingType, error) {
for _, t := range AllTrackingTypes {
if t.String() == value {
Expand All @@ -54,7 +43,6 @@ func GetTrackingType(value string) (TrackingType, error) {

type Editor struct {
Name EditorName `json:"name"`
RunType EditorRunType `json:"run_type"`
ExecPath string `json:"path"`
Args string `json:"args"`
ProcessCaptureDelay time.Duration `json:"process_capture_delay"`
Expand Down
3 changes: 1 addition & 2 deletions internal/config/editors/editor.notepad++.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ const (
func NotepadPlusPlusEditor() Editor {
return Editor{
Name: EditorNameNotepadPlusPlus,
RunType: EditorRunTypeStart,
ExecPath: "C:\\Program Files\\Notepad++\\notepad++.exe",
Args: "-multiInst -openFoldersAsWorkspace",
ProcessCaptureDelay: time.Second * 2,
ProcessCaptureDelay: time.Second * 5,
TrackingType: TrackingTypeProcess,
}
}
3 changes: 1 addition & 2 deletions internal/config/editors/editor.vscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ func VSCodeEditor() Editor {

return Editor{
Name: EditorNameVSCode,
RunType: EditorRunTypeRun,
ExecPath: vsCodePath,
Args: "-n",
ProcessCaptureDelay: time.Second * 2,
ProcessCaptureDelay: time.Second * 5,
TrackingType: TrackingTypeFileAccess,
}
}
7 changes: 7 additions & 0 deletions internal/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (l *logger) File() *os.File {
return nil
}

func (l *logger) ChildLogger(prefix string) *logger {
return &logger{
Logger: log.New(l.Writer(), fmt.Sprintf("%s%s: ", logPrefix, prefix), log.LstdFlags),
cfg: l.cfg,
}
}

func New(cfg *LoggerConfig) (*logger, error) {
storageManager := storage.GetStorage()

Expand Down
12 changes: 3 additions & 9 deletions internal/manager/cmd.create-session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/go-git/go-git/v5"
"github.com/nathan-fiscaletti/letstry/internal/config"
"github.com/nathan-fiscaletti/letstry/internal/config/editors"
"github.com/nathan-fiscaletti/letstry/internal/environment"
"github.com/nathan-fiscaletti/letstry/internal/logging"
"github.com/nathan-fiscaletti/letstry/internal/util/identifier"
Expand Down Expand Up @@ -98,12 +97,7 @@ func (s *manager) CreateSession(ctx context.Context, args CreateSessionArguments
cfgArgs := strings.Split(editor.Args, " ")
cmdArgs := append(cfgArgs, tempDir)
cmd := exec.Command(editor.ExecPath, cmdArgs...)
switch editor.RunType {
case editors.EditorRunTypeRun:
err = cmd.Run()
case editors.EditorRunTypeStart:
err = cmd.Start()
}
err = cmd.Start()
if err != nil {
return zeroValue, fmt.Errorf("failed to run editor: %v", err)
}
Expand Down Expand Up @@ -132,7 +126,7 @@ func (s *manager) CreateSession(ctx context.Context, args CreateSessionArguments

if appEnvironment.DebuggerAttached {
logger.Printf("skipping monitor process for session %s (debugger attached)\n", newSession.FormattedID())
logger.Printf("starting monitor session for session %s\n", newSession.FormattedID())
logger.Printf("starting monitor session for session %s with delay %v\n", newSession.FormattedID(), newSession.Editor.ProcessCaptureDelay)
err = s.MonitorSession(ctx, MonitorSessionArguments{
Delay: newSession.Editor.ProcessCaptureDelay,
TrackingType: newSession.Editor.TrackingType,
Expand All @@ -145,7 +139,7 @@ func (s *manager) CreateSession(ctx context.Context, args CreateSessionArguments
} else {
// Call this application again, but start it in the background as it's own process.
// This will allow the user to continue using the current terminal session.
logger.Printf("starting monitor process for session %s\n", newSession.FormattedID())
logger.Printf("starting monitor session for session %s with delay %v\n", newSession.FormattedID(), newSession.Editor.ProcessCaptureDelay)
cmd = exec.Command(os.Args[0], "monitor", fmt.Sprintf("%v", editor.ProcessCaptureDelay), newSession.Location, fmt.Sprintf("%v", newSession.PID), editor.TrackingType.String())
err = cmd.Start()
if err != nil {
Expand Down
31 changes: 21 additions & 10 deletions internal/manager/cmd.monitor-session.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,29 @@ type MonitorSessionArguments struct {

func (s *manager) MonitorSession(ctx context.Context, args MonitorSessionArguments) error {
// delay the start of the monitoring
logger, err := logging.LoggerFromContext(ctx)
if err != nil {
return err
}

logger.Printf("delaying monitoring for %v\n", args.Delay)
time.Sleep(args.Delay)

handler := func() error {
session, err := s.GetSessionForPath(ctx, args.Location)
if err != nil {
return err
}
session, err := s.GetSessionForPath(ctx, args.Location)
if err != nil {
return err
}
logger.Printf("monitoring session: %s\n", session.ID)

logger, err := logging.LoggerFromContext(ctx)
if err != nil {
return err
}
logger = logger.ChildLogger(fmt.Sprintf("sess-%s", session.ID))

logger.Printf("cleaning up session: %s (directory no longer being accessed)\n", session.ID)
handler := func() error {
switch session.Editor.TrackingType {
case editors.TrackingTypeFileAccess:
logger.Printf("cleaning up session: %s (directory no longer being accessed)\n", session.ID)
case editors.TrackingTypeProcess:
logger.Printf("cleaning up session: %s (process no longer running)\n", session.ID)
}

err = s.removeSession(ctx, session.ID)
if err != nil {
Expand All @@ -54,8 +63,10 @@ func (s *manager) MonitorSession(ctx context.Context, args MonitorSessionArgumen

switch args.TrackingType {
case editors.TrackingTypeProcess:
logger.Printf("using tracking type: %v\n", editors.TrackingTypeProcess)
return s.monitorProcess(args.PID, handler)
case editors.TrackingTypeFileAccess:
logger.Printf("using tracking type: %v\n", editors.TrackingTypeFileAccess)
_, err := os.Stat(args.Location)
if err != nil {
return err
Expand Down

0 comments on commit bc2456c

Please sign in to comment.