Update updatemgr

This commit is contained in:
Daniel 2021-08-23 23:30:49 +02:00
parent 8617724ec1
commit 85f69feef7

View file

@ -1,12 +1,15 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"github.com/safing/portbase/updater"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -14,42 +17,39 @@ var (
releaseCmd = &cobra.Command{ releaseCmd = &cobra.Command{
Use: "release", Use: "release",
Short: "Release scans the distribution directory and creates registry indexes and the symlink structure", Short: "Release scans the distribution directory and creates registry indexes and the symlink structure",
Args: cobra.ExactArgs(1),
RunE: release, RunE: release,
} }
preReleaseCmd = &cobra.Command{ preReleaseCmd = &cobra.Command{
Use: "prerelease", 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", 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), Args: cobra.ExactArgs(1),
RunE: prerelease, RunE: release,
} }
preReleaseFrom string
resetPreReleases bool resetPreReleases bool
includeUnreleased bool
) )
func init() { func init() {
rootCmd.AddCommand(releaseCmd) rootCmd.AddCommand(releaseCmd)
rootCmd.AddCommand(preReleaseCmd) rootCmd.AddCommand(preReleaseCmd)
preReleaseCmd.Flags().StringVar(&preReleaseFrom, "from", "", "Make a pre-release based on the given channel")
_ = preReleaseCmd.MarkFlagRequired("from")
preReleaseCmd.Flags().BoolVar(&resetPreReleases, "reset", false, "Reset pre-release assets") preReleaseCmd.Flags().BoolVar(&resetPreReleases, "reset", false, "Reset pre-release assets")
} }
func release(cmd *cobra.Command, args []string) error { func release(cmd *cobra.Command, args []string) error {
return writeIndex(
"stable",
getChannelVersions("", false),
)
}
func prerelease(cmd *cobra.Command, args []string) error {
channel := args[0] channel := args[0]
// Check if we want to reset instead. // Check if we want to reset instead.
if resetPreReleases { if resetPreReleases {
return removeFilesFromIndex(getChannelVersions(channel, true)) return removeFilesFromIndex(getChannelVersions(channel, preReleaseFrom, true))
} }
return writeIndex( return writeIndex(
channel, channel,
getChannelVersions(channel, false), getChannelVersions(channel, preReleaseFrom, false),
) )
} }
@ -108,7 +108,18 @@ func removeFilesFromIndex(versions map[string]string) error {
return nil return nil
} }
func getChannelVersions(channel string, storagePath bool) map[string]string { func getChannelVersions(channel string, prereleaseFrom string, storagePath bool) map[string]string {
if prereleaseFrom != "" {
registry.AddIndex(updater.Index{
Path: prereleaseFrom + ".json",
PreRelease: false,
})
err := registry.LoadIndexes(context.Background())
if err != nil {
panic(err)
}
}
// Sort all versions. // Sort all versions.
registry.SelectVersions() registry.SelectVersions()
export := registry.Export() export := registry.Export()
@ -117,17 +128,11 @@ func getChannelVersions(channel string, storagePath bool) map[string]string {
versions := make(map[string]string) versions := make(map[string]string)
for _, rv := range export { for _, rv := range export {
for _, v := range rv.Versions { for _, v := range rv.Versions {
// Ignore versions that don't match the release channel. // Ignore versions that are in the reference release channel.
if v.SemVer().Prerelease() != channel { if v.CurrentRelease {
// Stop at the first stable version, nothing should ever be selected
// beyond that.
if v.SemVer().Prerelease() == "" {
break break
} }
continue
}
// Add highest version of matching release channel. // Add highest version of matching release channel.
if storagePath { if storagePath {
versions[rv.Identifier] = rv.GetFile().Path() versions[rv.Identifier] = rv.GetFile().Path()