func (descriptor *appDescriptorV1V2) Init() (err error) {
	descriptor.descriptorVersion, err = versioning.ParseVersion(descriptor.DescriptorVersion)
	if err != nil {
		return err
	}

	descriptor.appVersion, err = versioning.ParseVersion(descriptor.Version)
	if err != nil {
		return err
	}

	descriptor.declaredBaseURL, err = url.Parse(ensureTrailingSlash(descriptor.BaseURL))
	if err != nil {
		return err
	}

	descriptor.setIconPath()

	descriptor.setCommandLine()

	descriptor.packageVersions, err = parsePackageVersions(descriptor.PackageVersions)
	if err != nil {
		return err
	}

	descriptor.actualBaseURL = getActualBaseURL(descriptor)

	return nil
}
Exemple #2
0
func NewAppDescriptorFromBytes(descriptorBytes []byte) (descriptor AppDescriptor, err error) {
	basicDescriptor, err := createBasicDescriptor(descriptorBytes)
	if err != nil {
		return nil, err
	}

	descriptorVersion, err := versioning.ParseVersion(basicDescriptor.DescriptorVersion)
	if err != nil {
		return nil, fmt.Errorf("Invalid Descriptor Version: %v", err.Error())
	}

	switch descriptorVersion.Major {
	case 3:
		log.Notice("V3 descriptor found! Deserializing it")
		descriptor, err = createV3Descriptor(descriptorBytes)

	case 2:
	case 1:
		log.Notice("V1/V2 descriptor found! Deserializing it")
		descriptor, err = createV1V2Descriptor(descriptorBytes)

	default:
		return nil, fmt.Errorf("Unsupported descriptor version (%v). Please, consider updating MoonDeploy - your current version is %v.",
			descriptorVersion,
			moondeploy.Version)
	}

	err = descriptor.Init()
	if err != nil {
		return nil, err
	}

	err = validate(descriptor)
	if err != nil {
		return nil, err
	}

	return descriptor, nil
}
Exemple #3
0
func parsePackageVersions(packageVersionsStringMap map[string]string) (result map[string]*versioning.Version, err error) {
	result = make(map[string]*versioning.Version)

	if packageVersionsStringMap == nil {
		return result, nil
	}

	for packageName, packageVersionString := range packageVersionsStringMap {
		if packageVersionString != "" {
			result[packageName], err = versioning.ParseVersion(packageVersionString)
			if err != nil {
				return nil, fmt.Errorf("Invalid version string for package '%v': '%v'",
					packageName,
					packageVersionString)
			}
		} else {
			result[packageName] = nil
		}
	}

	return result, 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
}
func (descriptor *appDescriptorV3) Init() (err error) {
	descriptor.descriptorVersion, err = versioning.ParseVersion(descriptor.DescriptorVersion)
	if err != nil {
		return fmt.Errorf("Error while parsing the Descriptor Version: %v", err.Error())
	}

	descriptor.declaredBaseURL, err = url.Parse(ensureTrailingSlash(descriptor.BaseURL))
	if err != nil {
		return fmt.Errorf("Error while parsing the Base URL: %v", err.Error())
	}

	if descriptor.DescriptorFileName != "" {
		descriptor.descriptorFileName = descriptor.DescriptorFileName
	} else {
		descriptor.descriptorFileName = defaultDescriptorFileName
	}

	descriptor.name = descriptor.Name

	descriptor.appVersion, err = versioning.ParseVersion(descriptor.Version)
	if err != nil {
		return fmt.Errorf("Error while parsing the app version: %v", err.Error())
	}

	descriptor.publisher = descriptor.Publisher
	descriptor.description = descriptor.Description

	descriptor.skipPackageLevels = descriptor.SkipPackageLevels
	descriptor.skipUpdateCheck = descriptor.SkipUpdateCheck

	if descriptor.SupportedOS != nil {
		descriptor.supportedSystems = descriptor.SupportedOS
	} else {
		descriptor.supportedSystems = []string{}
	}

	osSettings, osSettingsFound := descriptor.OS[runtime.GOOS]

	if osSettingsFound && osSettings.Packages != nil {
		descriptor.packageVersions, err = parsePackageVersions(osSettings.Packages)
	} else {
		descriptor.packageVersions, err = parsePackageVersions(descriptor.Packages)
	}

	if err != nil {
		return fmt.Errorf("Error while parsing the package versions: %v", err.Error())
	}

	if osSettingsFound && osSettings.CommandLine != nil {
		descriptor.commandLine = osSettings.CommandLine
	} else {
		descriptor.commandLine = descriptor.CommandLine
	}

	if osSettingsFound && osSettings.IconPath != "" {
		descriptor.iconPath = osSettings.IconPath
	} else {
		descriptor.iconPath = descriptor.IconPath
	}

	descriptor.actualBaseURL = getActualBaseURL(descriptor)

	return nil
}