|
| 1 | +package tag |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "kitops/pkg/lib/constants" |
| 7 | + "kitops/pkg/lib/repo" |
| 8 | + "kitops/pkg/output" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "oras.land/oras-go/v2/registry" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + shortDesc = "Tag a modelkit" |
| 16 | + longDesc = `Tag a modelkit with a new tag.` |
| 17 | +) |
| 18 | + |
| 19 | +type tagOptions struct { |
| 20 | + configHome string |
| 21 | + sourceRef *registry.Reference |
| 22 | + targetRef *registry.Reference |
| 23 | +} |
| 24 | + |
| 25 | +func (opts *tagOptions) complete(ctx context.Context, args []string) error { |
| 26 | + |
| 27 | + configHome, ok := ctx.Value(constants.ConfigKey{}).(string) |
| 28 | + if !ok { |
| 29 | + return fmt.Errorf("default config path not set on command context") |
| 30 | + } |
| 31 | + opts.configHome = configHome |
| 32 | + modelRef, _, err := repo.ParseReference(args[0]) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to parse reference %s: %w", opts.sourceRef, err) |
| 35 | + } |
| 36 | + opts.sourceRef = modelRef |
| 37 | + modelRef, _, err = repo.ParseReference(args[1]) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("failed to parse reference %s: %w", opts.targetRef, err) |
| 40 | + } |
| 41 | + opts.targetRef = modelRef |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func TagCommand() *cobra.Command { |
| 46 | + |
| 47 | + cmd := &cobra.Command{ |
| 48 | + Use: "tag SOURCE_MODELKIT[:TAG] TARGET_MODELKIT[:TAG]", |
| 49 | + Short: shortDesc, |
| 50 | + Long: longDesc, |
| 51 | + Example: `kit tag myregistry.com/myrepo/mykit:latest myregistry.com/myrepo/mykit:v1.0.0`, |
| 52 | + Run: runCommand(&tagOptions{}), |
| 53 | + } |
| 54 | + |
| 55 | + cmd.Args = cobra.ExactArgs(2) |
| 56 | + return cmd |
| 57 | +} |
| 58 | + |
| 59 | +func runCommand(opts *tagOptions) func(cmd *cobra.Command, args []string) { |
| 60 | + return func(cmd *cobra.Command, args []string) { |
| 61 | + if err := opts.complete(cmd.Context(), args); err != nil { |
| 62 | + output.Fatalf("Failed to parse argument: %s", err) |
| 63 | + } |
| 64 | + |
| 65 | + err := RunTag(cmd.Context(), opts) |
| 66 | + if err != nil { |
| 67 | + output.Fatalf("Failed to tag modelkit: %s", err) |
| 68 | + } |
| 69 | + output.Infof("Modelkit %s tagged as %s", opts.sourceRef, opts.targetRef) |
| 70 | + } |
| 71 | +} |
0 commit comments