-
Notifications
You must be signed in to change notification settings - Fork 62
/
push.go
70 lines (55 loc) · 1.61 KB
/
push.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cmd
import (
"fmt"
"github.com/cppforlife/go-cli-ui/ui"
regname "github.com/google/go-containerregistry/pkg/name"
ctlimg "github.com/k14s/imgpkg/pkg/imgpkg/image"
"github.com/spf13/cobra"
)
type PushOptions struct {
ui ui.UI
ImageFlags ImageFlags
FileFlags FileFlags
RegistryFlags RegistryFlags
}
func NewPushOptions(ui ui.UI) *PushOptions {
return &PushOptions{ui: ui}
}
func NewPushCmd(o *PushOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "push",
Short: "Push files as image",
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
Example: `
# Push image dkalinin/app1-config with contents of config/ directory
imgpkg push -i dkalinin/app1-config -f config/
# Push image dkalinin/app1-config with contents from multiple locations
imgpkg push -i dkalinin/app1-config -f config/ -f additional-config.yml`,
}
o.ImageFlags.Set(cmd)
o.FileFlags.Set(cmd)
o.RegistryFlags.Set(cmd)
return cmd
}
func (o *PushOptions) Run() error {
uploadRef, err := regname.NewTag(o.ImageFlags.Image, regname.WeakValidation)
if err != nil {
return fmt.Errorf("Parsing image '%s': %s", o.ImageFlags.Image, err)
}
registry := ctlimg.NewRegistry(o.RegistryFlags.AsRegistryOpts())
img, err := ctlimg.NewTarImage(o.FileFlags.Files).AsFileImage()
if err != nil {
return err
}
defer img.Remove()
err = registry.WriteImage(uploadRef, img)
if err != nil {
return fmt.Errorf("Writing image '%s': %s", uploadRef.Name(), err)
}
digest, err := img.Digest()
if err != nil {
return err
}
o.ui.BeginLinef("Pushed image '%s@%s'\n", uploadRef.Context(), digest)
return nil
}