forked from google/go-containerregistry
/
root.go
145 lines (128 loc) · 4.33 KB
/
root.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/docker/cli/cli/config"
"github.com/google/go-containerregistry/internal/cmd"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
)
const (
use = "crane"
short = "Crane is a tool for managing container images"
)
var Root = New(use, short, []crane.Option{})
// New returns a top-level command for crane. This is mostly exposed
// to share code with gcrane.
func New(use, short string, options []crane.Option) *cobra.Command {
verbose := false
insecure := false
ndlayers := false
platform := &platformValue{}
root := &cobra.Command{
Use: use,
Short: short,
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Usage() },
DisableAutoGenTag: true,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
options = append(options, crane.WithContext(cmd.Context()))
// TODO(jonjohnsonjr): crane.Verbose option?
if verbose {
logs.Debug.SetOutput(os.Stderr)
}
if insecure {
options = append(options, crane.Insecure)
}
if ndlayers {
options = append(options, crane.WithNondistributable())
}
if Version != "" {
binary := "crane"
if len(os.Args[0]) != 0 {
binary = filepath.Base(os.Args[0])
}
options = append(options, crane.WithUserAgent(fmt.Sprintf("%s/%s", binary, Version)))
}
options = append(options, crane.WithPlatform(platform.platform))
transport := remote.DefaultTransport.Clone()
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: insecure, //nolint: gosec
}
var rt http.RoundTripper = transport
// Add any http headers if they are set in the config file.
cf, err := config.Load(os.Getenv("DOCKER_CONFIG"))
if err != nil {
logs.Debug.Printf("failed to read config file: %v", err)
} else if len(cf.HTTPHeaders) != 0 {
rt = &headerTransport{
inner: rt,
httpHeaders: cf.HTTPHeaders,
}
}
options = append(options, crane.WithTransport(rt))
},
}
commands := []*cobra.Command{
NewCmdAppend(&options),
NewCmdAuth(options, "crane", "auth"),
NewCmdBlob(&options),
NewCmdCatalog(&options),
NewCmdConfig(&options),
NewCmdCopy(&options),
NewCmdDelete(&options),
NewCmdDigest(&options),
cmd.NewCmdEdit(&options),
NewCmdExport(&options),
NewCmdFlatten(&options),
NewCmdList(&options),
NewCmdManifest(&options),
NewCmdMutate(&options),
NewCmdOptimize(&options),
NewCmdPull(&options),
NewCmdPush(&options),
NewCmdRebase(&options),
NewCmdTag(&options),
NewCmdValidate(&options),
NewCmdVersion(),
}
root.AddCommand(commands...)
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable debug logs")
root.PersistentFlags().BoolVar(&insecure, "insecure", false, "Allow image references to be fetched without TLS")
root.PersistentFlags().BoolVar(&ndlayers, "allow-nondistributable-artifacts", false, "Allow pushing non-distributable (foreign) layers")
root.PersistentFlags().Var(platform, "platform", "Specifies the platform in the form os/arch[/variant][:osversion] (e.g. linux/amd64).")
return root
}
// headerTransport sets headers on outgoing requests.
type headerTransport struct {
httpHeaders map[string]string
inner http.RoundTripper
}
// RoundTrip implements http.RoundTripper.
func (ht *headerTransport) RoundTrip(in *http.Request) (*http.Response, error) {
for k, v := range ht.httpHeaders {
if http.CanonicalHeaderKey(k) == "User-Agent" {
// Docker sets this, which is annoying, since we're not docker.
// We might want to revisit completely ignoring this.
continue
}
in.Header.Set(k, v)
}
return ht.inner.RoundTrip(in)
}