forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (127 loc) · 4.12 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/genuinetools/img/internal/binutils"
_ "github.com/genuinetools/img/internal/unshare"
"github.com/genuinetools/img/types"
"github.com/genuinetools/img/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
defaultBackend = types.AutoBackend
defaultDockerRegistry = "https://index.docker.io/v1/"
defaultDockerfileName = "Dockerfile"
)
var (
backend string
stateDir string
debug bool
validBackends = []string{types.AutoBackend, types.NativeBackend, types.OverlayFSBackend}
)
const rootHelpTemplate = `{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
const rootUsageTemplate = `{{.Name}} - {{.Short}}
Usage: {{if .Runnable}}{{.UseLine}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableSubCommands}}
Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
func main() {
var printVersionAndExit bool
cmd := &cobra.Command{
Use: "img [OPTIONS] COMMAND [ARG...]",
Short: "Standalone, daemon-less, unprivileged Dockerfile and OCI compatible container image builder",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return cmd.Help()
}
return fmt.Errorf("img: '%s' is not an img command.\nSee 'img --help'", args[0])
},
Version: fmt.Sprintf("%s, build %s", version.VERSION, version.GITCOMMIT),
DisableFlagsInUseLine: true,
}
cmd.SetHelpTemplate(rootHelpTemplate)
cmd.SetUsageTemplate(rootUsageTemplate)
cmd.AddCommand(
newBuildCommand(),
newDiskUsageCommand(),
newListCommand(),
newLoginCommand(),
newLogoutCommand(),
newPruneCommand(),
newPullCommand(),
newPushCommand(),
newRemoveCommand(),
newSaveCommand(),
newTagCommand(),
newUnpackCommand(),
newVersionCommand(),
)
defaultStateDir := defaultStateDirectory()
// Version flag
cmd.Flags().BoolVarP(&printVersionAndExit, "version", "v", false, "Print version information and quit")
// Setup the global flags.
flags := cmd.PersistentFlags()
flags.BoolVarP(&debug, "debug", "d", false, "enable debug logging")
flags.StringVarP(&backend, "backend", "b", defaultBackend, fmt.Sprintf("backend for snapshots (%v)", validBackends))
flags.StringVarP(&stateDir, "state", "s", defaultStateDir, fmt.Sprintf("directory to hold the global state"))
// Set the before function.
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if printVersionAndExit {
fmt.Printf("img %s, build %s", version.VERSION, version.GITCOMMIT)
os.Exit(0)
}
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
// Make sure we have a valid backend.
found := false
for _, vb := range validBackends {
if vb == backend {
found = true
break
}
}
if !found {
return fmt.Errorf("%s is not a valid snapshots backend", backend)
}
// check that runc is available
b := binutils.BinaryAvailabilityCheck{
StateDir: stateDir,
DisableEmbeddedRunc: len(os.Getenv("IMG_DISABLE_EMBEDDED_RUNC")) > 0,
}
err := b.EnsureRuncIsAvailable()
if err != nil {
return err
}
return nil
}
// Run our program.
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func defaultStateDirectory() string {
// pam_systemd sets XDG_RUNTIME_DIR but not other dirs.
xdgDataHome := os.Getenv("XDG_DATA_HOME")
if xdgDataHome != "" {
dirs := strings.Split(xdgDataHome, ":")
return filepath.Join(dirs[0], "img")
}
home := os.Getenv("HOME")
if home != "" {
return filepath.Join(home, ".local", "share", "img")
}
return "/tmp/img"
}