github.com/mutagen-io/mutagen@v0.18.0-rc1/cmd/mutagen/daemon/start.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "runtime" 8 9 "github.com/spf13/cobra" 10 11 "github.com/mutagen-io/mutagen/cmd" 12 "github.com/mutagen-io/mutagen/cmd/external" 13 14 "github.com/mutagen-io/mutagen/pkg/daemon" 15 "github.com/mutagen-io/mutagen/pkg/platform" 16 ) 17 18 // startMain is the entry point for the start command. 19 func startMain(_ *cobra.Command, _ []string) error { 20 // If the daemon is registered with the system, it may have a different 21 // start mechanism, so see if the system should handle it. 22 if handled, err := daemon.RegisteredStart(); err != nil { 23 return fmt.Errorf("unable to start daemon using system mechanism: %w", err) 24 } else if handled { 25 return nil 26 } 27 28 // Compute the path to the Mutagen CLI executable. 29 var executablePath string 30 var err error 31 if !external.UsePathBasedLookupForDaemonStart { 32 executablePath, err = os.Executable() 33 } else { 34 executablePath, err = exec.LookPath(platform.ExecutableName("mutagen", runtime.GOOS)) 35 } 36 if err != nil { 37 return fmt.Errorf("unable to determine executable path: %w", err) 38 } 39 40 // Restart in the background. 41 daemonProcess := &exec.Cmd{ 42 Path: executablePath, 43 Args: []string{"mutagen", "daemon", "run"}, 44 SysProcAttr: daemonProcessAttributes, 45 } 46 if err := daemonProcess.Start(); err != nil { 47 return fmt.Errorf("unable to fork daemon: %w", err) 48 } 49 50 // Success. 51 return nil 52 } 53 54 // startCommand is the start command. 55 var startCommand = &cobra.Command{ 56 Use: "start", 57 Short: "Start the Mutagen daemon if it's not already running", 58 Args: cmd.DisallowArguments, 59 RunE: startMain, 60 SilenceUsage: true, 61 } 62 63 // startConfiguration stores configuration for the start command. 64 var startConfiguration struct { 65 // help indicates whether or not to show help information and exit. 66 help bool 67 } 68 69 func init() { 70 // Grab a handle for the command line flags. 71 flags := startCommand.Flags() 72 73 // Disable alphabetical sorting of flags in help output. 74 flags.SortFlags = false 75 76 // Manually add a help flag to override the default message. Cobra will 77 // still implement its logic automatically. 78 flags.BoolVarP(&startConfiguration.help, "help", "h", false, "Show help information") 79 }