Skip to content

Commit e2d9a73

Browse files
authored
Merge pull request #4 from larkinwc/fix/move-cmd
feat: Moving cmd around
2 parents 6d13f94 + f6ae176 commit e2d9a73

File tree

5 files changed

+172
-90
lines changed

5 files changed

+172
-90
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ release_todos.md
55

66

77
# package
8-
lxc-compose
8+
/lxc-compose
99
templates

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ The main purpose of the CLI is to read a lxc-compose.yml file and use it to crea
128128

129129
## Installation
130130

131+
Install directly using Go:
131132
```bash
132-
go install github.com/larkinwc/proxmox-lxc-compose@latest
133+
go install github.com/larkinwc/proxmox-lxc-compose/cmd/lxc-compose@latest
133134
```
134135

135136
Or build from source:
136-
137137
```bash
138138
git clone https://github.com/larkinwc/proxmox-lxc-compose.git
139139
cd proxmox-lxc-compose

cmd/lxc-compose/images.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

internal/cli/root.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

main.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)