Refactor project structure
This commit is contained in:
40
cmd/images.go
Normal file
40
cmd/images.go
Normal file
@ -0,0 +1,40 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitea.stuzer.link/stuzer05/docker-registry-manager/internal/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func RegistryImages(app *app.App) {
|
||||
type catalog struct {
|
||||
Repositories []string `json:"repositories"`
|
||||
}
|
||||
|
||||
resp, err := http.Get(app.RegistryURL + "/v2/_catalog")
|
||||
if err != nil {
|
||||
fmt.Println("Error fetching images:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
fmt.Println("No images found in the registry.")
|
||||
return
|
||||
}
|
||||
|
||||
var cat catalog
|
||||
if err := json.NewDecoder(resp.Body).Decode(&cat); err != nil {
|
||||
fmt.Println("Error decoding response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(cat.Repositories) == 0 {
|
||||
fmt.Println("No images found in the registry.")
|
||||
} else {
|
||||
for _, img := range cat.Repositories {
|
||||
fmt.Println(img)
|
||||
}
|
||||
}
|
||||
}
|
22
cmd/push.go
Normal file
22
cmd/push.go
Normal file
@ -0,0 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.stuzer.link/stuzer05/docker-registry-manager/internal/app"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegistryPush(app *app.App, imageName string) {
|
||||
if !strings.HasPrefix(imageName, app.RegistryName) {
|
||||
imageName = app.RegistryName + "/" + imageName
|
||||
}
|
||||
|
||||
cmd := exec.Command("docker", "push", imageName)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Println("Error pushing image:", err)
|
||||
}
|
||||
}
|
101
cmd/rm.go
Normal file
101
cmd/rm.go
Normal file
@ -0,0 +1,101 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitea.stuzer.link/stuzer05/docker-registry-manager/internal/app"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegistryRm(app *app.App, imageArg string) {
|
||||
parts := strings.SplitN(imageArg, ":", 2)
|
||||
imageName := parts[0]
|
||||
tag := ""
|
||||
if len(parts) > 1 {
|
||||
tag = parts[1]
|
||||
}
|
||||
|
||||
if tag == "" {
|
||||
deleteImageByName(app, imageName)
|
||||
} else {
|
||||
deleteImageByTag(app, imageName, tag)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteImageByName(app *app.App, imageName string) {
|
||||
type tagList struct {
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
resp, err := http.Get(fmt.Sprintf("%s/v2/%s/tags/list", app.RegistryURL, imageName))
|
||||
if err != nil {
|
||||
fmt.Println("Error fetching tags:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
fmt.Printf("Error: Image '%s' not found in the registry.\n", imageName)
|
||||
return
|
||||
}
|
||||
|
||||
var tl tagList
|
||||
if err := json.NewDecoder(resp.Body).Decode(&tl); err != nil {
|
||||
fmt.Println("Error decoding response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, tag := range tl.Tags {
|
||||
deleteImageByTag(app, imageName, tag)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteImageByTag(app *app.App, imageName, tag string) {
|
||||
client := &http.Client{}
|
||||
|
||||
req, err := http.NewRequest("HEAD", fmt.Sprintf("%s/v2/%s/manifests/%s", app.RegistryURL, imageName, tag), nil)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating request:", err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Error fetching manifest:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
fmt.Printf("Error: Image '%s:%s' not found in the registry.\n", imageName, tag)
|
||||
return
|
||||
}
|
||||
|
||||
digest := resp.Header.Get("Docker-Content-Digest")
|
||||
|
||||
req, err = http.NewRequest("DELETE", fmt.Sprintf("%s/v2/%s/manifests/%s", app.RegistryURL, imageName, digest), nil)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating delete request:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Join username and password for Basic Auth
|
||||
registryCredentials := app.RegistryUsername + ":" + app.RegistryPassword
|
||||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(registryCredentials)))
|
||||
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Error deleting image:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusAccepted {
|
||||
fmt.Printf("Deleted image: %s:%s\n", imageName, tag)
|
||||
} else {
|
||||
fmt.Printf("Failed to delete image: %s:%s (status code: %d)\n", imageName, tag, resp.StatusCode)
|
||||
}
|
||||
}
|
22
cmd/tag.go
Normal file
22
cmd/tag.go
Normal file
@ -0,0 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.stuzer.link/stuzer05/docker-registry-manager/internal/app"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegistryTag(app *app.App, sourceImage, targetImage string) {
|
||||
if !strings.HasPrefix(targetImage, app.RegistryName) {
|
||||
targetImage = app.RegistryName + "/" + targetImage
|
||||
}
|
||||
|
||||
cmd := exec.Command("docker", "tag", sourceImage, targetImage)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Println("Error tagging image:", err)
|
||||
}
|
||||
}
|
40
cmd/tags.go
Normal file
40
cmd/tags.go
Normal file
@ -0,0 +1,40 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitea.stuzer.link/stuzer05/docker-registry-manager/internal/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func RegistryTags(app *app.App, imageName string) {
|
||||
type tagList struct {
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
resp, err := http.Get(fmt.Sprintf("%s/v2/%s/tags/list", app.RegistryURL, imageName))
|
||||
if err != nil {
|
||||
fmt.Println("Error fetching tags:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
fmt.Printf("Error: Image '%s' not found in the registry.\n", imageName)
|
||||
return
|
||||
}
|
||||
|
||||
var tl tagList
|
||||
if err := json.NewDecoder(resp.Body).Decode(&tl); err != nil {
|
||||
fmt.Println("Error decoding response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(tl.Tags) == 0 {
|
||||
fmt.Printf("No tags found for image: %s\n", imageName)
|
||||
} else {
|
||||
for _, tag := range tl.Tags {
|
||||
fmt.Println(tag)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user