Skip to content

Commit

Permalink
add auth flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Horiodino committed Aug 3, 2024
1 parent a394de7 commit 02c1a06
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
10 changes: 5 additions & 5 deletions cmd/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

var (
platform, output, tag, path string
platform, output, tag, path, auth string
push, loadDocker, loadPodman, devDeps, dfSwap, digest bool
)
var (
Expand All @@ -39,6 +39,7 @@ func init() {
OCICmd.Flags().StringVarP(&tag, "tag", "t", "", "The tag that will be replaced with original tag in Dockerfile")
OCICmd.Flags().StringVar(&path, "path", "", "The path to Dockerfile")
OCICmd.Flags().BoolVar(&digest, "digest", false, "Get the digest of the image")
OCICmd.Flags().StringVar(&auth, "auth", "", "The auth to use to authenticate to the registry")
}

// OCICmd represents the export command
Expand Down Expand Up @@ -213,20 +214,19 @@ var OCICmd = &cobra.Command{

if push {
fmt.Println(styles.HighlightStyle.Render("Pushing image to registry..."))
err = oci.Push(output+"/result", artifact.Name)
err = oci.Push(output+"/result", artifact.Name, digest, auth)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}
fmt.Println(styles.SucessStyle.Render(fmt.Sprintf("Image %s pushed to registry", artifact.Name)))

if digest {
fmt.Println(styles.HighlightStyle.Render("Getting digest of the image..."))
err = oci.GetDigest(artifact.Name)
digest, err := os.ReadFile("bsf-result/digest")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}
fmt.Println(styles.SucessStyle.Render(fmt.Sprintf("Digest: %s", string(digest))))
}
}
},
Expand Down
23 changes: 8 additions & 15 deletions pkg/oci/skopeo.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package oci

import (
"fmt"
"os"
"os/exec"

"github.com/google/go-containerregistry/pkg/crane"
)

// LoadDocker loads the image to the docker daemon
Expand Down Expand Up @@ -35,8 +32,14 @@ func LoadPodman(dir, imageName string) error {
}

// Push image to registry
func Push(dir, imageName string) error {
cmd := exec.Command("nix", "run", "nixpkgs#skopeo", "--", "copy", "--insecure-policy", "dir:"+dir, "docker://"+imageName)
func Push(dir, imageName string, digest bool, auth string) error {
var cmd *exec.Cmd
if digest {
cmd = exec.Command("skopeo", "copy", "--insecure-policy", "dir:"+dir, "docker://"+imageName+"@@unknown-digest@@", "--digestfile=bsf-result/digest", "--dest-creds", auth)

} else {
cmd = exec.Command("nix", "run", "nixpkgs#skopeo", "--", "copy", "--insecure-policy", "dir:"+dir, "docker://"+imageName, "--dest-creds", auth)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
Expand All @@ -46,13 +49,3 @@ func Push(dir, imageName string) error {

return nil
}

func GetDigest(image string) error {
digest, err := crane.Digest(image)
if err != nil {
return err
}

fmt.Println("Digest:", digest)
return nil
}

0 comments on commit 02c1a06

Please sign in to comment.