-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcmd.go
More file actions
130 lines (116 loc) · 4.14 KB
/
cmd.go
File metadata and controls
130 lines (116 loc) · 4.14 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/registry"
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/util"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
var (
cacheDir string
// Config is the global tool configuration
Config = GlobalConfig{}
)
// GlobalConfig is the global tool configuration
type GlobalConfig struct {
Pkg PkgConfig `yaml:"pkg"`
}
// PkgConfig is the config specific to the `pkg` subcommand
type PkgConfig struct {
}
func readConfig() {
cfgPath := filepath.Join(os.Getenv("HOME"), ".moby", "linuxkit", "config.yml")
cfgBytes, err := os.ReadFile(cfgPath)
if err != nil {
if os.IsNotExist(err) {
return
}
fmt.Printf("Failed to read %q\n", cfgPath)
os.Exit(1)
}
if err := yaml.Unmarshal(cfgBytes, &Config); err != nil {
fmt.Printf("Failed to parse %q\n", cfgPath)
os.Exit(1)
}
}
func newCmd() *cobra.Command {
var (
flagQuiet bool
flagVerbose int
flagVerboseName = "verbose"
mirrorsRaw []string
certFiles []string
)
cmd := &cobra.Command{
Use: "linuxkit",
DisableAutoGenTag: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
readConfig()
// prepend mirrors from env var so CLI flags take precedence (last SetProxy call wins)
if envMirrors := os.Getenv(envVarMirror); envMirrors != "" {
envList := strings.FieldsFunc(envMirrors, func(r rune) bool { return r == ',' || r == ' ' })
mirrorsRaw = append(envList, mirrorsRaw...)
}
// convert the provided mirrors to a map
for _, m := range mirrorsRaw {
if m == "" {
continue
}
parts := strings.SplitN(m, "=", 2)
// if no equals sign, use the whole string as the mirror for all registries
// not otherwise specified
var key, value string
if len(parts) == 1 {
key = "*"
value = parts[0]
} else {
key = parts[0]
value = parts[1]
}
// value must start with http:// or https://
if !strings.HasPrefix(value, "http://") && !strings.HasPrefix(value, "https://") {
return fmt.Errorf("mirror %q must start with http:// or https://", value)
}
// special logic for docker.io because of its odd references
if key == "docker.io" || key == "docker.io/" {
for _, prefix := range []string{"docker.io", "index.docker.io", "registry-1.docker.io"} {
registry.SetProxy(prefix, value)
}
} else {
registry.SetProxy(key, value)
}
}
for _, f := range certFiles {
if f == "" {
continue
}
cert, err := os.ReadFile(f)
if err != nil {
return fmt.Errorf("failed to read certificate file %q: %w", f, err)
}
// Add the certificate file to the registry
registry.AddCert(cert)
}
// Set up logging
return util.SetupLogging(flagQuiet, flagVerbose, cmd.Flag(flagVerboseName).Changed)
},
}
cmd.AddCommand(buildCmd()) // apko login
cmd.AddCommand(cacheCmd())
cmd.AddCommand(metadataCmd())
cmd.AddCommand(pkgCmd())
cmd.AddCommand(pushCmd())
cmd.AddCommand(runCmd())
cmd.AddCommand(serveCmd())
cmd.AddCommand(versionCmd())
cmd.PersistentFlags().StringVar(&cacheDir, "cache", defaultLinuxkitCache(), fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
cmd.PersistentFlags().StringArrayVar(&mirrorsRaw, "mirror", nil, fmt.Sprintf("Mirror to use for pulling images, format is <registry>=<mirror>, e.g. docker.io=http://mymirror.io, or just http://mymirror.io for all not otherwise specified; must include protocol. Can be provided multiple times. Also read from env var %s (space/comma-separated list); CLI flags take precedence.", envVarMirror))
cmd.PersistentFlags().StringArrayVar(&certFiles, "cert-file", nil, "Path to certificate files to use for pulling images, can be provided multiple times. Will augment system-provided certs.")
cmd.PersistentFlags().BoolVarP(&flagQuiet, "quiet", "q", false, "Quiet execution")
cmd.PersistentFlags().IntVarP(&flagVerbose, flagVerboseName, "v", 1, "Verbosity of logging: 0 = quiet, 1 = info, 2 = debug, 3 = trace. Default is info. Setting it explicitly will create structured logging lines.")
return cmd
}