feat: re-implement subcommands

This commit is contained in:
Rob Watson 2025-05-15 06:30:52 +02:00
parent 3159616317
commit 8db2caf6a1

75
main.go
View File

@ -8,6 +8,7 @@ import (
"io" "io"
"log/slog" "log/slog"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
@ -47,6 +48,23 @@ func (e errInterrupt) ExitCode() int {
} }
func main() { func main() {
serverSubcommands := []*cli.Command{
{
Name: "print-config",
Usage: "Print the config file path",
Action: func(*cli.Context) error {
return printConfig()
},
},
{
Name: "edit-config",
Usage: "Edit the config file",
Action: func(*cli.Context) error {
return editConfig()
},
},
}
app := &cli.App{ app := &cli.App{
Name: "Octoplex", Name: "Octoplex",
Usage: "Octoplex is a live video restreamer for Docker.", Usage: "Octoplex is a live video restreamer for Docker.",
@ -68,13 +86,22 @@ func main() {
waitForClient: false, waitForClient: false,
}) })
}, },
Subcommands: serverSubcommands,
}, },
{ {
Name: "run", Name: "run",
Usage: "Run server and client together (testing)", Usage: "Run server and client in the same process",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return runClientAndServer(c) return runClientAndServer(c)
}, },
Subcommands: serverSubcommands,
},
{
Name: "version",
Usage: "Print the version",
Action: func(*cli.Context) error {
return printVersion()
},
}, },
}, },
} }
@ -250,3 +277,49 @@ func runClientAndServer(c *cli.Context) error {
return err return err
} }
} }
// printConfig prints the path to the config file to stderr.
func printConfig() error {
configService, err := config.NewDefaultService()
if err != nil {
return fmt.Errorf("build config service: %w", err)
}
fmt.Fprintln(os.Stderr, configService.Path())
return nil
}
// editConfig opens the config file in the user's editor.
func editConfig() error {
configService, err := config.NewDefaultService()
if err != nil {
return fmt.Errorf("build config service: %w", err)
}
if _, err = configService.ReadOrCreateConfig(); err != nil {
return fmt.Errorf("read or create config: %w", err)
}
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
binary, err := exec.LookPath(editor)
if err != nil {
return fmt.Errorf("look path: %w", err)
}
fmt.Fprintf(os.Stderr, "Editing config file: %s\n", configService.Path())
if err := syscall.Exec(binary, []string{"--", configService.Path()}, os.Environ()); err != nil {
return fmt.Errorf("exec: %w", err)
}
return nil
}
// printVersion prints the version of the application to stderr.
func printVersion() error {
fmt.Fprintf(os.Stderr, "%s version %s\n", domain.AppName, cmp.Or(version, "0.0.0-dev"))
return nil
}