github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/osutils/autostart/autostart_darwin.go (about)

     1  package autostart
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/ActiveState/cli/internal/assets"
     9  	"github.com/ActiveState/cli/internal/errs"
    10  	"github.com/ActiveState/cli/internal/fileutils"
    11  	"github.com/ActiveState/cli/internal/logging"
    12  	"github.com/ActiveState/cli/internal/osutils/user"
    13  	"github.com/ActiveState/cli/internal/strutils"
    14  )
    15  
    16  const (
    17  	launchFileFormatName = "com.activestate.platform.%s.plist"
    18  	autostartFileSource  = "com.activestate.platform.autostart.plist.tpl"
    19  )
    20  
    21  func enable(exec string, opts Options) error {
    22  	enabled, err := isEnabled(exec, opts)
    23  	if err != nil {
    24  		return errs.Wrap(err, "Could not check if app autostart is enabled")
    25  	}
    26  
    27  	if enabled {
    28  		return nil
    29  	}
    30  
    31  	path, err := autostartPath(exec, opts)
    32  	if err != nil {
    33  		return errs.Wrap(err, "Could not get launch file")
    34  	}
    35  
    36  	asset, err := assets.ReadFileBytes(autostartFileSource)
    37  	if err != nil {
    38  		return errs.Wrap(err, "Could not read asset")
    39  	}
    40  
    41  	content, err := strutils.ParseTemplate(
    42  		string(asset),
    43  		map[string]interface{}{
    44  			"Label":       opts.MacLabel,
    45  			"Exec":        exec,
    46  			"Interactive": opts.MacInteractive,
    47  		}, nil)
    48  	if err != nil {
    49  		return errs.Wrap(err, "Could not parse %s", fmt.Sprintf(launchFileFormatName, filepath.Base(exec)))
    50  	}
    51  
    52  	if err = fileutils.WriteFile(path, []byte(content)); err != nil {
    53  		return errs.Wrap(err, "Could not write launch file")
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func disable(exec string, opts Options) error {
    60  	enabled, err := isEnabled(exec, opts)
    61  	if err != nil {
    62  		return errs.Wrap(err, "Could not check if app autostart is enabled")
    63  	}
    64  
    65  	if !enabled {
    66  		logging.Debug("Autostart is already disabled for %s", opts.Name)
    67  		return nil
    68  	}
    69  
    70  	path, err := autostartPath(exec, opts)
    71  	if err != nil {
    72  		return errs.Wrap(err, "Could not get launch file")
    73  	}
    74  
    75  	return os.Remove(path)
    76  }
    77  
    78  func isEnabled(exec string, opts Options) (bool, error) {
    79  	path, err := autostartPath(exec, opts)
    80  	if err != nil {
    81  		return false, errs.Wrap(err, "Could not get launch file")
    82  	}
    83  
    84  	return fileutils.FileExists(path), nil
    85  }
    86  
    87  func autostartPath(_ string, opts Options) (string, error) {
    88  	dir, err := user.HomeDir()
    89  	if err != nil {
    90  		return "", errs.Wrap(err, "Could not get home directory")
    91  	}
    92  	path := filepath.Join(dir, "Library/LaunchAgents", fmt.Sprintf(launchFileFormatName, opts.LaunchFileName))
    93  	return path, nil
    94  }
    95  
    96  func upgrade(exec string, opts Options) error {
    97  	path, err := autostartPath(exec, opts)
    98  	if err != nil {
    99  		return errs.Wrap(err, "Could not get launch file")
   100  	}
   101  
   102  	legacy, err := isLegacyPlist(path)
   103  	if err != nil {
   104  		return errs.Wrap(err, "Could not check if legacy plist")
   105  	}
   106  
   107  	if !legacy {
   108  		return nil
   109  	}
   110  
   111  	logging.Debug("Legacy autostart file found, removing: %s", path)
   112  	return os.Remove(path)
   113  }