func (app *App) CreateDesktopShortcut(launcher launchers.Launcher, referenceDescriptor descriptors.AppDescriptor) (err error) { desktopDir, err := caravel.GetUserDesktop() if err != nil { return err } if !caravel.DirectoryExists(desktopDir) { return fmt.Errorf("Expected desktop dir '%v' not found", desktopDir) } shortcutFileName := caravel.FormatFileName(referenceDescriptor.GetName()) + ".desktop" log.Debug("Shortcut file name: '%v'", shortcutFileName) shortcutFilePath := filepath.Join(desktopDir, shortcutFileName) log.Info("Creating desktop shortcut: '%v'...", shortcutFilePath) shortcutFile, err := os.OpenFile(shortcutFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0700) if err != nil { return err } defer func() { shortcutFile.Close() if err != nil { os.Remove(shortcutFilePath) } }() actualIconPath := app.GetActualIconPath(launcher) shortcutContent := fmt.Sprintf(linuxShortcutContent, referenceDescriptor.GetName(), referenceDescriptor.GetDescription(), launcher.GetExecutable(), app.GetLocalDescriptorPath(), actualIconPath) _, err = shortcutFile.Write([]byte(shortcutContent)) if err != nil { return err } log.Notice("Desktop shortcut created") return nil }
func (app *App) CreateDesktopShortcut(launcher launchers.Launcher, referenceDescriptor descriptors.AppDescriptor) (err error) { desktopDir, err := caravel.GetUserDesktop() if err != nil { return err } shortcutName := caravel.FormatFileName(referenceDescriptor.GetName()) + ".lnk" log.Debug("Shortcut file name: '%v'", shortcutName) shortcutFilePath := filepath.Join(desktopDir, shortcutName) log.Debug("Shortcut path: '%v'", shortcutFilePath) log.Info("Creating desktop shortcut: '%v'...", shortcutFilePath) log.Debug("Creating temp file for script...") salt := rand.Int63() tempFileName := fmt.Sprintf("shortcutScript_%v_%v.vbs", time.Now().Unix(), salt) tempFilePath := filepath.Join(os.TempDir(), tempFileName) tempFile, err := os.Create(tempFilePath) if err != nil { return err } defer func() { tempFile.Close() tempRemovalErr := os.Remove(tempFilePath) if tempRemovalErr != nil { log.Warning("Cannot remove the temp script: %v", tempFilePath) } else { log.Debug("Temp script successfully removed") } if err != nil { os.Remove(shortcutFilePath) } }() log.Debug("Temp script file created: %v", tempFilePath) actualIconPath := app.GetActualIconPath(launcher) log.Debug("Actual icon path: '%v'", actualIconPath) workingDirectory := filepath.Dir(app.GetLocalDescriptorPath()) log.Debug("Working directory: '%v'", workingDirectory) shortcutScript := fmt.Sprintf(windowsShortcutContent, shortcutFilePath, app.GetLocalDescriptorPath(), referenceDescriptor.GetDescription(), actualIconPath, workingDirectory) log.Debug("Writing script temp file...") tempFile.Write([]byte(shortcutScript)) tempFile.Close() log.Debug("Temp script ready") log.Debug("Now executing the temp script...") shortcutCreationCommand := exec.Command("wscript", "/b", "/nologo", tempFilePath) err = shortcutCreationCommand.Run() if err != nil { return err } if !shortcutCreationCommand.ProcessState.Success() { return fmt.Errorf("The script did not run successfully") } log.Debug("The script was successful") return nil }