Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var DaemonCommand *cli.Command = &cli.Command{
Name: "daemon",
Usage: "shelltime daemon service",
Subcommands: []*cli.Command{
DaemonStatusCommand,
DaemonInstallCommand,
DaemonUninstallCommand,
DaemonReinstallCommand,
Expand Down
105 changes: 105 additions & 0 deletions commands/daemon.status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package commands

import (
"fmt"
"net"
"os"
"time"

"github.com/gookit/color"
"github.com/malamtime/cli/model"
"github.com/urfave/cli/v2"
)

var DaemonStatusCommand = &cli.Command{
Name: "status",
Usage: "Check the status of the shelltime daemon service",
Action: commandDaemonStatus,
}

func commandDaemonStatus(c *cli.Context) error {
ctx := c.Context

printSectionHeader("Daemon Status")

// Read config for socket path and feature flags
cfg, err := configService.ReadConfigFile(ctx)
socketPath := model.DefaultSocketPath
if err == nil && cfg.SocketPath != "" {
socketPath = cfg.SocketPath
}

// Check 1: Socket file existence
socketExists := checkSocketFileExists(socketPath)
if socketExists {
printSuccess(fmt.Sprintf("Socket file exists at %s", socketPath))
} else {
printError(fmt.Sprintf("Socket file does not exist at %s", socketPath))
}

// Check 2: Socket connectivity
connected, latency, connErr := checkSocketConnection(socketPath, 2*time.Second)
if connected {
printSuccess(fmt.Sprintf("Daemon is responding (latency: %v)", latency.Round(time.Microsecond)))
} else {
if socketExists {
printError(fmt.Sprintf("Cannot connect to daemon: %v", connErr))
} else {
printError("Cannot connect to daemon (socket not found)")
}
}

// Check 3: Service manager status
installer, installerErr := model.NewDaemonInstaller("", "")
if installerErr == nil {
if err := installer.Check(); err == nil {
printSuccess("Service is registered and running")
} else {
printWarning("Service is not running via system service manager")
}
}
Comment on lines +53 to +60
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Calling model.NewDaemonInstaller with empty strings for baseFolder and username will likely cause the service status check to fail, as the installer needs this information to locate service definition files. You should fetch the current user's details to construct the correct paths, similar to how it's handled in the install and uninstall commands. Note that this change requires importing the os/user and path/filepath packages.

Suggested change
installer, installerErr := model.NewDaemonInstaller("", "")
if installerErr == nil {
if err := installer.Check(); err == nil {
printSuccess("Service is registered and running")
} else {
printWarning("Service is not running via system service manager")
}
}
// Check 3: Service manager status
if currentUser, err := user.Current(); err == nil {
baseFolder := filepath.Join(currentUser.HomeDir, ".shelltime")
if installer, err := model.NewDaemonInstaller(baseFolder, currentUser.Username); err == nil {
if err := installer.Check(); err == nil {
printSuccess("Service is registered and running")
} else {
printWarning("Service is not running via system service manager")
}
}
}


// Configuration section
printSectionHeader("Configuration")
fmt.Printf(" Socket Path: %s\n", socketPath)

if cfg.CCOtel != nil && cfg.CCOtel.Enabled != nil && *cfg.CCOtel.Enabled {
fmt.Printf(" CCOtel: enabled (port %d)\n", cfg.CCOtel.GRPCPort)
} else {
fmt.Println(" CCOtel: disabled")
}

if cfg.CodeTracking != nil && cfg.CodeTracking.Enabled != nil && *cfg.CodeTracking.Enabled {
fmt.Println(" Code Tracking: enabled")
} else {
fmt.Println(" Code Tracking: disabled")
}
Comment on lines +66 to +76
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The nested nil checks for cfg.CCOtel.Enabled and cfg.CodeTracking.Enabled are verbose and repetitive. You can improve readability by extracting the boolean conditions into variables.

Suggested change
if cfg.CCOtel != nil && cfg.CCOtel.Enabled != nil && *cfg.CCOtel.Enabled {
fmt.Printf(" CCOtel: enabled (port %d)\n", cfg.CCOtel.GRPCPort)
} else {
fmt.Println(" CCOtel: disabled")
}
if cfg.CodeTracking != nil && cfg.CodeTracking.Enabled != nil && *cfg.CodeTracking.Enabled {
fmt.Println(" Code Tracking: enabled")
} else {
fmt.Println(" Code Tracking: disabled")
}
ccOtelEnabled := cfg.CCOtel != nil && cfg.CCOtel.Enabled != nil && *cfg.CCOtel.Enabled
if ccOtelEnabled {
fmt.Printf(" CCOtel: enabled (port %d)\n", cfg.CCOtel.GRPCPort)
} else {
fmt.Println(" CCOtel: disabled")
}
codeTrackingEnabled := cfg.CodeTracking != nil && cfg.CodeTracking.Enabled != nil && *cfg.CodeTracking.Enabled
if codeTrackingEnabled {
fmt.Println(" Code Tracking: enabled")
} else {
fmt.Println(" Code Tracking: disabled")
}


// Overall status
fmt.Println()
if connected {
color.Green.Println("Status: Running")
} else {
color.Red.Println("Status: Stopped")
fmt.Println()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This extra fmt.Println() call will add a second blank line when the daemon is stopped. The pull request description mentions that output formatting should match existing patterns, and this extra line could create an inconsistency. Removing it will make the output cleaner.

color.Yellow.Println("Run 'shelltime daemon install' to start the daemon service.")
}

return nil
}

func checkSocketFileExists(socketPath string) bool {
_, err := os.Stat(socketPath)
return err == nil
}

func checkSocketConnection(socketPath string, timeout time.Duration) (bool, time.Duration, error) {
start := time.Now()
conn, err := net.DialTimeout("unix", socketPath, timeout)
if err != nil {
return false, 0, err
}
defer conn.Close()
latency := time.Since(start)
return true, latency, nil
}