Adapt updatemgr to new release channel handling
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -31,24 +30,7 @@ var rootCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
registry = &updater.ResourceRegistry{}
|
registry = &updater.ResourceRegistry{}
|
||||||
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0o755))
|
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0755))
|
||||||
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())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,83 +4,140 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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() {
|
func init() {
|
||||||
rootCmd.AddCommand(releaseCmd)
|
rootCmd.AddCommand(releaseCmd)
|
||||||
}
|
rootCmd.AddCommand(preReleaseCmd)
|
||||||
|
preReleaseCmd.Flags().BoolVar(&resetPreReleases, "reset", false, "Reset pre-release assets")
|
||||||
var releaseCmd = &cobra.Command{
|
|
||||||
Use: "release",
|
|
||||||
Short: "Release scans the distribution directory and creates registry indexes and the symlink structure",
|
|
||||||
RunE: release,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func release(cmd *cobra.Command, args []string) error {
|
func release(cmd *cobra.Command, args []string) error {
|
||||||
// Set stable and beta to latest version.
|
return writeIndex(
|
||||||
updateToLatestVersion(true, true)
|
"stable",
|
||||||
|
getChannelVersions("", false),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Export versions.
|
func prerelease(cmd *cobra.Command, args []string) error {
|
||||||
betaData, err := json.MarshalIndent(exportSelected(true), "", " ")
|
channel := args[0]
|
||||||
if err != nil {
|
|
||||||
return err
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build destination paths.
|
// Build destination path.
|
||||||
betaIndexFilePath := filepath.Join(registry.StorageDir().Path, "beta.json")
|
indexFilePath := filepath.Join(registry.StorageDir().Path, channel+".json")
|
||||||
stableIndexFilePath := filepath.Join(registry.StorageDir().Path, "stable.json")
|
|
||||||
|
|
||||||
// Print previews.
|
// Print preview.
|
||||||
fmt.Printf("beta (%s):\n", betaIndexFilePath)
|
fmt.Printf("%s (%s):\n", channel, indexFilePath)
|
||||||
fmt.Println(string(betaData))
|
fmt.Println(string(versionData))
|
||||||
fmt.Printf("\nstable: (%s)\n", stableIndexFilePath)
|
|
||||||
fmt.Println(string(stableData))
|
|
||||||
|
|
||||||
// Ask for confirmation.
|
// 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...")
|
fmt.Println("aborted...")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write indexes.
|
// Write new index to disk.
|
||||||
err = ioutil.WriteFile(betaIndexFilePath, betaData, 0o644) //nolint:gosec // 0644 is intended
|
err = ioutil.WriteFile(indexFilePath, versionData, 0644) //nolint:gosec // 0644 is intended
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("written %s\n", betaIndexFilePath)
|
fmt.Printf("written %s\n", indexFilePath)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateToLatestVersion(stable, beta bool) error {
|
func removeFilesFromIndex(versions map[string]string) error {
|
||||||
for _, resource := range registry.Export() {
|
// Print preview.
|
||||||
sort.Sort(resource)
|
fmt.Println("To be deleted:")
|
||||||
err := resource.AddVersion(resource.Versions[0].VersionNumber, false, stable, beta)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("deleted")
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ var scanCmd = &cobra.Command{
|
|||||||
|
|
||||||
func scan(cmd *cobra.Command, args []string) error {
|
func scan(cmd *cobra.Command, args []string) error {
|
||||||
// Reset and rescan.
|
// Reset and rescan.
|
||||||
registry.Reset()
|
registry.ResetResources()
|
||||||
err := registry.ScanStorage("")
|
err := registry.ScanStorage("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -36,8 +36,8 @@ func scan(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func exportSelected(beta bool) map[string]string {
|
func exportSelected(preReleases bool) map[string]string {
|
||||||
registry.SetBeta(beta)
|
registry.SetUsePreReleases(preReleases)
|
||||||
registry.SelectVersions()
|
registry.SelectVersions()
|
||||||
export := registry.Export()
|
export := registry.Export()
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user