Step 15: CLI wiring, prune, and sgardd daemon.

Local prune: garden.Prune() removes orphaned blobs. 2 tests.

CLI commands: sgard push, sgard pull (with SSH auth via --ssh-key
or ssh-agent), sgard prune (local by default, remote with --remote).

Server daemon: cmd/sgardd with --listen, --repo, --authorized-keys
flags. Runs gRPC server with optional SSH key auth interceptor.

Root command gains --remote and --ssh-key persistent flags with
resolveRemote() (flag > env > repo/remote file).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 00:03:51 -07:00
parent 4b841cdd82
commit 94963bb8d6
9 changed files with 420 additions and 14 deletions

View File

@@ -4,11 +4,16 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
var repoFlag string
var (
repoFlag string
remoteFlag string
sshKeyFlag string
)
var rootCmd = &cobra.Command{
Use: "sgard",
@@ -23,8 +28,29 @@ func defaultRepo() string {
return filepath.Join(home, ".sgard")
}
// resolveRemote returns the remote address from flag, env, or repo config file.
func resolveRemote() (string, error) {
if remoteFlag != "" {
return remoteFlag, nil
}
if env := os.Getenv("SGARD_REMOTE"); env != "" {
return env, nil
}
// Try <repo>/remote file.
data, err := os.ReadFile(filepath.Join(repoFlag, "remote"))
if err == nil {
addr := strings.TrimSpace(string(data))
if addr != "" {
return addr, nil
}
}
return "", fmt.Errorf("no remote configured; use --remote, SGARD_REMOTE, or create %s/remote", repoFlag)
}
func main() {
rootCmd.PersistentFlags().StringVar(&repoFlag, "repo", defaultRepo(), "path to sgard repository")
rootCmd.PersistentFlags().StringVar(&remoteFlag, "remote", "", "gRPC server address (host:port)")
rootCmd.PersistentFlags().StringVar(&sshKeyFlag, "ssh-key", "", "path to SSH private key")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)

71
cmd/sgard/prune.go Normal file
View File

@@ -0,0 +1,71 @@
package main
import (
"context"
"fmt"
"github.com/kisom/sgard/client"
"github.com/kisom/sgard/garden"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var pruneCmd = &cobra.Command{
Use: "prune",
Short: "Remove orphaned blobs not referenced by the manifest",
Long: "Remove orphaned blobs locally, or on the remote server with --remote.",
RunE: func(cmd *cobra.Command, args []string) error {
addr, _ := resolveRemote()
if addr != "" {
return pruneRemote(addr)
}
return pruneLocal()
},
}
func pruneLocal() error {
g, err := garden.Open(repoFlag)
if err != nil {
return err
}
removed, err := g.Prune()
if err != nil {
return err
}
fmt.Printf("Pruned %d orphaned blob(s).\n", removed)
return nil
}
func pruneRemote(addr string) error {
signer, err := client.LoadSigner(sshKeyFlag)
if err != nil {
return err
}
creds := client.NewSSHCredentials(signer)
conn, err := grpc.NewClient(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithPerRPCCredentials(creds),
)
if err != nil {
return fmt.Errorf("connecting to %s: %w", addr, err)
}
defer func() { _ = conn.Close() }()
c := client.New(conn)
removed, err := c.Prune(context.Background())
if err != nil {
return err
}
fmt.Printf("Pruned %d orphaned blob(s) on remote.\n", removed)
return nil
}
func init() {
rootCmd.AddCommand(pruneCmd)
}

60
cmd/sgard/pull.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"context"
"fmt"
"github.com/kisom/sgard/client"
"github.com/kisom/sgard/garden"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var pullCmd = &cobra.Command{
Use: "pull",
Short: "Pull checkpoint from remote server",
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := resolveRemote()
if err != nil {
return err
}
g, err := garden.Open(repoFlag)
if err != nil {
return err
}
signer, err := client.LoadSigner(sshKeyFlag)
if err != nil {
return err
}
creds := client.NewSSHCredentials(signer)
conn, err := grpc.NewClient(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithPerRPCCredentials(creds),
)
if err != nil {
return fmt.Errorf("connecting to %s: %w", addr, err)
}
defer func() { _ = conn.Close() }()
c := client.New(conn)
pulled, err := c.Pull(context.Background(), g)
if err != nil {
return err
}
if pulled == 0 {
fmt.Println("Already up to date.")
} else {
fmt.Printf("Pulled %d blob(s).\n", pulled)
}
return nil
},
}
func init() {
rootCmd.AddCommand(pullCmd)
}

61
cmd/sgard/push.go Normal file
View File

@@ -0,0 +1,61 @@
package main
import (
"context"
"errors"
"fmt"
"github.com/kisom/sgard/client"
"github.com/kisom/sgard/garden"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var pushCmd = &cobra.Command{
Use: "push",
Short: "Push local checkpoint to remote server",
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := resolveRemote()
if err != nil {
return err
}
g, err := garden.Open(repoFlag)
if err != nil {
return err
}
signer, err := client.LoadSigner(sshKeyFlag)
if err != nil {
return err
}
creds := client.NewSSHCredentials(signer)
conn, err := grpc.NewClient(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithPerRPCCredentials(creds),
)
if err != nil {
return fmt.Errorf("connecting to %s: %w", addr, err)
}
defer func() { _ = conn.Close() }()
c := client.New(conn)
pushed, err := c.Push(context.Background(), g)
if errors.Is(err, client.ErrServerNewer) {
fmt.Println("Server is newer; run sgard pull instead.")
return nil
}
if err != nil {
return err
}
fmt.Printf("Pushed %d blob(s).\n", pushed)
return nil
},
}
func init() {
rootCmd.AddCommand(pushCmd)
}