15e4156802
Without this, double-clicking drover.exe printed CLI help to stdout — fine in a terminal but invisible to a user who just clicked an icon. Adding a RunE on the root command opens the smoke-test message box when no subcommand was given. CLI flags (--version, --help) and explicit subcommands keep their old behaviour. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
// Command drover is the entry point for the Discord proxy CLI.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.okcu.io/root/drover-go/internal/updater"
|
|
)
|
|
|
|
// Build-time variables, populated via -ldflags "-X main.Version=... -X main.Commit=... -X main.BuildDate=...".
|
|
var (
|
|
Version = "dev"
|
|
Commit = "dev"
|
|
BuildDate = "dev"
|
|
)
|
|
|
|
// configPath is the path to the TOML config file, set via the --config global flag.
|
|
// Reserved for use in later phases.
|
|
var configPath string
|
|
|
|
func main() {
|
|
// Inject our build version so the updater package can stamp it on the
|
|
// User-Agent header it sends to git.okcu.io.
|
|
updater.SetVersion(Version)
|
|
|
|
if err := newRootCmd().Execute(); err != nil {
|
|
// Cobra already prints the error; just exit non-zero.
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func newRootCmd() *cobra.Command {
|
|
root := &cobra.Command{
|
|
Use: "drover",
|
|
Short: "Discord proxy via SOCKS5 + WinDivert",
|
|
Version: fmt.Sprintf("%s (commit %s, built %s)", Version, Commit, BuildDate),
|
|
SilenceUsage: true,
|
|
SilenceErrors: false,
|
|
// No subcommand and no flags = end-user double-clicked the exe;
|
|
// open the smoke-test window instead of dumping CLI help to a
|
|
// console they didn't ask for.
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
showTestWindow()
|
|
return nil
|
|
},
|
|
}
|
|
|
|
// Custom version template: "drover-go vX.Y.Z (commit abc1234, built 2026-05-01)".
|
|
root.SetVersionTemplate(fmt.Sprintf("drover-go v%s (commit %s, built %s)\n", Version, Commit, BuildDate))
|
|
|
|
root.PersistentFlags().StringVar(&configPath, "config", "", "path to TOML config file (reserved)")
|
|
|
|
root.AddCommand(newCheckCmd())
|
|
root.AddCommand(newUpdateCmd())
|
|
root.AddCommand(newServiceCmd())
|
|
root.AddCommand(newGUICmd())
|
|
|
|
return root
|
|
}
|
|
|
|
func newGUICmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "gui",
|
|
Short: "Show a test window (smoke check that the binary launches on this machine)",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
showTestWindow()
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func newCheckCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "check",
|
|
Short: "Run the 7-step proxy diagnostic",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Fprintln(cmd.OutOrStdout(), "TODO: 7-step diagnostic")
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func newUpdateCmd() *cobra.Command {
|
|
var checkOnly bool
|
|
cmd := &cobra.Command{
|
|
Use: "update",
|
|
Short: "Self-update via the Forgejo Releases API",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
ctx := cmd.Context()
|
|
out := cmd.OutOrStdout()
|
|
|
|
src := updater.NewForgejoSource("git.okcu.io", "root", "drover-go", "windows-amd64.exe")
|
|
rel, hasUpdate, err := updater.CheckForUpdate(ctx, src, Version)
|
|
if err != nil {
|
|
return fmt.Errorf("check for update: %w", err)
|
|
}
|
|
if !hasUpdate {
|
|
fmt.Fprintln(out, "No updates available")
|
|
return nil
|
|
}
|
|
fmt.Fprintf(out, "Update available: %s (current v%s)\n", rel.TagName, Version)
|
|
if checkOnly {
|
|
return nil
|
|
}
|
|
|
|
fmt.Fprintln(out, "Downloading...")
|
|
if err := updater.ApplyUpdate(ctx, rel, func(d, t int64) {
|
|
if t > 0 {
|
|
fmt.Fprintf(out, "\r%d/%d bytes", d, t)
|
|
}
|
|
}); err != nil {
|
|
return fmt.Errorf("apply update: %w", err)
|
|
}
|
|
fmt.Fprintln(out, "\nUpdate applied. Restart drover.")
|
|
return nil
|
|
},
|
|
}
|
|
cmd.Flags().BoolVar(&checkOnly, "check-only", false, "only check for an update, do not apply")
|
|
return cmd
|
|
}
|
|
|
|
func newServiceCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "service",
|
|
Short: "Manage the drover Windows service",
|
|
}
|
|
for _, name := range []string{"install", "uninstall", "start", "stop"} {
|
|
name := name
|
|
cmd.AddCommand(&cobra.Command{
|
|
Use: name,
|
|
Short: fmt.Sprintf("%s the drover Windows service", name),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Fprintf(cmd.OutOrStdout(), "TODO: service %s\n", name)
|
|
return nil
|
|
},
|
|
})
|
|
}
|
|
return cmd
|
|
}
|