mirror of
https://github.com/labring/sealos.git
synced 2026-09-24 15:46:19 +08:00
fix: only set default --platform flag when it's not changed (#2276)
Signed-off-by: fengxsong <fengxsong@outlook.com> Signed-off-by: fengxsong <fengxsong@outlook.com>
This commit is contained in:
@@ -21,10 +21,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/containers/buildah/define"
|
||||
|
||||
"github.com/containers/buildah/imagebuildah"
|
||||
buildahcli "github.com/containers/buildah/pkg/cli"
|
||||
"github.com/containers/buildah/pkg/parse"
|
||||
"github.com/containers/buildah/util"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -79,10 +77,6 @@ func newBuildCommand() *cobra.Command {
|
||||
fromAndBudFlags, err := buildahcli.GetFromAndBudFlags(&fromAndBudResults, &userNSResults, &namespaceResults)
|
||||
bailOnError(err, "failed to setup From and Build flags")
|
||||
|
||||
// set as default, otherwise parse.PlatformsFromOptions will get empty list
|
||||
err = fromAndBudFlags.Set("platform", parse.DefaultPlatform())
|
||||
bailOnError(err, "failed to set default platform flag")
|
||||
|
||||
sopts.RegisterFlags(flags)
|
||||
flags.AddFlagSet(&buildFlags)
|
||||
flags.AddFlagSet(&layerFlags)
|
||||
@@ -130,9 +124,10 @@ func buildCmd(c *cobra.Command, inputArgs []string, sopts saveOptions, iopts bui
|
||||
return fmt.Errorf("cannot find any of %v in context directory", strings.Join(defaultFileNames, ", "))
|
||||
}
|
||||
}
|
||||
if err := setDefaultFlags(c); err != nil {
|
||||
if err := setDefaultFlagsWithSetters(c, setDefaultPlatformFlag, setDefaultTLSVerifyFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
options, containerfiles, removeAll, err := buildahcli.GenBuildOptions(c, inputArgs, iopts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+18
-8
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/containers/buildah"
|
||||
"github.com/containers/buildah/pkg/parse"
|
||||
"github.com/containers/common/pkg/umask"
|
||||
is "github.com/containers/image/v5/storage"
|
||||
"github.com/containers/image/v5/types"
|
||||
@@ -51,11 +52,12 @@ func flagChanged(c *cobra.Command, name string) bool {
|
||||
}
|
||||
|
||||
func setDefaultFlags(c *cobra.Command) error {
|
||||
defaulters := []func(*cobra.Command) error{
|
||||
setDefaultTLSVerifyFlag,
|
||||
}
|
||||
for i := range defaulters {
|
||||
if err := defaulters[i](c); err != nil {
|
||||
return setDefaultFlagsWithSetters(c, setDefaultTLSVerifyFlag)
|
||||
}
|
||||
|
||||
func setDefaultFlagsWithSetters(c *cobra.Command, setters ...func(*cobra.Command) error) error {
|
||||
for i := range setters {
|
||||
if err := setters[i](c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -63,9 +65,17 @@ func setDefaultFlags(c *cobra.Command) error {
|
||||
}
|
||||
|
||||
func setDefaultTLSVerifyFlag(c *cobra.Command) error {
|
||||
if fs := c.Flag("tls-verify"); fs != nil && !fs.Changed {
|
||||
if err := c.Flags().Set("tls-verify", "false"); err != nil {
|
||||
return fmt.Errorf("failed to set --tls-verify default to false: %v", err)
|
||||
return setDefaultFlagIfNotChanged(c, "tls-verify", "false")
|
||||
}
|
||||
|
||||
func setDefaultPlatformFlag(c *cobra.Command) error {
|
||||
return setDefaultFlagIfNotChanged(c, "platform", parse.DefaultPlatform())
|
||||
}
|
||||
|
||||
func setDefaultFlagIfNotChanged(c *cobra.Command, k, v string) error {
|
||||
if fs := c.Flag(k); fs != nil && !fs.Changed {
|
||||
if err := c.Flags().Set(k, v); err != nil {
|
||||
return fmt.Errorf("failed to set --%s default to %s: %v", k, v, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -43,12 +43,15 @@ func newCreateCmd() *cobra.Command {
|
||||
if !IsRootless() {
|
||||
return nil
|
||||
}
|
||||
shell, shellSet := os.LookupEnv("SHELL")
|
||||
if !shellSet {
|
||||
logger.Error("no command specified and no `SHELL` env set")
|
||||
os.Exit(1)
|
||||
args = args[1:]
|
||||
if len(args) < 1 {
|
||||
shell, shellSet := os.LookupEnv("SHELL")
|
||||
if !shellSet {
|
||||
logger.Error("no command specified and no `SHELL` env set")
|
||||
os.Exit(1)
|
||||
}
|
||||
args = []string{shell}
|
||||
}
|
||||
args = []string{shell}
|
||||
// nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Env = unshare.RootlessEnv()
|
||||
|
||||
+1
-1
@@ -226,7 +226,7 @@ func fromCmd(c *cobra.Command, args []string, iopts *fromReply) error {
|
||||
if len(args) > 1 {
|
||||
return errors.New("too many arguments specified")
|
||||
}
|
||||
if err := setDefaultFlags(c); err != nil {
|
||||
if err := setDefaultFlagsWithSetters(c, setDefaultPlatformFlag, setDefaultTLSVerifyFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := auth.CheckAuthFile(iopts.authfile); err != nil {
|
||||
|
||||
+1
-1
@@ -131,7 +131,7 @@ func pullCmd(c *cobra.Command, args []string, iopts *pullOptions) error {
|
||||
if len(args) > 1 {
|
||||
return errors.New("too many arguments specified")
|
||||
}
|
||||
if err := setDefaultFlags(c); err != nil {
|
||||
if err := setDefaultFlagsWithSetters(c, setDefaultPlatformFlag, setDefaultTLSVerifyFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
systemContext, err := parse.SystemContextFromOptions(c)
|
||||
|
||||
Reference in New Issue
Block a user