|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/larkinwc/proxmox-lxc-compose/pkg/errors" |
| 9 | + "github.com/larkinwc/proxmox-lxc-compose/pkg/logging" |
| 10 | + "github.com/larkinwc/proxmox-lxc-compose/pkg/oci" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + rootCmd.AddCommand(imagesCmd) |
| 17 | + imagesCmd.AddCommand(pullCmd) |
| 18 | + imagesCmd.AddCommand(pushCmd) |
| 19 | + imagesCmd.AddCommand(listCmd) |
| 20 | + imagesCmd.AddCommand(removeCmd) |
| 21 | +} |
| 22 | + |
| 23 | +var imagesCmd = &cobra.Command{ |
| 24 | + Use: "images", |
| 25 | + Short: "Manage OCI images", |
| 26 | + Long: `Manage OCI images including pulling, pushing, listing and removing images`, |
| 27 | +} |
| 28 | + |
| 29 | +var pullCmd = &cobra.Command{ |
| 30 | + Use: "pull [registry/repository:tag]", |
| 31 | + Short: "Pull an image from a registry", |
| 32 | + Args: cobra.ExactArgs(1), |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + ref, err := oci.ParseImageReference(args[0]) |
| 35 | + if err != nil { |
| 36 | + return errors.Wrap(err, errors.ErrValidation, "invalid image reference") |
| 37 | + } |
| 38 | + |
| 39 | + logging.Info("Starting image pull", |
| 40 | + "image", args[0], |
| 41 | + "ref", ref) |
| 42 | + |
| 43 | + manager, err := getRegistryManager() |
| 44 | + if err != nil { |
| 45 | + return errors.Wrap(err, errors.ErrSystem, "failed to initialize registry manager") |
| 46 | + } |
| 47 | + |
| 48 | + if err := manager.Pull(cmd.Context(), ref); err != nil { |
| 49 | + if errors.IsType(err, errors.ErrRegistry) { |
| 50 | + logging.Error("Failed to pull image", |
| 51 | + "image", args[0], |
| 52 | + "error", err) |
| 53 | + } |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +var pushCmd = &cobra.Command{ |
| 62 | + Use: "push [registry/repository:tag]", |
| 63 | + Short: "Push an image to a registry", |
| 64 | + Args: cobra.ExactArgs(1), |
| 65 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 66 | + ref, err := oci.ParseImageReference(args[0]) |
| 67 | + if err != nil { |
| 68 | + return errors.Wrap(err, errors.ErrValidation, "invalid image reference") |
| 69 | + } |
| 70 | + |
| 71 | + logging.Info("Starting image push", |
| 72 | + "image", args[0], |
| 73 | + "ref", ref) |
| 74 | + |
| 75 | + manager, err := getRegistryManager() |
| 76 | + if err != nil { |
| 77 | + return errors.Wrap(err, errors.ErrSystem, "failed to initialize registry manager") |
| 78 | + } |
| 79 | + |
| 80 | + if err := manager.Push(cmd.Context(), ref); err != nil { |
| 81 | + if errors.IsType(err, errors.ErrRegistry) { |
| 82 | + logging.Error("Failed to push image", |
| 83 | + "image", args[0], |
| 84 | + "error", err) |
| 85 | + } |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +var listCmd = &cobra.Command{ |
| 94 | + Use: "list", |
| 95 | + Short: "List locally stored images", |
| 96 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 97 | + manager, err := getRegistryManager() |
| 98 | + if err != nil { |
| 99 | + return errors.Wrap(err, errors.ErrSystem, "failed to initialize registry manager") |
| 100 | + } |
| 101 | + |
| 102 | + images, err := manager.List(cmd.Context()) |
| 103 | + if err != nil { |
| 104 | + logging.Error("Failed to list images", "error", err) |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if len(images) == 0 { |
| 109 | + fmt.Println("No images found") |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Println("REPOSITORY\t\tTAG\t\tDIGEST") |
| 114 | + for _, img := range images { |
| 115 | + digest := img.Digest |
| 116 | + if digest == "" { |
| 117 | + digest = "-" |
| 118 | + } |
| 119 | + fmt.Printf("%s/%s\t\t%s\t\t%s\n", |
| 120 | + img.Registry, img.Repository, img.Tag, digest) |
| 121 | + } |
| 122 | + return nil |
| 123 | + }, |
| 124 | +} |
| 125 | + |
| 126 | +var removeCmd = &cobra.Command{ |
| 127 | + Use: "remove [registry/repository:tag]", |
| 128 | + Short: "Remove an image from local storage", |
| 129 | + Args: cobra.ExactArgs(1), |
| 130 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 131 | + ref, err := oci.ParseImageReference(args[0]) |
| 132 | + if err != nil { |
| 133 | + return errors.Wrap(err, errors.ErrValidation, "invalid image reference") |
| 134 | + } |
| 135 | + |
| 136 | + logging.Info("Removing image", |
| 137 | + "image", args[0], |
| 138 | + "ref", ref) |
| 139 | + |
| 140 | + manager, err := getRegistryManager() |
| 141 | + if err != nil { |
| 142 | + return errors.Wrap(err, errors.ErrSystem, "failed to initialize registry manager") |
| 143 | + } |
| 144 | + |
| 145 | + if err := manager.Delete(cmd.Context(), ref); err != nil { |
| 146 | + logging.Error("Failed to remove image", |
| 147 | + "image", args[0], |
| 148 | + "error", err) |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | + }, |
| 154 | +} |
| 155 | + |
| 156 | +func getRegistryManager() (*oci.RegistryManager, error) { |
| 157 | + homeDir, err := os.UserHomeDir() |
| 158 | + if err != nil { |
| 159 | + return nil, errors.Wrap(err, errors.ErrSystem, "failed to get home directory") |
| 160 | + } |
| 161 | + |
| 162 | + storageDir := filepath.Join(homeDir, ".lxc-compose", "images") |
| 163 | + manager, err := oci.NewRegistryManager(storageDir) |
| 164 | + if err != nil { |
| 165 | + return nil, errors.Wrap(err, errors.ErrSystem, "failed to create registry manager") |
| 166 | + } |
| 167 | + |
| 168 | + return manager, nil |
| 169 | +} |
0 commit comments