Skip to content

Commit

Permalink
Merge pull request #5812 from jwhonce/wip/options
Browse files Browse the repository at this point in the history
Add support for the global flags and config files
  • Loading branch information
openshift-merge-robot authored Apr 14, 2020
2 parents 0d01f09 + d517276 commit f0b6cde
Show file tree
Hide file tree
Showing 22 changed files with 687 additions and 192 deletions.
3 changes: 3 additions & 0 deletions cmd/podmanV2/containers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var (
Args: func(cmd *cobra.Command, args []string) error {
return parse.CheckAllLatestAndCIDFile(cmd, args, true, false)
},
Annotations: map[string]string{
registry.RootRequired: "true",
},
}
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/podmanV2/containers/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
flags.StringArrayVarP(&stopOptions.CIDFiles, "cidfile", "", nil, "Read the container ID from the file")
flags.BoolVarP(&stopOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.UintVarP(&stopTimeout, "time", "t", defaultContainerConfig.Engine.StopTimeout, "Seconds to wait for stop before killing the container")
if registry.EngineOptions.EngineMode == entities.ABIMode {
if registry.PodmanOptions.EngineMode == entities.ABIMode {
_ = flags.MarkHidden("latest")
_ = flags.MarkHidden("cidfile")
_ = flags.MarkHidden("ignore")
Expand Down
2 changes: 1 addition & 1 deletion cmd/podmanV2/containers/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
flags.DurationVarP(&waitOptions.Interval, "interval", "i", time.Duration(250), "Milliseconds to wait before polling for completion")
flags.BoolVarP(&waitOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.StringVar(&waitCondition, "condition", "stopped", "Condition to wait on")
if registry.EngineOptions.EngineMode == entities.ABIMode {
if registry.PodmanOptions.EngineMode == entities.ABIMode {
// TODO: This is the same as V1. We could skip creating the flag altogether in V2...
_ = flags.MarkHidden("latest")
}
Expand Down
33 changes: 4 additions & 29 deletions cmd/podmanV2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"os"
"reflect"
"runtime"
"strings"

_ "github.com/containers/libpod/cmd/podmanV2/containers"
_ "github.com/containers/libpod/cmd/podmanV2/healthcheck"
Expand All @@ -14,36 +12,13 @@ import (
"github.com/containers/libpod/cmd/podmanV2/registry"
_ "github.com/containers/libpod/cmd/podmanV2/system"
_ "github.com/containers/libpod/cmd/podmanV2/volumes"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/storage/pkg/reexec"
"github.com/sirupsen/logrus"
)

func init() {
if err := libpod.SetXdgDirs(); err != nil {
logrus.Errorf(err.Error())
os.Exit(1)
}

switch runtime.GOOS {
case "darwin":
fallthrough
case "windows":
registry.EngineOptions.EngineMode = entities.TunnelMode
case "linux":
registry.EngineOptions.EngineMode = entities.ABIMode
default:
logrus.Errorf("%s is not a supported OS", runtime.GOOS)
os.Exit(1)
}

// TODO: Is there a Cobra way to "peek" at os.Args?
for _, v := range os.Args {
if strings.HasPrefix(v, "--remote") {
registry.EngineOptions.EngineMode = entities.TunnelMode
}
}
// This is the bootstrap configuration, if user gives
// CLI flags parts of this configuration may be overwritten
registry.PodmanOptions = registry.NewPodmanConfig()
}

func main() {
Expand All @@ -53,7 +28,7 @@ func main() {
return
}
for _, c := range registry.Commands {
if Contains(registry.EngineOptions.EngineMode, c.Mode) {
if Contains(registry.PodmanOptions.EngineMode, c.Mode) {
parent := rootCmd
if c.Parent != nil {
parent = c.Parent
Expand Down
59 changes: 59 additions & 0 deletions cmd/podmanV2/registry/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package registry

import (
"os"
"runtime"
"strings"

"github.com/containers/common/pkg/config"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/sirupsen/logrus"
)

const (
RootRequired = "RootRequired"
)

var (
PodmanOptions entities.PodmanConfig
)

// NewPodmanConfig creates a PodmanConfig from the environment
func NewPodmanConfig() entities.PodmanConfig {
if err := libpod.SetXdgDirs(); err != nil {
logrus.Errorf(err.Error())
os.Exit(1)
}

var mode entities.EngineMode
switch runtime.GOOS {
case "darwin":
fallthrough
case "windows":
mode = entities.TunnelMode
case "linux":
mode = entities.ABIMode
default:
logrus.Errorf("%s is not a supported OS", runtime.GOOS)
os.Exit(1)
}

// cobra.Execute() may not be called yet, so we peek at os.Args.
for _, v := range os.Args {
// Prefix checking works because of how default EngineMode's
// have been defined.
if strings.HasPrefix(v, "--remote=") {
mode = entities.TunnelMode
}
}

// FIXME: for rootless, where to get the path
// TODO:
cfg, err := config.NewConfig("")
if err != nil {
logrus.Error("Failed to obtain podman configuration")
os.Exit(1)
}
return entities.PodmanConfig{Config: cfg, EngineMode: mode}
}
38 changes: 16 additions & 22 deletions cmd/podmanV2/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ var (
exitCode = ExecErrorCodeGeneric
imageEngine entities.ImageEngine

Commands []CliCommand
EngineOptions entities.EngineOptions
// Commands holds the cobra.Commands to present to the user, including
// parent if not a child of "root"
Commands []CliCommand
)

func SetExitCode(code int) {
Expand Down Expand Up @@ -83,8 +84,8 @@ func ImageEngine() entities.ImageEngine {
// NewImageEngine is a wrapper for building an ImageEngine to be used for PreRunE functions
func NewImageEngine(cmd *cobra.Command, args []string) (entities.ImageEngine, error) {
if imageEngine == nil {
EngineOptions.FlagSet = cmd.Flags()
engine, err := infra.NewImageEngine(EngineOptions)
PodmanOptions.FlagSet = cmd.Flags()
engine, err := infra.NewImageEngine(PodmanOptions)
if err != nil {
return nil, err
}
Expand All @@ -100,8 +101,8 @@ func ContainerEngine() entities.ContainerEngine {
// NewContainerEngine is a wrapper for building an ContainerEngine to be used for PreRunE functions
func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEngine, error) {
if containerEngine == nil {
EngineOptions.FlagSet = cmd.Flags()
engine, err := infra.NewContainerEngine(EngineOptions)
PodmanOptions.FlagSet = cmd.Flags()
engine, err := infra.NewContainerEngine(PodmanOptions)
if err != nil {
return nil, err
}
Expand All @@ -125,24 +126,17 @@ func IdOrLatestArgs(cmd *cobra.Command, args []string) error {
return nil
}

type podmanContextKey string

var podmanFactsKey = podmanContextKey("engineOptions")

func NewOptions(ctx context.Context, facts *entities.EngineOptions) context.Context {
return context.WithValue(ctx, podmanFactsKey, facts)
}

func Options(cmd *cobra.Command) (*entities.EngineOptions, error) {
if f, ok := cmd.Context().Value(podmanFactsKey).(*entities.EngineOptions); ok {
return f, errors.New("Command Context ")
}
return nil, nil
}

func GetContext() context.Context {
if cliCtx == nil {
cliCtx = context.TODO()
cliCtx = context.Background()
}
return cliCtx
}

type ContextOptionsKey string

const PodmanOptionsKey ContextOptionsKey = "PodmanOptions"

func GetContextWithOptions() context.Context {
return context.WithValue(GetContext(), PodmanOptionsKey, PodmanOptions)
}
2 changes: 1 addition & 1 deletion cmd/podmanV2/registry/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func IsRemote() bool {
return EngineOptions.EngineMode == entities.TunnelMode
return PodmanOptions.EngineMode == entities.TunnelMode
}
Loading

0 comments on commit f0b6cde

Please sign in to comment.