func (app *App) installPackage( packageName string, settings config.Settings, progressCallback caravel.RetrievalProgressCallback) (err error) { remoteDescriptor := app.GetRemoteDescriptor() packageURL, err := remoteDescriptor.GetRemoteFileURL(packageName) if err != nil { return err } log.Debug("Creating package temp file...") packageTempFile, err := ioutil.TempFile(os.TempDir(), packageName) if err != nil { return err } packageTempFilePath := packageTempFile.Name() log.Debug("Package temp file created '%v'", packageTempFilePath) defer func() { packageTempFile.Close() log.Debug("Deleting package temp file: '%v'", packageTempFilePath) tempFileRemovalErr := os.Remove(packageTempFilePath) if tempFileRemovalErr != nil { log.Warning("Could not remove the package temp file! '%v'", tempFileRemovalErr) } else { log.Notice("Package temp file removed") } }() log.Info("Retrieving package: %v", packageURL) err = caravel.RetrieveChunksFromURL(packageURL, packageTempFile, settings.GetBufferSize(), progressCallback) if err != nil { return err } log.Notice("Package retrieved") log.Debug("Closing the package temp file...") packageTempFile.Close() if err != nil { return err } log.Notice("Package temp file closed") err = os.MkdirAll(app.filesDirectory, 0700) if err != nil { return err } log.Info("Extracting the package. Skipping levels: %v...", remoteDescriptor.GetSkipPackageLevels()) err = caravel.ExtractZipSkipLevels(packageTempFilePath, app.filesDirectory, remoteDescriptor.GetSkipPackageLevels()) if err != nil { return err } log.Notice("Package extracted") return nil }
func backgroundOrchestrator(launcher launchers.Launcher, bootDescriptorPath string, guiOutcomeChannel chan guiOutcomeStruct) { outcome := runEngineWithGtk(launcher, bootDescriptorPath) userInterface := outcome.userInterface err := outcome.err log.SetCallback(func(level logging.Level, message string) {}) log.Debug("Result returned by the background routine. Is UI available? %v", userInterface != nil) if err != nil { log.Warning("Err is: %v", err) if userInterface != nil { switch err.(type) { case *engine.ExecutionCanceled: break default: userInterface.ShowError(err.Error()) } } } log.Debug("Now programmatically quitting GTK") gtk.MainQuit() guiOutcomeChannel <- outcome }
func runEngineWithGtk(launcher launchers.Launcher, bootDescriptorPath string) guiOutcomeStruct { log.Debug("Creating the GTK+ user interface...") userInterface, err := gtkui.NewGtkUserInterface(launcher) if err != nil { return guiOutcomeStruct{ userInterface: nil, err: err, } } log.Debug("User interface created") //---------------------------------------------------------------------------- log.Info("Opening boot descriptor: '%v'...", bootDescriptorPath) bootDescriptor, err := descriptors.NewAppDescriptorFromPath(bootDescriptorPath) if err != nil { return guiOutcomeStruct{ userInterface: userInterface, err: err, } } log.Notice("Boot descriptor ready") //---------------------------------------------------------------------------- log.Debug("Starting the launch process...") err = engine.Run(launcher, userInterface, bootDescriptor) return guiOutcomeStruct{ userInterface: userInterface, err: err, } }
func NewTerminalUserInterface(launcher launchers.Launcher, terminal terminals.Terminal) *TerminalUserInterface { log.Debug("Terminal rows: %v", terminal.GetRows()) log.Debug("Terminal columns: %v", terminal.GetColumns()) return &TerminalUserInterface{ launcher: launcher, terminal: terminal, } }
func lookForActualURLInCache(descriptor AppDescriptor) *url.URL { log.Debug("Checking if the Base URL is a key of the Actual Base URL cache...") cachedActualURL := actualBaseURLCache[descriptor.GetDeclaredBaseURL().String()] if cachedActualURL != nil { log.Debug("Actual URL found in the cache! '%v'", cachedActualURL) return cachedActualURL } log.Debug("Actual URL not in the cache") return nil }
func initializeLogging(settings config.Settings) { log.SetLevel(settings.GetLoggingLevel()) logsDirectory := settings.GetLogsDirectory() log.Debug("Logs directory is: '%v'", logsDirectory) tryToRemoveLogs(logsDirectory, settings) ensureLogsDirectory(logsDirectory) logFile := createLogFile(logsDirectory) log.Debug("Now redirecting log lines to file: '%v'", logFile.Name()) log.Setup(logFile) }
func getRawMoonSettings() (rawMoonSettings *rawMoonSettingsStruct) { rawMoonSettings = &rawMoonSettingsStruct{ BackgroundColor: -1, ForegroundColor: -1, LogMaxAgeInHours: defaultLogMaxAgeInHours, } userDir, err := caravel.GetUserDirectory() if err != nil { return rawMoonSettings } userSettingsPath := filepath.Join(userDir, userSettingsFileName) if !caravel.FileExists(userSettingsPath) { return rawMoonSettings } rawSettingsBytes, err := ioutil.ReadFile(userSettingsPath) if err != nil { return rawMoonSettings } err = json.Unmarshal(rawSettingsBytes, rawMoonSettings) if err != nil { return &rawMoonSettingsStruct{} } log.Debug("Settings file content: %#v", rawMoonSettings) return rawMoonSettings }
func (app *App) GetLocalDescriptor() (localDescriptor descriptors.AppDescriptor) { if app.localDescriptorCached { return app.localDescriptor } app.localDescriptorCached = true if !caravel.FileExists(app.localDescriptorPath) { log.Notice("The local descriptor is missing") return nil } log.Notice("The local descriptor has been found! Opening it...") localDescriptor, err := descriptors.NewAppDescriptorFromPath(app.localDescriptorPath) if err != nil { log.Warning(err.Error()) return nil } log.Notice("Local descriptor ready") log.Debug("The local descriptor is: %#v", localDescriptor) app.localDescriptor = localDescriptor return localDescriptor }
func main() { launcher := getMoonLauncher() fmt.Println(launcher.GetTitle()) if len(os.Args) < 2 { exitWithUsage() } settings := launcher.GetSettings() initializeLogging(settings) log.Debug("Launcher is: %#v", launcher) command := os.Args[1] err := executeCommand(launcher, command) switch err.(type) { case nil: os.Exit(v3.ExitCodeSuccess) case *engine.ExecutionCanceled: exitWithCancel() case *verbs.InvalidCommandLineArguments: exitWithUsage() default: exitWithError(err) } }
func (app *App) GetRemoteDescriptor() (remoteDescriptor descriptors.AppDescriptor) { if app.remoteDescriptorCached { return app.remoteDescriptor } app.remoteDescriptorCached = true bootDescriptor := app.bootDescriptor localDescriptor := app.GetLocalDescriptor() var remoteDescriptorURL *url.URL var err error if localDescriptor != nil { if localDescriptor.IsSkipUpdateCheck() { log.Notice("Skipping update check, as requested by the local descriptor") return nil } remoteDescriptorURL, err = localDescriptor.GetRemoteFileURL(localDescriptor.GetDescriptorFileName()) } else { remoteDescriptorURL, err = bootDescriptor.GetRemoteFileURL(bootDescriptor.GetDescriptorFileName()) } if err != nil { log.Warning(err.Error()) return nil } log.Notice("The remote descriptor's URL is: %v", remoteDescriptorURL) log.Info("Retrieving the remote descriptor...") remoteDescriptorBytes, err := caravel.RetrieveFromURL(remoteDescriptorURL) if err != nil { log.Warning(err.Error()) return nil } log.Notice("Remote descriptor retrieved") log.Info("Opening the remote descriptor...") remoteDescriptor, err = descriptors.NewAppDescriptorFromBytes(remoteDescriptorBytes) if err != nil { log.Warning(err.Error()) return nil } log.Notice("Remote descriptor ready") log.Debug("The remote descriptor is: %#v", remoteDescriptor) app.remoteDescriptor = remoteDescriptor return remoteDescriptor }
func lookForActualURLOnGitHub(descriptor AppDescriptor) *url.URL { log.Debug("Checking if the Declared Base URL points to the 'latest' release of a GitHub repo...") gitHubDescriptorInfo := gitHubUtils.GetGitHubDescriptorInfo( descriptor.GetDeclaredBaseURL(), descriptor.GetDescriptorFileName()) if gitHubDescriptorInfo != nil { log.Debug("The given base URL actually references version '%v', whose descriptor is at URL: '%v'", gitHubDescriptorInfo.Version, gitHubDescriptorInfo.DescriptorURL) parentDirURL, err := url.Parse(".") if err != nil { panic(err) } actualBaseURL := gitHubDescriptorInfo.DescriptorURL.ResolveReference(parentDirURL) log.Debug("The actual base URL returned by the GitHub API is: '%v'", actualBaseURL) return actualBaseURL } return nil }
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 } if !caravel.DirectoryExists(desktopDir) { return fmt.Errorf("Expected desktop dir '%v' not found", desktopDir) } scriptFileName := caravel.FormatFileName(referenceDescriptor.GetName()) log.Debug("Bash shortcut name: '%v'", scriptFileName) scriptFilePath := filepath.Join(desktopDir, scriptFileName) log.Info("Creating Bash shortcut: '%v'...", scriptFilePath) scriptFile, err := os.OpenFile(scriptFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0700) if err != nil { return err } defer func() { scriptFile.Close() if err != nil { os.Remove(scriptFilePath) } }() scriptContent := fmt.Sprintf(macScriptContentFormat, launcher.GetExecutable(), app.localDescriptorPath) _, err = scriptFile.Write([]byte(scriptContent)) if err != nil { return err } log.Notice("Bash shortcut script created") return nil }
func (app *App) Launch(command *exec.Cmd, settings config.Settings, userInterface ui.UserInterface) (err error) { log.Info("Starting the app...") log.Debug("Hiding the user interface...") userInterface.Hide() log.Notice("User interface hidden") if settings.IsSkipAppOutput() { return command.Run() } var outputBytes []byte outputBytes, err = command.CombinedOutput() if outputBytes != nil && len(outputBytes) > 0 { log.Info("------------------------------") log.Info(string(outputBytes)) log.Info("------------------------------") } return err }
func getActualBaseURL(descriptor AppDescriptor) *url.URL { var actualBaseURL *url.URL for _, searchStrategy := range actualBaseURLSearchStrategies { actualBaseURL = searchStrategy(descriptor) if actualBaseURL != nil { log.Notice("The actual base URL has been found by a search strategy!") break } } if actualBaseURL == nil { log.Debug("The actual base URL just matches the declared base URL") actualBaseURL = descriptor.GetDeclaredBaseURL() } actualBaseURLCache[descriptor.GetDeclaredBaseURL().String()] = actualBaseURL return actualBaseURL }
func StartGUI(launcher launchers.Launcher, bootDescriptorPath string) (err error) { log.Debug("Initializing GTK...") gtkui.InitGTK() log.Debug("GTK initialized") guiOutcomeChannel := make(chan guiOutcomeStruct) defer close(guiOutcomeChannel) go backgroundOrchestrator(launcher, bootDescriptorPath, guiOutcomeChannel) log.Debug("Starting GTK main loop...") gtk.Main() log.SetCallback(func(level logging.Level, message string) {}) log.Debug("GTK main loop terminated") select { case guiOutcome := <-guiOutcomeChannel: log.Debug("Outcome retrieved from the GUI channel") if guiOutcome.userInterface != nil && guiOutcome.userInterface.IsClosedByUser() { return &engine.ExecutionCanceled{} } err = guiOutcome.err if err != nil { log.Warning("Err is: %v", err) return err } log.Notice("OK") return nil default: log.Debug("The user has manually closed the program") return &engine.ExecutionCanceled{} } }
/* Run is the entry point you must employ to create a custom installer, for example to employ custom settings or a brand-new user interface, based on any technology */ func Run( launcher launchers.Launcher, userInterface ui.UserInterface, bootDescriptor descriptors.AppDescriptor) (err error) { settings := launcher.GetSettings() //---------------------------------------------------------------------------- setupUserInterface(launcher, userInterface) defer dismissUserInterface(userInterface) //---------------------------------------------------------------------------- userInterface.SetHeader("Performing startup operations") log.Debug("The boot descriptor is: %#v", bootDescriptor) //---------------------------------------------------------------------------- userInterface.SetApp(bootDescriptor.GetName()) //---------------------------------------------------------------------------- appGallery := apps.NewAppGallery(settings.GetGalleryDirectory()) log.Debug("The app gallery is: %#v", appGallery) //---------------------------------------------------------------------------- log.Info("Resolving the app...") app, err := appGallery.GetApp(bootDescriptor) if err != nil { return err } log.Notice("The app directory is: '%v'", app.Directory) log.Debug("App is: %#v", app) firstRun := !app.DirectoryExists() log.Debug("Is this a first run for the app? %v", firstRun) //---------------------------------------------------------------------------- if firstRun { log.Info("Now asking the user if the app can run...") canRun := app.CanPerformFirstRun(userInterface) if !canRun { return &ExecutionCanceled{} } log.Debug("The user agreed to proceed") log.Info("Ensuring the app dir is available...") err = app.EnsureDirectory() if err != nil { return err } log.Notice("App dir available") } //---------------------------------------------------------------------------- log.Info("Locking the app dir...") err = app.LockDirectory() if err != nil { return err } defer func() { unlockErr := app.UnlockDirectory() if unlockErr != nil { log.Warning(unlockErr.Error()) } }() log.Notice("App dir locked") //---------------------------------------------------------------------------- log.Info("Checking for conflicting local descriptors...") err = app.CheckForConflictingLocalDescriptors() if err != nil { return err } log.Notice("No conflicting local descriptors found") //---------------------------------------------------------------------------- log.Info("Resolving the local descriptor...") localDescriptor := app.GetLocalDescriptor() startedWithLocalDescriptor := localDescriptor != nil log.Debug("Started with local descriptor? %v", startedWithLocalDescriptor) if startedWithLocalDescriptor { log.Info("Checking that local descriptor and boot descriptor actually match...") err = descriptors.CheckDescriptorMatch(localDescriptor, bootDescriptor) if err != nil { return err } log.Notice("The descriptors match correctly") } //---------------------------------------------------------------------------- log.Info("Resolving the remote descriptor...") remoteDescriptor := app.GetRemoteDescriptor() if remoteDescriptor != nil { log.Info("Checking that remote descriptor and boot descriptor actually match...") err = descriptors.CheckDescriptorMatch(remoteDescriptor, bootDescriptor) if err != nil { return err } log.Notice("The descriptors match correctly") } //---------------------------------------------------------------------------- log.Info("Now choosing the reference descriptor...") referenceDescriptor, err := app.GetReferenceDescriptor() if err != nil { return err } log.Notice("Reference descriptor chosen") log.Debug("The reference descriptor is: %#v", referenceDescriptor) //---------------------------------------------------------------------------- err = referenceDescriptor.CheckRequirements() if err != nil { return err } //---------------------------------------------------------------------------- userInterface.SetApp(referenceDescriptor.GetTitle()) //---------------------------------------------------------------------------- log.Info("Resolving the OS-specific app command line...") commandLine := referenceDescriptor.GetCommandLine() log.Notice("Command line resolved") log.Debug("Command line is: %v", commandLine) //---------------------------------------------------------------------------- err = app.CheckFiles(settings, userInterface) if err != nil { return err } //---------------------------------------------------------------------------- userInterface.SetHeader("Preparing the command...") log.Info("Creating the command...") command := app.PrepareCommand(commandLine) log.Notice("Command created") log.Debug("Command path: %v", command.Path) log.Debug("Command arguments: %v", command.Args) //---------------------------------------------------------------------------- referenceDescriptorSaved := app.SaveReferenceDescriptor() if !startedWithLocalDescriptor && referenceDescriptorSaved { if userInterface.AskForDesktopShortcut(referenceDescriptor) { log.Info("Creating desktop shortcut...") err = app.CreateDesktopShortcut(launcher, referenceDescriptor) if err != nil { log.Warning("Could not create desktop shortcut: %v", err) } else { log.Notice("Desktop shortcut created") } } else { log.Info("The user refused to create a desktop shortcut") } } //---------------------------------------------------------------------------- app.UnlockDirectory() //---------------------------------------------------------------------------- userInterface.SetHeader("Launching the application") userInterface.SetStatus("") return app.Launch(command, settings, userInterface) }
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 }
func GetGitHubDescriptorInfo(baseURL *url.URL, descriptorFileName string) *GitHubDescriptorInfo { projectParams := latestVersionURLRegex.FindStringSubmatch(baseURL.String()) if projectParams == nil { log.Debug("The URL does not reference a 'latest' release on GitHub") return nil } log.Debug("The URL references a 'latest' release on GitHub") gitHubUser := projectParams[1] gitHubRepo := projectParams[2] apiLatestVersionURL, err := url.Parse(fmt.Sprintf( apiLatestVersionURLTemplate, gitHubUser, gitHubRepo)) if err != nil { log.Warning(err.Error()) return nil } log.Debug("Calling GitHub's API, at '%v'...", apiLatestVersionURL) apiResponseBytes, err := caravel.RetrieveFromURL(apiLatestVersionURL) if err != nil { log.Warning(err.Error()) return nil } log.Debug("API returned OK") log.Debug("Deserializing the API response...") var latestVersionResponse latestVersionResponse err = json.Unmarshal(apiResponseBytes, &latestVersionResponse) if err != nil { log.Warning(err.Error()) return nil } log.Debug("Response correctly deserialized: %#v", latestVersionResponse) log.Debug("Now processing the response fields...") result := &GitHubDescriptorInfo{} for _, asset := range latestVersionResponse.Assets { if asset.Name == descriptorFileName { result.DescriptorURL, err = url.Parse(asset.BrowserDownloadURL) if err != nil { log.Warning("Error while parsing the BrowserDownloadURL: %v", err.Error()) return nil } break } } if result.DescriptorURL == nil { log.Warning("The app descriptor ('%v') could not be found as an asset of the latest release", descriptorFileName) return nil } tagComponents := tagRegex.FindStringSubmatch(latestVersionResponse.TagName) if tagComponents == nil { log.Warning("GitHub's release tag must be in the format: <any string, even empty><VERSION>, not '%v'", latestVersionResponse.TagName) return nil } result.Version, err = versioning.ParseVersion(tagComponents[1]) if err != nil { log.Warning("Error while parsing the version: %v", err.Error()) return nil } log.Notice("Response fields correctly processed") return result }