-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommand-line.go
More file actions
190 lines (159 loc) · 4.33 KB
/
Copy pathcommand-line.go
File metadata and controls
190 lines (159 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//go:build cli
package main
import (
"bufio"
"flag"
"fmt"
"io"
"nstudio/app/common/audio/player"
"nstudio/app/common/daemon"
"nstudio/app/common/util"
"os"
"time"
"github.com/charmbracelet/log"
)
type commandLineArguments struct {
Mode string
Port int
Host string
ConfigFile string
Status bool
Stop bool
Logs bool
Play string
Help bool
}
func processCommandLine() commandLineArguments {
mode := flag.String("mode", "help", "Server mode: gui, http, websocket, grpc, tcp, namedpipe, filesystem")
port := flag.Int("port", 8989, "Server port (for applicable modes)")
host := flag.String("host", "localhost", "Server host (for applicable modes)")
configFile := flag.String("config", "", "Path to custom config JSON file")
status := flag.Bool("status", false, "Check server status")
stop := flag.Bool("stop", false, "Stop running server")
logs := flag.Bool("logs", false, "Show server log file location")
play := flag.String("play", "", "Play an audio file (supports WAV, FLAC, OGG, MP3)")
help := flag.Bool("help", false, "Show help")
flag.Parse()
arguments := commandLineArguments{
Mode: *mode,
Port: *port,
Host: *host,
ConfigFile: *configFile,
Status: *status,
Stop: *stop,
Logs: *logs,
Play: *play,
Help: *help,
}
if arguments.Status {
handleStatus()
os.Exit(0)
}
if arguments.Stop {
handleStop()
os.Exit(0)
}
if arguments.Logs {
handleLogs()
os.Exit(0)
}
if arguments.Play != "" {
handlePlay(arguments.Play)
os.Exit(0)
}
if arguments.Help {
log.Info(helpText)
os.Exit(0)
}
return arguments
}
func handleStatus() {
running, pid, err := daemon.GetDaemonStatus()
if err != nil {
fmt.Printf("Error checking server status: %v\n", err)
os.Exit(1)
}
if !running {
fmt.Println("🔴 Server is not running")
os.Exit(1)
}
statusInfo, err := daemon.ReadDaemonStatus()
if err != nil {
fmt.Printf("🟡 Server is running (PID: %d) but status info unavailable: %v\n", pid, err)
fmt.Printf("Status file path: %s\n", daemon.GetStatusFilePath())
return
}
uptime := time.Since(statusInfo.StartTime)
fmt.Printf("🟢 Server is running (PID: %d)\n", statusInfo.PID)
fmt.Printf("Version: %s\n", statusInfo.Version)
fmt.Printf("Uptime: %s\n", util.FormatDuration(uptime))
fmt.Printf("Processed Messages: %d\n", statusInfo.ProcessedMessages)
}
func handleStop() {
running, pid, err := daemon.GetDaemonStatus()
if err != nil {
fmt.Printf("Error checking server status: %v\n", err)
os.Exit(1)
}
if !running {
fmt.Println("Server is not running")
os.Exit(1)
}
fmt.Printf("Stopping server (PID: %d)...\n", pid)
err = daemon.StopDaemon()
if err != nil {
fmt.Printf("Error stopping server: %v\n", err)
os.Exit(1)
}
fmt.Println("Server stopped successfully")
}
func handleLogs() {
logFilePath := daemon.GetLogFilePath()
// Check if log file exists
if _, err := os.Stat(logFilePath); os.IsNotExist(err) {
fmt.Printf("Log file does not exist: %s\n", logFilePath)
fmt.Println("Server may not be running or hasn't created logs yet.")
return
}
fmt.Printf("Attaching to server log: %s\n", logFilePath)
fmt.Println("Press Ctrl+C to exit")
fmt.Println("----------------------------------------")
file, err := os.Open(logFilePath)
if err != nil {
fmt.Printf("Error opening log file: %v\n", err)
os.Exit(1)
}
defer file.Close()
// Seek to end - 2048 bytes (approx) to show some context if file is large
stat, _ := file.Stat()
if stat.Size() > 2048 {
file.Seek(-2048, io.SeekEnd)
}
reader := bufio.NewReader(file)
if stat.Size() > 2048 {
// Read until newline to avoid partial line
reader.ReadBytes('\n')
}
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
time.Sleep(200 * time.Millisecond)
continue
}
fmt.Printf("Error reading log: %v\n", err)
break
}
fmt.Print(line)
// Small sleep to yield CPU
time.Sleep(10 * time.Millisecond)
}
}
func handlePlay(filePath string) {
fmt.Printf("Playing: %s\n", filePath)
err := player.PlayAudioFile(filePath)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}