backupy-agent/apps/agent/cmd/agent/main.go
TronoSfera 8b0c978337 feat(initial): Backupy agent + backupy-decrypt CLI
Source ports from the TronoSfera/backupy-cloud monorepo:
- apps/agent/        — Go agent (WSS client, persistent queue, Docker
                       discovery, 5 DB drivers: PG/MySQL/Mongo/Redis/SQLite,
                       pre/post hooks, Prometheus metrics)
- apps/backupy-decrypt/ — standalone CLI for client-side decryption
- packages/proto/    — protobuf wire format (generated .pb.go committed
                       so the repo builds without protoc)
- docs/              — agent spec + wire-protocol contract

Apache-2.0 license. Image published to ghcr.io/tronosfera/backupy-agent
on every v* tag via .github/workflows/release.yml (multi-arch amd64+arm64).
2026-05-17 20:22:35 +03:00

72 lines
2.2 KiB
Go

// Command agent is the Backupy agent — connects to the control plane,
// runs backups, and reports status. See docs/03-agent-spec.md.
//
// Subcommands (per spec §3.7):
//
// agent run — start the service loop (default)
// agent version — print build metadata
// agent health-check — used as Docker HEALTHCHECK; exits 0 when healthy
// agent dump-state — debug-only state dump as JSON
//
// `agent self-update` is documented in the spec but landing in task D-15.
package main
import (
"fmt"
"log/slog"
"os"
"github.com/spf13/cobra"
"github.com/backupy/backupy/apps/agent/internal/version"
)
func main() {
root := newRootCmd()
if err := root.Execute(); err != nil {
// Cobra has already printed the error; this final line guarantees
// a non-zero exit code for environments that ignore Execute's err.
fmt.Fprintln(os.Stderr, "agent: fatal:", err)
os.Exit(1)
}
}
// newRootCmd wires up all subcommands. Exported as a factory so tests
// (and future `agent_test.go`) can construct an isolated root command.
func newRootCmd() *cobra.Command {
root := &cobra.Command{
Use: "agent",
Short: "Backupy agent — connects to the platform and runs backups",
Long: "Backupy agent service. See docs/03-agent-spec.md for the full spec.",
Version: version.Full(),
SilenceUsage: true,
SilenceErrors: false,
}
// When invoked with no subcommand, default to `run`. This makes the
// Docker ENTRYPOINT clean: ["/usr/local/bin/agent"] just works.
root.AddCommand(newRunCmd())
root.AddCommand(newVersionCmd())
root.AddCommand(newHealthCheckCmd())
root.AddCommand(newDumpStateCmd())
// Default to `run` when no args given (Docker CMD convention).
root.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("unknown command %q", args[0])
}
return newRunCmd().RunE(cmd, args)
}
return root
}
// fatal logs at error level and exits non-zero. Used by subcommands that
// want a uniform exit path after a structured log line.
func fatal(logger *slog.Logger, msg string, err error) {
if logger == nil {
logger = slog.Default()
}
logger.Error(msg, slog.Any("err", err))
os.Exit(1)
}