add status command

parent 912b6b8e
...@@ -19,6 +19,8 @@ user-facing configuration UI is expected to live in `ximperconf shell panel`. ...@@ -19,6 +19,8 @@ user-facing configuration UI is expected to live in `ximperconf shell panel`.
ximper-shell-panel list-modules ximper-shell-panel list-modules
ximper-shell-panel list-modules -o json ximper-shell-panel list-modules -o json
ximper-shell-panel start ximper-shell-panel start
ximper-shell-panel status
ximper-shell-panel status -o json
ximper-shell-panel stop ximper-shell-panel stop
ximper-shell-panel toggle ximper-shell-panel toggle
ximper-shell-panel restart ximper-shell-panel restart
......
...@@ -48,6 +48,19 @@ func main() { ...@@ -48,6 +48,19 @@ func main() {
Action: startCommand, Action: startCommand,
}, },
{ {
Name: "status",
Usage: "Show panel runtime status",
Action: statusCommand,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Usage: "output format (text, json)",
Value: "text",
Aliases: []string{"o"},
},
},
},
{
Name: "stop", Name: "stop",
Usage: "Stop the running panel", Usage: "Stop the running panel",
Action: stopCommand, Action: stopCommand,
...@@ -131,6 +144,36 @@ func startCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -131,6 +144,36 @@ func startCommand(ctx context.Context, cmd *cli.Command) error {
return StartWaybar() return StartWaybar()
} }
func statusCommand(ctx context.Context, cmd *cli.Command) error {
status := GetPanelStatus()
switch cmd.String("format") {
case "json":
data, err := json.MarshalIndent(status, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
case "text":
running := "no"
if status.Running {
running = "yes"
}
fmt.Println("Running:", running)
if status.PID != nil {
fmt.Println("PID:", *status.PID)
}
fmt.Println("Runtime dir:", status.RuntimeDir)
safeMode := "no"
if status.SafeMode {
safeMode = "yes"
}
fmt.Println("Safe mode:", safeMode)
default:
return fmt.Errorf("unsupported output format %q", cmd.String("format"))
}
return nil
}
func stopCommand(ctx context.Context, cmd *cli.Command) error { func stopCommand(ctx context.Context, cmd *cli.Command) error {
return StopWaybar() return StopWaybar()
} }
......
...@@ -12,6 +12,13 @@ import ( ...@@ -12,6 +12,13 @@ import (
"time" "time"
) )
type PanelStatus struct {
Running bool `json:"running"`
PID *int `json:"pid"`
RuntimeDir string `json:"runtime_dir"`
SafeMode bool `json:"safe_mode"`
}
func StartWaybar() error { func StartWaybar() error {
if PanelIsRunning() { if PanelIsRunning() {
return nil return nil
...@@ -65,15 +72,27 @@ func RestartWaybar() error { ...@@ -65,15 +72,27 @@ func RestartWaybar() error {
} }
func PanelIsRunning() bool { func PanelIsRunning() bool {
return GetPanelStatus().Running
}
func GetPanelStatus() PanelStatus {
status := PanelStatus{
RuntimeDir: RuntimeDir(),
SafeMode: SafeMode(),
}
pid, err := readPID() pid, err := readPID()
if err != nil { if err != nil {
return false return status
} }
if isPanelProcess(pid) { if !isPanelProcess(pid) {
return true _ = os.Remove(RuntimePIDPath())
return status
} }
_ = os.Remove(RuntimePIDPath())
return false status.Running = true
status.PID = &pid
return status
} }
func stopWaybar() error { func stopWaybar() error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment