New upstream version 0.0~git20181114.c527205
Micah Anderson
7 years ago
| 0 | # Compiled Object files, Static and Dynamic libs (Shared Objects) | |
| 1 | *.o | |
| 2 | *.a | |
| 3 | *.so | |
| 4 | ||
| 5 | # Folders | |
| 6 | _obj | |
| 7 | _test | |
| 8 | ||
| 9 | # Architecture specific extensions/prefixes | |
| 10 | *.[568vq] | |
| 11 | [568vq].out | |
| 12 | ||
| 13 | *.cgo1.go | |
| 14 | *.cgo2.c | |
| 15 | _cgo_defun.c | |
| 16 | _cgo_gotypes.go | |
| 17 | _cgo_export.* | |
| 18 | ||
| 19 | _testmain.go | |
| 20 | ||
| 21 | *.exe | |
| 22 | *.test | |
| 23 | *.prof |
| 0 | The MIT License (MIT) | |
| 1 | ||
| 2 | Copyright (c) 2016 ProtonMail | |
| 3 | ||
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | of this software and associated documentation files (the "Software"), to deal | |
| 6 | in the Software without restriction, including without limitation the rights | |
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | copies of the Software, and to permit persons to whom the Software is | |
| 9 | furnished to do so, subject to the following conditions: | |
| 10 | ||
| 11 | The above copyright notice and this permission notice shall be included in all | |
| 12 | copies or substantial portions of the Software. | |
| 13 | ||
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 20 | SOFTWARE. |
| 0 | # go-autostart | |
| 1 | ||
| 2 | [](https://godoc.org/github.com/ProtonMail/go-autostart) | |
| 3 | ||
| 4 | A Go library to run a command after login. | |
| 5 | ||
| 6 | ## Usage | |
| 7 | ||
| 8 | ```go | |
| 9 | package main | |
| 10 | ||
| 11 | import ( | |
| 12 | "log" | |
| 13 | "github.com/ProtonMail/go-autostart" | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | app := &autostart.App{ | |
| 18 | Name: "test", | |
| 19 | DisplayName: "Just a Test App", | |
| 20 | Exec: []string{"bash", "-c", "echo autostart >> ~/autostart.txt"}, | |
| 21 | } | |
| 22 | ||
| 23 | if app.IsEnabled() { | |
| 24 | log.Println("App is already enabled, removing it...") | |
| 25 | ||
| 26 | if err := app.Disable(); err != nil { | |
| 27 | log.Fatal(err) | |
| 28 | } | |
| 29 | } else { | |
| 30 | log.Println("Enabling app...") | |
| 31 | ||
| 32 | if err := app.Enable(); err != nil { | |
| 33 | log.Fatal(err) | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | log.Println("Done!") | |
| 38 | } | |
| 39 | ``` | |
| 40 | ||
| 41 | ## Behavior | |
| 42 | ||
| 43 | * On Linux and BSD, it creates a `.desktop` file in `$XDG_CONFIG_HOME/autostart` | |
| 44 | (i.e. `$HOME/.config/autostart`). See http://askubuntu.com/questions/48321/how-do-i-start-applications-automatically-on-login | |
| 45 | * On macOS, it creates a `launchd` job. See http://blog.gordn.org/2015/03/implementing-run-on-login-for-your-node.html | |
| 46 | * On Windows, it creates a link to the program in `%USERPROFILE%\Start Menu\Programs\Startup` | |
| 47 | ||
| 48 | ## License | |
| 49 | ||
| 50 | MIT |
| 0 | package autostart | |
| 1 | ||
| 2 | // An application that will be started when the user logs in. | |
| 3 | type App struct { | |
| 4 | // Unique identifier for the app. | |
| 5 | Name string | |
| 6 | // The command to execute, followed by its arguments. | |
| 7 | Exec []string | |
| 8 | // The app name. | |
| 9 | DisplayName string | |
| 10 | // The app icon. | |
| 11 | Icon string | |
| 12 | } |
| 0 | package autostart | |
| 1 | ||
| 2 | import ( | |
| 3 | "os" | |
| 4 | "path/filepath" | |
| 5 | "text/template" | |
| 6 | ) | |
| 7 | ||
| 8 | const jobTemplate = `<?xml version="1.0" encoding="UTF-8"?> | |
| 9 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| 10 | <plist version="1.0"> | |
| 11 | <dict> | |
| 12 | <key>Label</key> | |
| 13 | <string>{{.Name}}</string> | |
| 14 | <key>ProgramArguments</key> | |
| 15 | <array> | |
| 16 | {{range .Exec -}} | |
| 17 | <string>{{.}}</string> | |
| 18 | {{end}} | |
| 19 | </array> | |
| 20 | <key>RunAtLoad</key> | |
| 21 | <true/> | |
| 22 | </dict> | |
| 23 | </plist>` | |
| 24 | ||
| 25 | var launchDir string | |
| 26 | ||
| 27 | func init() { | |
| 28 | launchDir = filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents") | |
| 29 | } | |
| 30 | ||
| 31 | func (a *App) path() string { | |
| 32 | return filepath.Join(launchDir, a.Name+".plist") | |
| 33 | } | |
| 34 | ||
| 35 | // IsEnabled Check is app enabled startup. | |
| 36 | func (a *App) IsEnabled() bool { | |
| 37 | _, err := os.Stat(a.path()) | |
| 38 | return err == nil | |
| 39 | } | |
| 40 | ||
| 41 | // Enable this app on startup. | |
| 42 | func (a *App) Enable() error { | |
| 43 | t := template.Must(template.New("job").Parse(jobTemplate)) | |
| 44 | ||
| 45 | if err := os.MkdirAll(launchDir, 0777); err != nil { | |
| 46 | return err | |
| 47 | } | |
| 48 | f, err := os.Create(a.path()) | |
| 49 | if err != nil { | |
| 50 | return err | |
| 51 | } | |
| 52 | defer f.Close() | |
| 53 | ||
| 54 | if err := t.Execute(f, a); err != nil { | |
| 55 | return err | |
| 56 | } | |
| 57 | ||
| 58 | return nil | |
| 59 | } | |
| 60 | ||
| 61 | // Disable this app on startup. | |
| 62 | func (a *App) Disable() error { | |
| 63 | ||
| 64 | return os.Remove(a.path()) | |
| 65 | } |
| 0 | #include <windows.h> | |
| 1 | #include <stdlib.h> | |
| 2 | #include <stdint.h> | |
| 3 | #include <stdio.h> | |
| 4 | #include <string.h> | |
| 5 | #include <objbase.h> | |
| 6 | #include <shlobj.h> | |
| 7 | ||
| 8 | uint64_t CreateShortcut(char *shortcutA, char *path, char *args) { | |
| 9 | IShellLink* pISL; | |
| 10 | IPersistFile* pIPF; | |
| 11 | HRESULT hr; | |
| 12 | ||
| 13 | CoInitializeEx(NULL, COINIT_MULTITHREADED); | |
| 14 | ||
| 15 | // Shortcut filename: convert ANSI to unicode | |
| 16 | WORD shortcutW[MAX_PATH]; | |
| 17 | int nChar = MultiByteToWideChar(CP_ACP, 0, shortcutA, -1, shortcutW, MAX_PATH); | |
| 18 | ||
| 19 | hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID*)&pISL); | |
| 20 | if (!SUCCEEDED(hr)) { | |
| 21 | return hr+0x01000000; | |
| 22 | } | |
| 23 | ||
| 24 | // See https://msdn.microsoft.com/en-us/library/windows/desktop/bb774950(v=vs.85).aspx | |
| 25 | hr = pISL->lpVtbl->SetPath(pISL, path); | |
| 26 | if (!SUCCEEDED(hr)) { | |
| 27 | return hr+0x02000000; | |
| 28 | } | |
| 29 | ||
| 30 | hr = pISL->lpVtbl->SetArguments(pISL, args); | |
| 31 | if (!SUCCEEDED(hr)) { | |
| 32 | return hr+0x03000000; | |
| 33 | } | |
| 34 | ||
| 35 | // Save the shortcut | |
| 36 | hr = pISL->lpVtbl->QueryInterface(pISL, &IID_IPersistFile, (void**)&pIPF); | |
| 37 | if (!SUCCEEDED(hr)) { | |
| 38 | return hr+0x04000000; | |
| 39 | } | |
| 40 | ||
| 41 | hr = pIPF->lpVtbl->Save(pIPF, shortcutW, FALSE); | |
| 42 | if (!SUCCEEDED(hr)) { | |
| 43 | return hr+0x05000000; | |
| 44 | } | |
| 45 | ||
| 46 | pIPF->lpVtbl->Release(pIPF); | |
| 47 | pISL->lpVtbl->Release(pISL); | |
| 48 | ||
| 49 | return 0x0; | |
| 50 | } |
| 0 | package autostart | |
| 1 | ||
| 2 | // #cgo LDFLAGS: -lole32 -luuid | |
| 3 | /* | |
| 4 | #define WIN32_LEAN_AND_MEAN | |
| 5 | #include <stdint.h> | |
| 6 | #include <windows.h> | |
| 7 | ||
| 8 | uint64_t CreateShortcut(char *shortcutA, char *path, char *args); | |
| 9 | */ | |
| 10 | import "C" | |
| 11 | ||
| 12 | import ( | |
| 13 | "errors" | |
| 14 | "fmt" | |
| 15 | "os" | |
| 16 | "path/filepath" | |
| 17 | "strings" | |
| 18 | ) | |
| 19 | ||
| 20 | var startupDir string | |
| 21 | ||
| 22 | func init() { | |
| 23 | startupDir = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup") | |
| 24 | } | |
| 25 | ||
| 26 | func (a *App) path() string { | |
| 27 | return filepath.Join(startupDir, a.Name+".lnk") | |
| 28 | } | |
| 29 | ||
| 30 | func (a *App) IsEnabled() bool { | |
| 31 | _, err := os.Stat(a.path()) | |
| 32 | return err == nil | |
| 33 | } | |
| 34 | ||
| 35 | func (a *App) Enable() error { | |
| 36 | path := a.Exec[0] | |
| 37 | args := strings.Join(a.Exec[1:], " ") | |
| 38 | ||
| 39 | if err := os.MkdirAll(startupDir, 0777); err != nil { | |
| 40 | return err | |
| 41 | } | |
| 42 | res := C.CreateShortcut(C.CString(a.path()), C.CString(path), C.CString(args)) | |
| 43 | if res != 0 { | |
| 44 | return errors.New(fmt.Sprintf("autostart: cannot create shortcut '%s' error code: 0x%.8x", a.path(), res)) | |
| 45 | } | |
| 46 | return nil | |
| 47 | } | |
| 48 | ||
| 49 | func (a *App) Disable() error { | |
| 50 | return os.Remove(a.path()) | |
| 51 | } |
| 0 | // +build !windows,!darwin | |
| 1 | ||
| 2 | package autostart | |
| 3 | ||
| 4 | import ( | |
| 5 | "os" | |
| 6 | "path/filepath" | |
| 7 | "text/template" | |
| 8 | ) | |
| 9 | ||
| 10 | const desktopTemplate = `[Desktop Entry] | |
| 11 | Type=Application | |
| 12 | Name={{.DisplayName}} | |
| 13 | Exec={{.Exec}} | |
| 14 | {{- if .Icon}} | |
| 15 | Icon={{.Icon}}{{end}} | |
| 16 | X-GNOME-Autostart-enabled=true | |
| 17 | ` | |
| 18 | ||
| 19 | var autostartDir string | |
| 20 | ||
| 21 | func init() { | |
| 22 | if os.Getenv("XDG_CONFIG_HOME") != "" { | |
| 23 | autostartDir = os.Getenv("XDG_CONFIG_HOME") | |
| 24 | } else { | |
| 25 | autostartDir = filepath.Join(os.Getenv("HOME"), ".config") | |
| 26 | } | |
| 27 | autostartDir = filepath.Join(autostartDir, "autostart") | |
| 28 | } | |
| 29 | ||
| 30 | func (a *App) path() string { | |
| 31 | return filepath.Join(autostartDir, a.Name+".desktop") | |
| 32 | } | |
| 33 | ||
| 34 | // Check if the app is enabled on startup. | |
| 35 | func (a *App) IsEnabled() bool { | |
| 36 | _, err := os.Stat(a.path()) | |
| 37 | return err == nil | |
| 38 | } | |
| 39 | ||
| 40 | type app struct { | |
| 41 | *App | |
| 42 | } | |
| 43 | ||
| 44 | // Override App.Exec to return a string. | |
| 45 | func (a *app) Exec() string { | |
| 46 | return quote(a.App.Exec) | |
| 47 | } | |
| 48 | ||
| 49 | // Enable this app on startup. | |
| 50 | func (a *App) Enable() error { | |
| 51 | t := template.Must(template.New("desktop").Parse(desktopTemplate)) | |
| 52 | ||
| 53 | if err := os.MkdirAll(autostartDir, 0777); err != nil { | |
| 54 | return err | |
| 55 | } | |
| 56 | f, err := os.Create(a.path()) | |
| 57 | if err != nil { | |
| 58 | return err | |
| 59 | } | |
| 60 | defer f.Close() | |
| 61 | ||
| 62 | return t.Execute(f, &app{a}) | |
| 63 | } | |
| 64 | ||
| 65 | // Disable this app on startup. | |
| 66 | func (a *App) Disable() error { | |
| 67 | return os.Remove(a.path()) | |
| 68 | } |