working commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PullImage(ctx context.Context, filepath, imagepath string, timeout time.Duration) error {
|
||||
var err error
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
|
||||
if cli.username != "" && cli.password != "" {
|
||||
ref, err := name.ParseReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defaultTransport := &roundTripper{}
|
||||
|
||||
scopes := []string{repo.Scope(transport.PullScope)}
|
||||
|
||||
regName := repo.RegistryStr()
|
||||
reg, err := name.NewRegistry(regName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: cli.username,
|
||||
Password: cli.password,
|
||||
}
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
transport := &roundTripper{}
|
||||
options = append(options, crane.WithTransport(transport))
|
||||
}
|
||||
|
||||
image, err := crane.Pull(imagepath, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstdir := makeTmpFileName(filepath)
|
||||
err = os.MkdirAll(dstdir, 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = crane.SaveOCI(image, dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = archiveDir(dstdir, filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.RemoveAll(dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user