feat(push): 'image push' to single or multiple machines via unregistry proxy

This commit is contained in:
Pasha Sviderski
2025-09-30 15:18:26 +10:00
parent bcc504b8cc
commit ffed906cef
2 changed files with 394 additions and 6 deletions
+28 -6
View File
@@ -1,14 +1,18 @@
package image
import (
"context"
"fmt"
"github.com/docker/compose/v2/pkg/progress"
"github.com/psviderski/uncloud/internal/cli"
"github.com/spf13/cobra"
)
type pushOptions struct {
image string
machines []string
context string
}
func NewPushCommand() *cobra.Command {
@@ -29,19 +33,37 @@ The image is uploaded to the machine which CLI is connected to (default) or the
uc image push myapp:latest -m machine1,machine2,machine3`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
image := args[0]
uncli := cmd.Context().Value("cli").(*cli.CLI)
machines := cli.ExpandCommaSeparatedValues(opts.machines)
// TODO: Implement image push logic
fmt.Printf("Would push image %q to machines: %v\n", image, machines)
return fmt.Errorf("image push not yet implemented")
opts.image = args[0]
return push(cmd.Context(), uncli, opts)
},
}
cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil,
"Machine names to push the image to. Can be specified multiple times or as a comma-separated "+
"list of machine names. (default is connected machine)")
cmd.Flags().StringVarP(
&opts.context, "context", "c", "",
"Name of the cluster context. (default is the current context)",
)
return cmd
}
func push(ctx context.Context, uncli *cli.CLI, opts pushOptions) error {
client, err := uncli.ConnectCluster(ctx, opts.context)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer client.Close()
machines := cli.ExpandCommaSeparatedValues(opts.machines)
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
if err = client.PushImage(ctx, opts.image, machines); err != nil {
return fmt.Errorf("push image to cluster: %w", err)
}
return nil
}, uncli.ProgressOut(), fmt.Sprintf("Pushing image %s to cluster", opts.image))
}