github.com/openshift/source-to-image@v1.4.1-0.20240516041539-bf52fc02204e/pkg/cmd/cli/util/util.go (about)

     1  package cmd
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	log "k8s.io/klog/v2"
     9  
    10  	"github.com/spf13/cobra"
    11  	"github.com/spf13/pflag"
    12  
    13  	"github.com/openshift/source-to-image/pkg/api"
    14  )
    15  
    16  // AddCommonFlags adds the common flags for usage, build and rebuild commands
    17  func AddCommonFlags(c *cobra.Command, cfg *api.Config) {
    18  	c.Flags().BoolVarP(&(cfg.Quiet), "quiet", "q", false,
    19  		"Operate quietly. Suppress all non-error output.")
    20  	c.Flags().BoolVar(&(cfg.Incremental), "incremental", false,
    21  		"Perform an incremental build")
    22  	c.Flags().BoolVar(&(cfg.RemovePreviousImage), "rm", false,
    23  		"Remove the previous image during incremental builds")
    24  	c.Flags().StringVar(&(cfg.CallbackURL), "callback-url", "",
    25  		"Specify a URL to invoke via HTTP POST upon build completion")
    26  	c.Flags().VarP(&(cfg.BuilderPullPolicy), "pull-policy", "p",
    27  		"Specify when to pull the builder image (always, never or if-not-present)")
    28  	c.Flags().Var(&(cfg.PreviousImagePullPolicy), "incremental-pull-policy",
    29  		"Specify when to pull the previous image for incremental builds (always, never or if-not-present)")
    30  	c.Flags().Var(&(cfg.RuntimeImagePullPolicy), "runtime-pull-policy",
    31  		"Specify when to pull the runtime image (always, never or if-not-present)")
    32  	c.Flags().BoolVar(&(cfg.PreserveWorkingDir), "save-temp-dir", false,
    33  		"Save the temporary directory used by S2I instead of deleting it")
    34  	c.Flags().StringVarP(&(cfg.DockerCfgPath), "dockercfg-path", "", filepath.Join(os.Getenv("HOME"), ".docker/config.json"),
    35  		"Specify the path to the Docker configuration file")
    36  	c.Flags().StringVarP(&(cfg.Destination), "destination", "d", "",
    37  		"Specify a destination location for untar operation")
    38  }
    39  
    40  // SetupLogger makes --loglevel reflect in klog's -v flag
    41  func SetupLogger(flags *pflag.FlagSet) {
    42  
    43  	from := flag.CommandLine
    44  	if fflag := from.Lookup("v"); fflag != nil {
    45  		level := fflag.Value.(*log.Level)
    46  		loglevelPtr := (*int32)(level)
    47  		flags.Int32Var(loglevelPtr, "loglevel", 0, "Set the level of log output (0-5)")
    48  	}
    49  
    50  	// FIXME glog had only option to redirect output to stderr
    51  	// the preferred for S2I would be to redirect to stdout - klog can support via `klog.SetOutput()`
    52  	flag.CommandLine.Set("logtostderr", "true")
    53  }