Exemple #1
0
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 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
}