From e56efd1e5d54bad0875b8865ef3b052563b2b4d6 Mon Sep 17 00:00:00 2001 From: Timo Kats <31708538+TimoKats@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:48:06 +0200 Subject: [PATCH] add status command (#10) add `pim status` command that shows if pim is currently running its pid. --- commands/commands.go | 12 ++++++++++++ commands/lib/io.go | 7 ++++++- pim.go | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/commands/commands.go b/commands/commands.go index f074795..6a9a888 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -68,6 +68,18 @@ func LogCommand(command []string, database *lib.Database) error { return lib.ViewLog(database, command[2]) } +func StatusCommand() error { + pid, lockErr := lib.ReadLockFile() + processCount := lib.CountPimProcesses() + if processCount > 0 { + lib.Info.Printf("Pim is currently running at: %d", pid) + return lockErr + } else { + lib.Info.Println("No pim process running.") + return nil + } +} + func StartCommand(process lib.Process, database *lib.Database) error { if setupErr := SetupStart(); setupErr != nil { return setupErr diff --git a/commands/lib/io.go b/commands/lib/io.go index 6d2f063..2573fad 100644 --- a/commands/lib/io.go +++ b/commands/lib/io.go @@ -96,7 +96,7 @@ func ReadLockFile() (int, error) { return intPid, nil } -func RemoveDanglingLock() { +func CountPimProcesses() int { processCount := 0 test, _ := ExecuteCommand("ps -u") for _, line := range strings.Split(test, "\n") { @@ -104,6 +104,11 @@ func RemoveDanglingLock() { processCount += 1 } } + return processCount +} + +func RemoveDanglingLock() { + processCount := CountPimProcesses() if processCount < 2 && LockExists() { removeErr := os.Remove(LOCKPATH) if removeErr != nil { diff --git a/pim.go b/pim.go index 085d8d8..e1f52d5 100644 --- a/pim.go +++ b/pim.go @@ -25,6 +25,8 @@ func parseCommand(command []string, process lib.Process, database *lib.Database) return pim.StartCommand(process, database) case "stop": return pim.StopCommand() + case "status": + return pim.StatusCommand() case "log", "logs": return pim.LogCommand(command, database) case "ls":