Skip to content

Commit

Permalink
fix save channel on install, normalize channel names for flypkgs migr…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
michaeldwan committed Sep 22, 2023
1 parent 090b539 commit ab66cff
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 75 deletions.
16 changes: 3 additions & 13 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -82,16 +81,7 @@ type cache struct {
}

func (c *cache) Channel() string {
return normalizeChannel(c.channel)
}

func normalizeChannel(c string) string {
const pre = "pre"
if strings.Contains(c, pre) {
return pre
}

return "latest"
return update.NormalizeChannel(c.channel)
}

func (c *cache) Dirty() bool {
Expand All @@ -107,7 +97,7 @@ func (c *cache) SetChannel(channel string) string {

c.dirty = true

if channel = normalizeChannel(channel); c.channel != channel {
if channel = update.NormalizeChannel(channel); c.channel != channel {
// purge timestamp & release since we're changing channels
c.lastCheckedAt = time.Time{}
c.latestRelease = nil
Expand Down Expand Up @@ -136,7 +126,7 @@ func (c *cache) SetLatestRelease(channel string, r *update.Release) {
c.mu.Lock()
defer c.mu.Unlock()

if channel = normalizeChannel(channel); channel != c.channel {
if channel = update.NormalizeChannel(channel); channel != c.channel {
return
}

Expand Down
43 changes: 0 additions & 43 deletions internal/command/version/init_state.go

This file was deleted.

65 changes: 65 additions & 0 deletions internal/command/version/save_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package version

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/superfly/flyctl/internal/cache"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/state"
"github.com/superfly/flyctl/internal/update"
"github.com/superfly/flyctl/iostreams"
)

func newSaveInstall() *cobra.Command {
cmd := command.New(
"save-install",
"save-install",
"Save installation configuration",
runSaveInstall)

flag.Add(cmd,
flag.String{
Name: "channel",
Description: "Channel to use for updates",
},
)
flag.Add(cmd,
flag.Bool{
Name: "disable-auto-update",
Description: "Disable automatic updates",
},
)

cmd.Hidden = true

return cmd
}

func runSaveInstall(ctx context.Context) error {
channel := flag.GetString(ctx, "channel")
autoUpdateEnabled := flag.GetBool(ctx, "auto-update")

return saveInstall(ctx, channel, autoUpdateEnabled)
}

func saveInstall(ctx context.Context, channel string, autoUpdateEnabled bool) error {
io := iostreams.FromContext(ctx)
cache := cache.FromContext(ctx)

cache.SetChannel(update.NormalizeChannel(channel))

fmt.Fprintf(io.ErrOut, "set update hannel to %s\n", channel)

// TODO[md]: This was copied from internal/command/settings/autoupdate.go... move it to a helper
// so we're not doing it twice
path := state.ConfigFile(ctx)
if err := config.SetAutoUpdate(path, autoUpdateEnabled); err != nil {
return fmt.Errorf("failed persisting %s in %s: %w", config.AutoUpdateFileKey, path, err)
}
return nil
}
18 changes: 5 additions & 13 deletions internal/command/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import (
"github.com/superfly/flyctl/iostreams"

"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/cache"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
)

const saveInstallName = "saveinstall"

// New initializes and returns a new version Command.
func New() *cobra.Command {
const (
Expand All @@ -33,14 +30,15 @@ number and build date.`
// TODO: remove once installer is updated to use init-state
flag.Add(version,
flag.String{
Name: saveInstallName,
Name: "saveinstall",
Shorthand: "s",
Description: "Save parameter in config",
Hidden: true,
},
)

version.AddCommand(
newInitState(),
newSaveInstall(),
newUpgrade(),
)

Expand All @@ -49,14 +47,8 @@ number and build date.`
}

func run(ctx context.Context) (err error) {
if saveInstall := flag.GetString(ctx, saveInstallName); saveInstall != "" {
err = executeInitState(
iostreams.FromContext(ctx),
cache.FromContext(ctx),
saveInstall,
)

return
if channel := flag.GetString(ctx, "saveinstall"); channel != "" {
return saveInstall(ctx, channel, true)
}

var (
Expand Down
25 changes: 19 additions & 6 deletions internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ var _validatedReleaseLock sync.Mutex
// If the version is invalid, the error will be an InvalidReleaseError.
// Note that other errors may be returned if the API call fails.
func ValidateRelease(ctx context.Context, version string) (err error) {

_validatedReleaseLock.Lock()
defer _validatedReleaseLock.Unlock()

Expand Down Expand Up @@ -121,6 +120,7 @@ func ValidateRelease(ctx context.Context, version string) (err error) {

// LatestRelease reports the latest release for the given channel.
func LatestRelease(ctx context.Context, channel string) (*Release, error) {
channel = translateChannelForRails(channel)

// If running under homebrew, use the homebrew API to get the latest release
if IsUnderHomebrew() {
Expand Down Expand Up @@ -193,7 +193,6 @@ var errBrewNotFound = errors.New("command 'brew' not found")
var _brewBinDir memoize[string]

func brewBinDir() (string, error) {

return _brewBinDir.Get(func() (string, error) {

brewExe, err := safeexec.LookPath("brew")
Expand All @@ -219,7 +218,6 @@ var _isUnderHomebrew memoize[bool]
// IsUnderHomebrew reports whether the fly binary was found under the Homebrew
// prefix.
func IsUnderHomebrew() bool {

if runtime.GOOS == "windows" {
return false
}
Expand Down Expand Up @@ -331,7 +329,6 @@ func UpgradeInPlace(ctx context.Context, io *iostreams.IOStreams, prelease, sile
}

func GetCurrentBinaryPath() (string, error) {

if IsUnderHomebrew() {
brewBinPrefix, err := brewBinDir()
if err != nil {
Expand Down Expand Up @@ -382,7 +379,6 @@ func CanUpdateThisInstallation() bool {

// Relaunch only returns on error
func Relaunch(ctx context.Context, silent bool) error {

io := iostreams.FromContext(ctx)

if !silent {
Expand Down Expand Up @@ -469,7 +465,6 @@ func currentWindowsBinaries() ([]string, error) {

// BackgroundUpdate begins an update in the background.
func BackgroundUpdate() error {

binPath, err := exec.LookPath(os.Args[0])
if err != nil {
return err
Expand All @@ -486,3 +481,21 @@ func BackgroundUpdate() error {
}
return nil
}

func NormalizeChannel(channel string) string {
channel = strings.ToLower(channel)

return channel
}

// Old install code conflates channels and versions. This fixes it so
// "stable" maps to "latest" while preserving prerelease behavior.
// This will get removed once we're using the new flypkgs api.
func translateChannelForRails(channel string) string {
switch channel {
case "pre", "prerelease":
return "pre"
default:
return "latest"
}
}

0 comments on commit ab66cff

Please sign in to comment.