Replace per-call SSH signing with a two-layer auth system: Server: AuthInterceptor verifies JWT tokens (HMAC-SHA256 signed with repo-local jwt.key). Authenticate RPC accepts SSH-signed challenges and issues 30-day JWTs. Expired-but-valid tokens return a ReauthChallenge in error details (server-provided nonce for fast re-auth). Authenticate RPC is exempt from token requirement. Client: TokenCredentials replaces SSHCredentials as the primary PerRPCCredentials. NewWithAuth creates clients with auto-renewal — EnsureAuth obtains initial token, retryOnAuth catches Unauthenticated errors and re-authenticates transparently. Token cached at $XDG_STATE_HOME/sgard/token (0600). CLI: dialRemote() helper handles token loading, connection setup, and initial auth. Push/pull/prune commands simplified to use it. Proto: Added Authenticate RPC, AuthenticateRequest/Response, ReauthChallenge messages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
61 lines
1013 B
Go
61 lines
1013 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/kisom/sgard/garden"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
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()
|
|
}
|
|
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() error {
|
|
ctx := context.Background()
|
|
|
|
c, cleanup, err := dialRemote(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cleanup()
|
|
|
|
removed, err := c.Prune(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Pruned %d orphaned blob(s) on remote.\n", removed)
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(pruneCmd)
|
|
}
|