-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathautostart_windows.go
More file actions
84 lines (68 loc) · 2.04 KB
/
Copy pathautostart_windows.go
File metadata and controls
84 lines (68 loc) · 2.04 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
package autostart
import (
"os"
"path/filepath"
"strings"
"github.com/ActiveState/cli/internal/assets"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/osutils/shortcut"
)
var startupPath = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup")
func enable(exec string, opts Options) error {
enabled, err := isEnabled(exec, opts)
if err != nil {
return errs.Wrap(err, "Could not check if app is enabled")
}
if enabled {
return nil
}
name := formattedName(opts.Name)
s := shortcut.New(startupPath, name, exec, opts.Args...)
if err := s.Enable(); err != nil {
return errs.Wrap(err, "Could not create shortcut")
}
icon, err := assets.ReadFile(opts.IconFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
err = s.SetIconBlob(icon)
if err != nil {
return errs.Wrap(err, "Could not set icon for shortcut file")
}
err = s.SetWindowStyle(shortcut.Minimized)
if err != nil {
return errs.Wrap(err, "Could not set shortcut to minimized")
}
return nil
}
func disable(exec string, opts Options) error {
enabled, err := isEnabled(exec, opts)
if err != nil {
return errs.Wrap(err, "Could not check if app autostart is enabled")
}
if !enabled {
return nil
}
return os.Remove(shortcutFilename(opts.Name))
}
func isEnabled(_ string, opts Options) (bool, error) {
return fileutils.FileExists(shortcutFilename(opts.Name)), nil
}
func autostartPath(name string, _ Options) (string, error) {
return shortcutFilename(name), nil
}
func upgrade(exec string, opts Options) error {
return nil
}
func shortcutFilename(name string) string {
name = formattedName(name)
if testDir, ok := os.LookupEnv(constants.AutostartPathOverrideEnvVarName); ok {
startupPath = testDir
}
return filepath.Join(startupPath, name+".lnk")
}
func formattedName(name string) string {
return strings.ToLower(strings.ReplaceAll(name, " ", "-"))
}