github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/registry/registry.go (about) 1 package registry 2 3 import ( 4 "context" 5 6 "github.com/containers/libpod/pkg/domain/entities" 7 "github.com/containers/libpod/pkg/domain/infra" 8 "github.com/pkg/errors" 9 "github.com/spf13/cobra" 10 ) 11 12 // DefaultAPIAddress is the default address of the REST socket 13 const DefaultAPIAddress = "unix:/run/podman/podman.sock" 14 15 // DefaultVarlinkAddress is the default address of the varlink socket 16 const DefaultVarlinkAddress = "unix:/run/podman/io.podman" 17 18 type CliCommand struct { 19 Mode []entities.EngineMode 20 Command *cobra.Command 21 Parent *cobra.Command 22 } 23 24 const ExecErrorCodeGeneric = 125 25 26 var ( 27 cliCtx context.Context 28 containerEngine entities.ContainerEngine 29 exitCode = ExecErrorCodeGeneric 30 imageEngine entities.ImageEngine 31 32 // Commands holds the cobra.Commands to present to the user, including 33 // parent if not a child of "root" 34 Commands []CliCommand 35 ) 36 37 func SetExitCode(code int) { 38 exitCode = code 39 } 40 41 func GetExitCode() int { 42 return exitCode 43 } 44 45 // HelpTemplate returns the help template for podman commands 46 // This uses the short and long options. 47 // command should not use this. 48 func HelpTemplate() string { 49 return `{{.Short}} 50 51 Description: 52 {{.Long}} 53 54 {{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}` 55 } 56 57 // UsageTemplate returns the usage template for podman commands 58 // This blocks the displaying of the global options. The main podman 59 // command should not use this. 60 func UsageTemplate() string { 61 return `Usage(v2):{{if (and .Runnable (not .HasAvailableSubCommands))}} 62 {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} 63 {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} 64 65 Aliases: 66 {{.NameAndAliases}}{{end}}{{if .HasExample}} 67 68 Examples: 69 {{.Example}}{{end}}{{if .HasAvailableSubCommands}} 70 71 Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}} 72 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} 73 74 Flags: 75 {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} 76 {{end}} 77 ` 78 } 79 80 func ImageEngine() entities.ImageEngine { 81 return imageEngine 82 } 83 84 // NewImageEngine is a wrapper for building an ImageEngine to be used for PreRunE functions 85 func NewImageEngine(cmd *cobra.Command, args []string) (entities.ImageEngine, error) { 86 if imageEngine == nil { 87 PodmanOptions.FlagSet = cmd.Flags() 88 engine, err := infra.NewImageEngine(PodmanOptions) 89 if err != nil { 90 return nil, err 91 } 92 imageEngine = engine 93 } 94 return imageEngine, nil 95 } 96 97 func ContainerEngine() entities.ContainerEngine { 98 return containerEngine 99 } 100 101 // NewContainerEngine is a wrapper for building an ContainerEngine to be used for PreRunE functions 102 func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEngine, error) { 103 if containerEngine == nil { 104 PodmanOptions.FlagSet = cmd.Flags() 105 engine, err := infra.NewContainerEngine(PodmanOptions) 106 if err != nil { 107 return nil, err 108 } 109 containerEngine = engine 110 } 111 return containerEngine, nil 112 } 113 114 func SubCommandExists(cmd *cobra.Command, args []string) error { 115 if len(args) > 0 { 116 return errors.Errorf("unrecognized command `%[1]s %[2]s`\nTry '%[1]s --help' for more information.", cmd.CommandPath(), args[0]) 117 } 118 return errors.Errorf("missing command '%[1]s COMMAND'\nTry '%[1]s --help' for more information.", cmd.CommandPath()) 119 } 120 121 // IdOrLatestArgs used to validate a nameOrId was provided or the "--latest" flag 122 func IdOrLatestArgs(cmd *cobra.Command, args []string) error { 123 if len(args) > 1 || (len(args) == 0 && !cmd.Flag("latest").Changed) { 124 return errors.New(`command requires a name, id or the "--latest" flag`) 125 } 126 return nil 127 } 128 129 func GetContext() context.Context { 130 if cliCtx == nil { 131 cliCtx = context.Background() 132 } 133 return cliCtx 134 } 135 136 type ContextOptionsKey string 137 138 const PodmanOptionsKey ContextOptionsKey = "PodmanOptions" 139 140 func GetContextWithOptions() context.Context { 141 return context.WithValue(GetContext(), PodmanOptionsKey, PodmanOptions) 142 }