Adapt updatemgr to new release channel handling

This commit is contained in:
Daniel
2021-06-03 23:30:52 +02:00
parent 417d7e4743
commit 9da5ff86d3
4 changed files with 105 additions and 162 deletions

View File

@@ -1,7 +1,6 @@
package main
import (
"context"
"fmt"
"os"
"path/filepath"
@@ -31,24 +30,7 @@ var rootCmd = &cobra.Command{
}
registry = &updater.ResourceRegistry{}
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0o755))
if err != nil {
return err
}
registry.AddIndex(updater.Index{
Path: "stable.json",
Stable: true,
Beta: false,
})
registry.AddIndex(updater.Index{
Path: "beta.json",
Stable: false,
Beta: true,
})
err = registry.LoadIndexes(context.TODO())
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0755))
if err != nil {
return err
}

View File

@@ -4,83 +4,140 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"github.com/spf13/cobra"
)
var (
releaseCmd = &cobra.Command{
Use: "release",
Short: "Release scans the distribution directory and creates registry indexes and the symlink structure",
RunE: release,
}
preReleaseCmd = &cobra.Command{
Use: "prerelease",
Short: "Stage scans the specified directory and loads the indexes - it then creates a staging index with all files newer than the stable and beta indexes",
Args: cobra.ExactArgs(1),
RunE: prerelease,
}
resetPreReleases bool
includeUnreleased bool
)
func init() {
rootCmd.AddCommand(releaseCmd)
}
var releaseCmd = &cobra.Command{
Use: "release",
Short: "Release scans the distribution directory and creates registry indexes and the symlink structure",
RunE: release,
rootCmd.AddCommand(preReleaseCmd)
preReleaseCmd.Flags().BoolVar(&resetPreReleases, "reset", false, "Reset pre-release assets")
}
func release(cmd *cobra.Command, args []string) error {
// Set stable and beta to latest version.
updateToLatestVersion(true, true)
return writeIndex(
"stable",
getChannelVersions("", false),
)
}
// Export versions.
betaData, err := json.MarshalIndent(exportSelected(true), "", " ")
if err != nil {
return err
func prerelease(cmd *cobra.Command, args []string) error {
channel := args[0]
// Check if we want to reset instead.
if resetPreReleases {
return removeFilesFromIndex(getChannelVersions(channel, true))
}
stableData, err := json.MarshalIndent(exportSelected(false), "", " ")
return writeIndex(
channel,
getChannelVersions(channel, false),
)
}
func writeIndex(channel string, versions map[string]string) error {
// Export versions and format them.
versionData, err := json.MarshalIndent(versions, "", " ")
if err != nil {
return err
}
// Build destination paths.
betaIndexFilePath := filepath.Join(registry.StorageDir().Path, "beta.json")
stableIndexFilePath := filepath.Join(registry.StorageDir().Path, "stable.json")
// Build destination path.
indexFilePath := filepath.Join(registry.StorageDir().Path, channel+".json")
// Print previews.
fmt.Printf("beta (%s):\n", betaIndexFilePath)
fmt.Println(string(betaData))
fmt.Printf("\nstable: (%s)\n", stableIndexFilePath)
fmt.Println(string(stableData))
// Print preview.
fmt.Printf("%s (%s):\n", channel, indexFilePath)
fmt.Println(string(versionData))
// Ask for confirmation.
if !confirm("\nDo you want to write these new indexes (and update latest symlinks)?") {
if !confirm("\nDo you want to write this index?") {
fmt.Println("aborted...")
return nil
}
// Write indexes.
err = ioutil.WriteFile(betaIndexFilePath, betaData, 0o644) //nolint:gosec // 0644 is intended
// Write new index to disk.
err = ioutil.WriteFile(indexFilePath, versionData, 0644) //nolint:gosec // 0644 is intended
if err != nil {
return err
}
fmt.Printf("written %s\n", betaIndexFilePath)
err = ioutil.WriteFile(stableIndexFilePath, stableData, 0o644) //nolint:gosec // 0644 is intended
if err != nil {
return err
}
fmt.Printf("written %s\n", stableIndexFilePath)
// Create symlinks to latest stable versions.
symlinksDir := registry.StorageDir().ChildDir("latest", 0o755)
err = registry.CreateSymlinks(symlinksDir)
if err != nil {
return err
}
fmt.Printf("updated stable symlinks in %s\n", symlinksDir.Path)
fmt.Printf("written %s\n", indexFilePath)
return nil
}
func updateToLatestVersion(stable, beta bool) error {
for _, resource := range registry.Export() {
sort.Sort(resource)
err := resource.AddVersion(resource.Versions[0].VersionNumber, false, stable, beta)
func removeFilesFromIndex(versions map[string]string) error {
// Print preview.
fmt.Println("To be deleted:")
for _, filePath := range versions {
fmt.Println(filePath)
}
// Ask for confirmation.
if !confirm("\nDo you want to delete these files?") {
fmt.Println("aborted...")
return nil
}
// Delete files.
for _, filePath := range versions {
err := os.Remove(filePath)
if err != nil {
return err
}
}
fmt.Println("deleted")
return nil
}
func getChannelVersions(channel string, storagePath bool) map[string]string {
// Sort all versions.
registry.SelectVersions()
export := registry.Export()
// Go through all versions and save the highest version, if not stable or beta.
versions := make(map[string]string)
for _, rv := range export {
for _, v := range rv.Versions {
// Ignore versions that don't match the release channel.
if v.SemVer().Prerelease() != channel {
// Stop at the first stable version, nothing should ever be selected
// beyond that.
if v.SemVer().Prerelease() == "" {
break
}
continue
}
// Add highest version of matching release channel.
if storagePath {
versions[rv.Identifier] = rv.GetFile().Path()
} else {
versions[rv.Identifier] = v.VersionNumber
}
break
}
}
return versions
}

View File

@@ -19,7 +19,7 @@ var scanCmd = &cobra.Command{
func scan(cmd *cobra.Command, args []string) error {
// Reset and rescan.
registry.Reset()
registry.ResetResources()
err := registry.ScanStorage("")
if err != nil {
return err
@@ -36,8 +36,8 @@ func scan(cmd *cobra.Command, args []string) error {
return nil
}
func exportSelected(beta bool) map[string]string {
registry.SetBeta(beta)
func exportSelected(preReleases bool) map[string]string {
registry.SetUsePreReleases(preReleases)
registry.SelectVersions()
export := registry.Export()

View File

@@ -1,96 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
var (
stageReset bool
)
func init() {
rootCmd.AddCommand(stageCmd)
stageCmd.Flags().BoolVar(&stageReset, "reset", false, "Reset staging assets")
}
var stageCmd = &cobra.Command{
Use: "stage",
Short: "Stage scans the specified directory and loads the indexes - it then creates a staging index with all files newer than the stable and beta indexes",
RunE: stage,
}
func stage(cmd *cobra.Command, args []string) error {
// Check if we want to reset staging instead.
if stageReset {
for _, stagedPath := range exportStaging(true) {
err := os.Remove(stagedPath)
if err != nil {
return err
}
}
return nil
}
// Export all staged versions and format them.
stagingData, err := json.MarshalIndent(exportStaging(false), "", " ")
if err != nil {
return err
}
// Build destination path.
stagingIndexFilePath := filepath.Join(registry.StorageDir().Path, "staging.json")
// Print preview.
fmt.Printf("staging (%s):\n", stagingIndexFilePath)
fmt.Println(string(stagingData))
// Ask for confirmation.
if !confirm("\nDo you want to write this index?") {
fmt.Println("aborted...")
return nil
}
// Write new index to disk.
err = ioutil.WriteFile(stagingIndexFilePath, stagingData, 0o644) //nolint:gosec // 0644 is intended
if err != nil {
return err
}
fmt.Printf("written %s\n", stagingIndexFilePath)
return nil
}
func exportStaging(storagePath bool) map[string]string {
// Sort all versions.
registry.SetBeta(false)
registry.SelectVersions()
export := registry.Export()
// Go through all versions and save the highest version, if not stable or beta.
versions := make(map[string]string)
for _, rv := range export {
// Get highest version.
v := rv.Versions[0]
// Do not take stable or beta releases into account.
if v.StableRelease || v.BetaRelease {
continue
}
// Add highest version to staging
if storagePath {
rv.SelectedVersion = v
versions[rv.Identifier] = rv.GetFile().Path()
} else {
versions[rv.Identifier] = v.VersionNumber
}
}
return versions
}