Skip to content

Commit

Permalink
add status command (#10)
Browse files Browse the repository at this point in the history
add `pim status` command that shows if pim is currently running its pid.
  • Loading branch information
TimoKats authored Oct 18, 2024
1 parent a6d7772 commit e56efd1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion commands/lib/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,19 @@ 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") {
if strings.Contains(line, "pim start") {
processCount += 1
}
}
return processCount
}

func RemoveDanglingLock() {
processCount := CountPimProcesses()
if processCount < 2 && LockExists() {
removeErr := os.Remove(LOCKPATH)
if removeErr != nil {
Expand Down
2 changes: 2 additions & 0 deletions pim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit e56efd1

Please sign in to comment.