github.com/mutagen-io/mutagen@v0.18.0-rc1/cmd/mutagen/main.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/fatih/color"
     9  
    10  	"github.com/mutagen-io/mutagen/cmd"
    11  	"github.com/mutagen-io/mutagen/cmd/mutagen/daemon"
    12  	"github.com/mutagen-io/mutagen/cmd/mutagen/forward"
    13  	"github.com/mutagen-io/mutagen/cmd/mutagen/project"
    14  	"github.com/mutagen-io/mutagen/cmd/mutagen/sync"
    15  
    16  	"github.com/mutagen-io/mutagen/pkg/mutagen"
    17  	"github.com/mutagen-io/mutagen/pkg/prompting"
    18  )
    19  
    20  // rootMain is the entry point for the root command.
    21  func rootMain(command *cobra.Command, _ []string) error {
    22  	// If no commands were given, then print help information and bail. We don't
    23  	// have to worry about warning about arguments being present here (which
    24  	// would be incorrect usage) because arguments can't even reach this point
    25  	// (they will be mistaken for subcommands and a error will be displayed).
    26  	command.Help()
    27  
    28  	// Success.
    29  	return nil
    30  }
    31  
    32  // rootCommand is the root command.
    33  var rootCommand = &cobra.Command{
    34  	Use:          "mutagen",
    35  	Version:      mutagen.Version,
    36  	Short:        "Fast file synchronization and network forwarding for remote development",
    37  	RunE:         rootMain,
    38  	SilenceUsage: true,
    39  }
    40  
    41  // rootConfiguration stores configuration for the root command.
    42  var rootConfiguration struct {
    43  	// help indicates whether or not to show help information and exit.
    44  	help bool
    45  }
    46  
    47  func init() {
    48  	// Disable alphabetical sorting of commands in help output. This is a global
    49  	// setting that affects all Cobra command instances.
    50  	cobra.EnableCommandSorting = false
    51  
    52  	// Disable Cobra's use of mousetrap. This breaks daemon registration on
    53  	// Windows because it tries to enforce that the CLI only be launched from
    54  	// a console, which it's not when running automatically.
    55  	cobra.MousetrapHelpText = ""
    56  
    57  	// Set the template used by the version flag.
    58  	rootCommand.SetVersionTemplate("Mutagen version {{ .Version }}\n")
    59  
    60  	// Grab a handle for the command line flags.
    61  	flags := rootCommand.Flags()
    62  
    63  	// Disable alphabetical sorting of flags in help output.
    64  	flags.SortFlags = false
    65  
    66  	// Manually add a help flag to override the default message. Cobra will
    67  	// still implement its logic automatically.
    68  	flags.BoolVarP(&rootConfiguration.help, "help", "h", false, "Show help information")
    69  
    70  	// Hide Cobra's completion command.
    71  	rootCommand.CompletionOptions.HiddenDefaultCmd = true
    72  
    73  	// Register commands.
    74  	rootCommand.AddCommand(
    75  		sync.SyncCommand,
    76  		forward.ForwardCommand,
    77  		project.ProjectCommand,
    78  		daemon.DaemonCommand,
    79  		versionCommand,
    80  		legalCommand,
    81  		generateCommand,
    82  	)
    83  
    84  	// Enable color output support for all commands in the hierarchy.
    85  	rootCommand.SetOut(color.Output)
    86  	rootCommand.SetErr(color.Error)
    87  }
    88  
    89  func main() {
    90  	// Check if a prompting environment is set. If so, treat this as a prompt
    91  	// request. Prompting is sort of a special pseudo-command that's indicated
    92  	// by the presence of an environment variable, and hence it has to be
    93  	// handled in a bit of a special manner.
    94  	if _, ok := os.LookupEnv(prompting.PrompterEnvironmentVariable); ok {
    95  		if err := promptMain(os.Args[1:]); err != nil {
    96  			cmd.Fatal(err)
    97  		}
    98  		return
    99  	}
   100  
   101  	// Handle terminal compatibility issues. If this call returns, it means that
   102  	// we should proceed normally.
   103  	cmd.HandleTerminalCompatibility()
   104  
   105  	// Execute the root command.
   106  	if err := rootCommand.Execute(); err != nil {
   107  		os.Exit(1)
   108  	}
   109  }