add status command

parent 912b6b8e
......@@ -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 -o json
ximper-shell-panel start
ximper-shell-panel status
ximper-shell-panel status -o json
ximper-shell-panel stop
ximper-shell-panel toggle
ximper-shell-panel restart
......
......@@ -48,6 +48,19 @@ func main() {
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",
Usage: "Stop the running panel",
Action: stopCommand,
......@@ -131,6 +144,36 @@ func startCommand(ctx context.Context, cmd *cli.Command) error {
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 {
return StopWaybar()
}
......
......@@ -12,6 +12,13 @@ import (
"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 {
if PanelIsRunning() {
return nil
......@@ -65,15 +72,27 @@ func RestartWaybar() error {
}
func PanelIsRunning() bool {
return GetPanelStatus().Running
}
func GetPanelStatus() PanelStatus {
status := PanelStatus{
RuntimeDir: RuntimeDir(),
SafeMode: SafeMode(),
}
pid, err := readPID()
if err != nil {
return false
}
if isPanelProcess(pid) {
return true
return status
}
if !isPanelProcess(pid) {
_ = os.Remove(RuntimePIDPath())
return false
return status
}
status.Running = true
status.PID = &pid
return status
}
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