github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/root/root.go (about)

     1  package root
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"runtime"
     7  
     8  	"github.com/henvic/wedeploycli/autocomplete"
     9  	"github.com/henvic/wedeploycli/color"
    10  	"github.com/henvic/wedeploycli/command/canceled"
    11  	"github.com/henvic/wedeploycli/command/cmdmanager"
    12  	"github.com/henvic/wedeploycli/command/internal/template"
    13  	"github.com/henvic/wedeploycli/command/internal/we"
    14  	cmdversion "github.com/henvic/wedeploycli/command/version"
    15  	"github.com/henvic/wedeploycli/defaults"
    16  	"github.com/henvic/wedeploycli/envs"
    17  	"github.com/henvic/wedeploycli/isterm"
    18  	"github.com/henvic/wedeploycli/verbose"
    19  	"github.com/henvic/wedeploycli/verbosereq"
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  // Cmd is the main command for the CLI
    24  var Cmd = &cobra.Command{
    25  	Use:               "lcp",
    26  	Short:             "Liferay Cloud Platform CLI Tool",
    27  	Args:              cobra.NoArgs,
    28  	PersistentPreRunE: persistentPreRun,
    29  	RunE:              runE,
    30  	SilenceErrors:     true,
    31  	SilenceUsage:      true,
    32  }
    33  
    34  var (
    35  	longHelp bool
    36  
    37  	deferred bool
    38  	version  bool
    39  )
    40  
    41  // see note on usage of maybeEnableVerboseByEnv
    42  func maybeEnableVerboseByEnv() {
    43  	if unsafe, _ := os.LookupEnv(envs.UnsafeVerbose); unsafe == "true" {
    44  		verbose.Enabled = true
    45  	}
    46  }
    47  
    48  func init() {
    49  	template.Configure(Cmd)
    50  	cobra.EnableCommandSorting = false
    51  	cobra.MousetrapHelpText = defaults.MousetrapHelpText
    52  	autocomplete.RootCmd = Cmd
    53  
    54  	Cmd.PersistentFlags().BoolVarP(
    55  		&verbose.Enabled,
    56  		"verbose",
    57  		"v",
    58  		false,
    59  		"Show more information about an operation")
    60  
    61  	// this has to run after defining the --verbose flag above
    62  	maybeEnableVerboseByEnv()
    63  
    64  	Cmd.PersistentFlags().BoolVarP(
    65  		&longHelp,
    66  		"long-help",
    67  		"H",
    68  		false,
    69  		"Show help message (hidden commands and flags included)")
    70  
    71  	Cmd.PersistentFlags().BoolVarP(
    72  		&deferred,
    73  		"defer-verbose",
    74  		"V",
    75  		false,
    76  		"Defer verbose output")
    77  
    78  	Cmd.PersistentFlags().BoolVar(
    79  		&verbosereq.Disabled,
    80  		"no-verbose-requests",
    81  		false,
    82  		"Hide verbose output for requests")
    83  
    84  	Cmd.PersistentFlags().BoolVar(
    85  		&color.NoColorFlag,
    86  		"no-color",
    87  		false,
    88  		"Disable color output")
    89  
    90  	Cmd.PersistentFlags().BoolVar(
    91  		&isterm.NoTTY,
    92  		"no-tty",
    93  		false,
    94  		"Run without terminal support")
    95  
    96  	Cmd.Flags().BoolVar(
    97  		&version,
    98  		"version", false, "Print version information and quit")
    99  
   100  	cmdmanager.HideFlag("version", Cmd)
   101  	hideHelpFlag()
   102  	cmdmanager.HidePersistentFlag("long-help", Cmd)
   103  	cmdmanager.HidePersistentFlag("defer-verbose", Cmd)
   104  	cmdmanager.HidePersistentFlag("no-verbose-requests", Cmd)
   105  	cmdmanager.HidePersistentFlag("no-color", Cmd)
   106  	cmdmanager.HidePersistentFlag("no-tty", Cmd)
   107  
   108  	for _, c := range commands {
   109  		Cmd.AddCommand(c)
   110  	}
   111  }
   112  
   113  func hideHelpFlag() {
   114  	// hide the --help flag on all commands, but top-level
   115  	Cmd.PersistentFlags().BoolP("help", "h", false, "Show help message")
   116  	_ = Cmd.PersistentFlags().MarkHidden("help")
   117  	Cmd.Flags().BoolP("help", "h", false, "Show help message")
   118  }
   119  
   120  func checkCompatibility() error {
   121  	// Heuristics to identify Windows Subsystem for Linux
   122  	// and block it from being used from inside a Linux space working directory
   123  	// due to the subsystem incompatibility
   124  	if runtime.GOOS != "windows" {
   125  		return nil
   126  	}
   127  
   128  	if dir, _ := os.Getwd(); dir != `C:\WINDOWS\system32` {
   129  		return nil
   130  	}
   131  
   132  	return errors.New(`cowardly refusing to use "lcp.exe" Windows binary on a Linux working directory.
   133  Windows Subsystem for Linux has no support for accessing Linux filesystem from a Windows application.
   134  Please install the Linux version of this application with:
   135  curl https://cdn.liferay.cloud/cli/latest/lcp.sh -fsSL | bash`)
   136  }
   137  
   138  func checkLongHelp(cmd *cobra.Command) error {
   139  	if cmd.Flag("long-help").Value.String() != "true" {
   140  		return nil
   141  	}
   142  
   143  	if err := cmd.Flag("help").Value.Set("true"); err != nil {
   144  		panic(err)
   145  	}
   146  
   147  	if err := cmd.Help(); err != nil {
   148  		return err
   149  	}
   150  
   151  	return canceled.Skip()
   152  }
   153  
   154  func persistentPreRun(cmd *cobra.Command, args []string) error {
   155  	// it's problematic to use checkLongHelp here, see https://github.com/wedeploy/cli/issues/418
   156  	if err := checkLongHelp(cmd); err != nil {
   157  		return err
   158  	}
   159  
   160  	if deferred {
   161  		verbose.Enabled = true
   162  		verbose.Deferred = true
   163  	}
   164  
   165  	if err := checkCompatibility(); err != nil {
   166  		return err
   167  	}
   168  
   169  	// load default cloud remote on config context
   170  	var wectx = we.Context()
   171  	return wectx.SetEndpoint(defaults.CloudRemote)
   172  }
   173  
   174  func runE(cmd *cobra.Command, args []string) error {
   175  	if version {
   176  		cmdversion.Print()
   177  		return nil
   178  	}
   179  
   180  	return cmd.Help()
   181  }