-
Notifications
You must be signed in to change notification settings - Fork 147
/
start.go
71 lines (57 loc) · 1.77 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package daemon
import (
"os"
"os/exec"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/mutagen-io/mutagen/pkg/daemon"
)
func startMain(command *cobra.Command, arguments []string) error {
// Validate arguments.
if len(arguments) != 0 {
return errors.New("unexpected arguments provided")
}
// If the daemon is registered with the system, it may have a different
// start mechanism, so see if the system should handle it.
if handled, err := daemon.RegisteredStart(); err != nil {
return errors.Wrap(err, "unable to start daemon using system mechanism")
} else if handled {
return nil
}
// Compute the path to the current executable.
executablePath, err := os.Executable()
if err != nil {
return errors.Wrap(err, "unable to determine executable path")
}
// Restart in the background.
daemonProcess := &exec.Cmd{
Path: executablePath,
Args: []string{"mutagen", "daemon", "run"},
SysProcAttr: daemonProcessAttributes,
}
if err := daemonProcess.Start(); err != nil {
return errors.Wrap(err, "unable to fork daemon")
}
// Success.
return nil
}
var startCommand = &cobra.Command{
Use: "start",
Short: "Start the Mutagen daemon if it's not already running",
RunE: startMain,
SilenceUsage: true,
}
var startConfiguration struct {
// help indicates whether or not help information should be shown for the
// command.
help bool
}
func init() {
// Grab a handle for the command line flags.
flags := startCommand.Flags()
// Disable alphabetical sorting of flags in help output.
flags.SortFlags = false
// Manually add a help flag to override the default message. Cobra will
// still implement its logic automatically.
flags.BoolVarP(&startConfiguration.help, "help", "h", false, "Show help information")
}