Skip to content

Commit 6ca63c5

Browse files
committed
fix(doctor): add daemon service check and improve path handling with base folder validation
1 parent 16feced commit 6ca63c5

7 files changed

Lines changed: 83 additions & 21 deletions

File tree

commands/doctor.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,35 @@ func commandDoctor(c *cli.Context) error {
7272

7373
// 4. Check configuration
7474
printSectionHeader("Configuration")
75-
// TODO: Implement configuration checks
76-
printInfo("Configuration checks not yet implemented.")
75+
cfg, err := configService.ReadConfigFile(ctx)
76+
if err != nil {
77+
printError(fmt.Sprintf("Error reading config file: %v", err))
78+
return err
79+
}
80+
printSuccess("Configuration file is valid.")
81+
if cfg.EnableMetrics != nil && *cfg.EnableMetrics {
82+
printWarning("Metrics are enabled. it would has performance impact.")
83+
}
84+
85+
if cfg.DataMasking != nil && *cfg.DataMasking {
86+
printSuccess("Data masking is enabled.")
87+
}
88+
if cfg.Encrypted != nil && *cfg.Encrypted {
89+
printSuccess("Encrypted is enabled.")
90+
}
7791

7892
// 5. Check daemon process
7993
printSectionHeader("Daemon Process")
80-
// TODO: Implement daemon process check
81-
printInfo("Daemon process check not yet implemented.")
94+
daemonInstaller, err := model.NewDaemonInstaller("", "")
95+
if err != nil {
96+
printError(fmt.Sprintf("Error checking daemon installer: %v", err))
97+
return err
98+
}
99+
if err := daemonInstaller.Check(); err != nil {
100+
printWarning(fmt.Sprintf("Daemon is not running: %v. it's ok if you haven't installed it yet.", err))
101+
} else {
102+
printSuccess("Daemon is running.")
103+
}
82104

83105
// 6. Check user's current shell and PATH
84106
printSectionHeader("Shell Environment")

commands/hooks.install.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/gookit/color"
@@ -15,7 +16,7 @@ var HooksInstallCommand = &cli.Command{
1516
}
1617

1718
func commandHooksInstall(c *cli.Context) error {
18-
binFolder := os.ExpandEnv("$HOME/.shelltime/bin")
19+
binFolder := os.ExpandEnv(fmt.Sprintf("$HOME/%s/bin", model.COMMAND_BASE_STORAGE_FOLDER))
1920
if _, err := os.Stat(binFolder); os.IsNotExist(err) {
2021
color.Red.Println("📁 cannot find bin folder at", binFolder)
2122
color.Red.Println("Please run 'curl -sSL https://raw.githubusercontent.com/malamtime/installation/master/install.bash | bash' first")

model/daemon-installer.darwin.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,32 @@ func NewMacDaemonInstaller(baseFolder, user string) *MacDaemonInstaller {
3030
}
3131
}
3232

33+
func (m *MacDaemonInstaller) Check() error {
34+
cmd := exec.Command("launchctl", "print", "system/"+m.serviceName)
35+
if err := cmd.Run(); err == nil {
36+
return nil
37+
}
38+
return fmt.Errorf("service %s is not running", m.serviceName)
39+
}
40+
3341
func (m *MacDaemonInstaller) CheckAndStopExistingService() error {
3442
color.Yellow.Println("🔍 Checking if service is running...")
35-
cmd := exec.Command("launchctl", "list", m.serviceName)
36-
if err := cmd.Run(); err == nil {
37-
color.Yellow.Println("🛑 Stopping existing service...")
38-
if err := exec.Command("launchctl", "unload", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", m.serviceName)).Run(); err != nil {
39-
return fmt.Errorf("failed to stop existing service: %w", err)
40-
}
43+
44+
if err := m.Check(); err != nil {
45+
return err
46+
}
47+
48+
color.Yellow.Println("🛑 Stopping existing service...")
49+
if err := exec.Command("launchctl", "unload", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", m.serviceName)).Run(); err != nil {
50+
return fmt.Errorf("failed to stop existing service: %w", err)
4151
}
4252
return nil
4353
}
4454

4555
func (m *MacDaemonInstaller) InstallService(username string) error {
56+
if m.baseFolder == "" {
57+
return fmt.Errorf("base folder is not set")
58+
}
4659
daemonPath := filepath.Join(m.baseFolder, "daemon")
4760
// Create daemon directory if not exists
4861
if err := os.MkdirAll(daemonPath, 0755); err != nil {
@@ -68,6 +81,9 @@ func (m *MacDaemonInstaller) InstallService(username string) error {
6881
}
6982

7083
func (m *MacDaemonInstaller) RegisterService() error {
84+
if m.baseFolder == "" {
85+
return fmt.Errorf("base folder is not set")
86+
}
7187
plistPath := fmt.Sprintf("/Library/LaunchDaemons/%s.plist", m.serviceName)
7288
if _, err := os.Stat(plistPath); err != nil {
7389
sourceFile := filepath.Join(m.baseFolder, fmt.Sprintf("daemon/%s.plist", m.serviceName))
@@ -87,6 +103,9 @@ func (m *MacDaemonInstaller) StartService() error {
87103
}
88104

89105
func (m *MacDaemonInstaller) UnregisterService() error {
106+
if m.baseFolder == "" {
107+
return fmt.Errorf("base folder is not set")
108+
}
90109
color.Yellow.Println("🛑 Stopping service if running...")
91110
// Try to stop the service first
92111
_ = exec.Command("launchctl", "unload", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", m.serviceName)).Run()

model/daemon-installer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
// DaemonInstaller interface defines methods for daemon installation across different platforms
1010
type DaemonInstaller interface {
11+
Check() error
1112
CheckAndStopExistingService() error
1213
InstallService(username string) error
1314
RegisterService() error

model/daemon-installer.linux.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,32 @@ func NewLinuxDaemonInstaller(baseFolder, user string) *LinuxDaemonInstaller {
2525
return &LinuxDaemonInstaller{baseFolder: baseFolder, user: user}
2626
}
2727

28-
func (l *LinuxDaemonInstaller) CheckAndStopExistingService() error {
29-
color.Yellow.Println("🔍 Checking if service is running...")
28+
func (l *LinuxDaemonInstaller) Check() error {
3029
cmd := exec.Command("systemctl", "is-active", "shelltime")
3130
if err := cmd.Run(); err == nil {
32-
color.Yellow.Println("🛑 Stopping existing service...")
33-
if err := exec.Command("systemctl", "stop", "shelltime").Run(); err != nil {
34-
return fmt.Errorf("failed to stop existing service: %w", err)
35-
}
31+
return nil
32+
}
33+
return fmt.Errorf("service shelltime is not running")
34+
}
35+
36+
func (l *LinuxDaemonInstaller) CheckAndStopExistingService() error {
37+
color.Yellow.Println("🔍 Checking if service is running...")
38+
39+
if err := l.Check(); err != nil {
40+
return err
41+
}
42+
43+
color.Yellow.Println("🛑 Stopping existing service...")
44+
if err := exec.Command("systemctl", "stop", "shelltime").Run(); err != nil {
45+
return fmt.Errorf("failed to stop existing service: %w", err)
3646
}
3747
return nil
3848
}
3949

4050
func (l *LinuxDaemonInstaller) InstallService(username string) error {
51+
if l.baseFolder == "" {
52+
return fmt.Errorf("base folder is not set")
53+
}
4154
daemonPath := filepath.Join(l.baseFolder, "daemon")
4255
// Create daemon directory if not exists
4356
if err := os.MkdirAll(daemonPath, 0755); err != nil {
@@ -64,6 +77,9 @@ func (l *LinuxDaemonInstaller) InstallService(username string) error {
6477
}
6578

6679
func (l *LinuxDaemonInstaller) RegisterService() error {
80+
if l.baseFolder == "" {
81+
return fmt.Errorf("base folder is not set")
82+
}
6783
servicePath := "/etc/systemd/system/shelltime.service"
6884
if _, err := os.Stat(servicePath); err != nil {
6985
sourceFile := filepath.Join(l.baseFolder, "daemon/shelltime.service")
@@ -93,6 +109,9 @@ func (l *LinuxDaemonInstaller) StartService() error {
93109
}
94110

95111
func (l *LinuxDaemonInstaller) UnregisterService() error {
112+
if l.baseFolder == "" {
113+
return fmt.Errorf("base folder is not set")
114+
}
96115
color.Yellow.Println("🛑 Stopping and disabling service if running...")
97116
// Try to stop and disable the service
98117
_ = exec.Command("systemctl", "stop", "shelltime").Run()

model/shell.fish.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type FishHookService struct {
1818
}
1919

2020
func NewFishHookService() ShellHookService {
21-
sourceContent := os.ExpandEnv("$HOME/.shelltime/hooks/fish.fish")
21+
sourceContent := os.ExpandEnv(fmt.Sprintf("$HOME/%s/hooks/fish.fish", COMMAND_BASE_STORAGE_FOLDER))
2222
hookLines := []string{
2323
"# Added by shelltime CLI",
24-
"fish_add_path $HOME/.shelltime/bin",
24+
fmt.Sprintf("fish_add_path $HOME/%s/bin", COMMAND_BASE_STORAGE_FOLDER),
2525
fmt.Sprintf("source %s", sourceContent),
2626
}
2727
configPath := os.ExpandEnv("$HOME/.config/fish/config.fish")

model/shell.zsh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ type ZshHookService struct {
1717
}
1818

1919
func NewZshHookService() ShellHookService {
20-
sourceContent := os.ExpandEnv("$HOME/.shelltime/hooks/zsh.zsh")
20+
sourceContent := os.ExpandEnv(fmt.Sprintf("$HOME/%s/hooks/zsh.zsh", COMMAND_BASE_STORAGE_FOLDER))
2121
return &ZshHookService{
2222
shellName: "zsh",
2323
configPath: os.ExpandEnv("$HOME/.zshrc"),
2424
hookLines: []string{
2525
"# Added by shelltime CLI",
26-
`export PATH="$HOME/.shelltime/bin:$PATH"`,
26+
fmt.Sprintf("export PATH=\"$HOME/%s/bin:$PATH\"", COMMAND_BASE_STORAGE_FOLDER),
2727
fmt.Sprintf("source %s", sourceContent),
2828
},
2929
}

0 commit comments

Comments
 (0)