github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/daemon/register_windows.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"golang.org/x/sys/windows/registry"
     8  )
     9  
    10  // RegistrationSupported indicates whether or not daemon registration is
    11  // supported on this platform.
    12  const RegistrationSupported = true
    13  
    14  const (
    15  	// rootKey is the registry root for daemon registration.
    16  	rootKey = registry.CURRENT_USER
    17  	// runPath is the path to the registry entries for automatic startup.
    18  	runPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
    19  	// runKeyName is the key used to register Mutagen for automatic startup.
    20  	runKeyName = "Mutagen"
    21  )
    22  
    23  // Register performs automatic daemon startup registration.
    24  func Register() error {
    25  	// Attempt to open the relevant registry path and ensure it's cleaned up
    26  	// when we're done.
    27  	key, err := registry.OpenKey(rootKey, runPath, registry.SET_VALUE)
    28  	if err != nil {
    29  		return fmt.Errorf("unable to open registry path: %w", err)
    30  	}
    31  	defer key.Close()
    32  
    33  	// Compute the path to the current executable.
    34  	executablePath, err := os.Executable()
    35  	if err != nil {
    36  		return fmt.Errorf("unable to determine executable path: %w", err)
    37  	}
    38  
    39  	// Compute the command to start the Mutagen daemon.
    40  	command := fmt.Sprintf("\"%s\" daemon start", executablePath)
    41  
    42  	// Attempt to register the daemon.
    43  	if err := key.SetStringValue(runKeyName, command); err != nil {
    44  		return fmt.Errorf("unable to set registry key: %w", err)
    45  	}
    46  
    47  	// Success.
    48  	return nil
    49  }
    50  
    51  // Unregister performs automatic daemon startup de-registration.
    52  func Unregister() error {
    53  	// Attempt to open the relevant registry path and ensure it's cleaned up
    54  	// when we're done.
    55  	key, err := registry.OpenKey(rootKey, runPath, registry.QUERY_VALUE|registry.SET_VALUE)
    56  	if err != nil {
    57  		return fmt.Errorf("unable to open registry path: %w", err)
    58  	}
    59  	defer key.Close()
    60  
    61  	// Attempt to deregister the daemon.
    62  	if err := key.DeleteValue(runKeyName); err != nil && !os.IsNotExist(err) {
    63  		return fmt.Errorf("unable to remove registry key: %w", err)
    64  	}
    65  
    66  	// Success.
    67  	return nil
    68  }
    69  
    70  // RegisteredStart potentially handles daemon start operations if the daemon is
    71  // registered for automatic start with the system. It returns false if the start
    72  // operation was not handled and should be handled by the normal start command.
    73  func RegisteredStart() (bool, error) {
    74  	return false, nil
    75  }
    76  
    77  // RegisteredStop potentially handles stop start operations if the daemon is
    78  // registered for automatic start with the system. It returns false if the stop
    79  // operation was not handled and should be handled by the normal stop command.
    80  func RegisteredStop() (bool, error) {
    81  	return false, nil
    82  }