github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/osutils/autostart/autostart_windows.go (about) 1 package autostart 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 8 "github.com/ActiveState/cli/internal/assets" 9 "github.com/ActiveState/cli/internal/constants" 10 "github.com/ActiveState/cli/internal/errs" 11 "github.com/ActiveState/cli/internal/fileutils" 12 "github.com/ActiveState/cli/internal/osutils/shortcut" 13 ) 14 15 var startupPath = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup") 16 17 func enable(exec string, opts Options) error { 18 enabled, err := isEnabled(exec, opts) 19 if err != nil { 20 return errs.Wrap(err, "Could not check if app is enabled") 21 } 22 if enabled { 23 return nil 24 } 25 26 name := formattedName(opts.Name) 27 s := shortcut.New(startupPath, name, exec, opts.Args...) 28 if err := s.Enable(); err != nil { 29 return errs.Wrap(err, "Could not create shortcut") 30 } 31 32 icon, err := assets.ReadFileBytes(opts.IconFileSource) 33 if err != nil { 34 return errs.Wrap(err, "Could not read asset") 35 } 36 37 err = s.SetIconBlob(icon) 38 if err != nil { 39 return errs.Wrap(err, "Could not set icon for shortcut file") 40 } 41 42 err = s.SetWindowStyle(shortcut.Minimized) 43 if err != nil { 44 return errs.Wrap(err, "Could not set shortcut to minimized") 45 } 46 47 return nil 48 } 49 50 func disable(exec string, opts Options) error { 51 enabled, err := isEnabled(exec, opts) 52 if err != nil { 53 return errs.Wrap(err, "Could not check if app autostart is enabled") 54 } 55 56 if !enabled { 57 return nil 58 } 59 return os.Remove(shortcutFilename(opts.Name)) 60 } 61 62 func isEnabled(_ string, opts Options) (bool, error) { 63 return fileutils.FileExists(shortcutFilename(opts.Name)), nil 64 } 65 66 func autostartPath(name string, _ Options) (string, error) { 67 return shortcutFilename(name), nil 68 } 69 70 func upgrade(exec string, opts Options) error { 71 return nil 72 } 73 74 func shortcutFilename(name string) string { 75 name = formattedName(name) 76 if testDir, ok := os.LookupEnv(constants.AutostartPathOverrideEnvVarName); ok { 77 startupPath = testDir 78 } 79 return filepath.Join(startupPath, name+".lnk") 80 } 81 82 func formattedName(name string) string { 83 return strings.ToLower(strings.ReplaceAll(name, " ", "-")) 84 }