Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"github.com/cloudposse/atmos/internal/exec"
)

var checkFlag bool
var (
checkFlag bool
versionFormat string
)

var versionCmd = &cobra.Command{
Use: "version",
Expand All @@ -15,13 +18,12 @@
Example: "atmos version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
exec.NewVersionExec().Execute(checkFlag)
// Check for the cache and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
checkErrorAndExit(exec.NewVersionExec(&atmosConfig).Execute(checkFlag, versionFormat))

Check warning on line 21 in cmd/version.go

View check run for this annotation

Codecov / codecov/patch

cmd/version.go#L21

Added line #L21 was not covered by tests
},
}

func init() {
versionCmd.Flags().BoolVarP(&checkFlag, "check", "c", false, "Run additional checks after displaying version info")
versionCmd.Flags().StringVar(&versionFormat, "format", "", "Specify the output format")
RootCmd.AddCommand(versionCmd)
}
8 changes: 4 additions & 4 deletions internal/exec/mock_version_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 112 additions & 7 deletions internal/exec/version.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package exec

import (
"encoding/json"
"fmt"
"runtime"
"strings"

log "github.com/charmbracelet/log"
tuiUtils "github.com/cloudposse/atmos/internal/tui/utils"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
"gopkg.in/yaml.v2"

"github.com/cloudposse/atmos/pkg/version"
)

type versionExec struct {
atmosConfig *schema.AtmosConfiguration
printStyledText func(string) error
getLatestGitHubRepoRelease func(string, string) (string, error)
getLatestGitHubRepoRelease func() (string, error)
printMessage func(string)
printMessageToUpgradeToAtmosLatestRelease func(string)
loadCacheConfig func() (cfg.CacheConfig, error)
shouldCheckForUpdates func(lastChecked int64, frequency string) bool
}

func NewVersionExec() *versionExec {
func NewVersionExec(atmosConfig *schema.AtmosConfiguration) *versionExec {

Check warning on line 29 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L29

Added line #L29 was not covered by tests
return &versionExec{
printStyledText: tuiUtils.PrintStyledText,
getLatestGitHubRepoRelease: u.GetLatestGitHubRepoRelease,
printMessage: u.PrintMessage,
atmosConfig: atmosConfig,
printStyledText: tuiUtils.PrintStyledText,
getLatestGitHubRepoRelease: func() (string, error) {
return u.GetLatestGitHubRepoRelease("cloudposse", "atmos")
},

Check warning on line 35 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L31-L35

Added lines #L31 - L35 were not covered by tests
printMessage: u.PrintMessage,
printMessageToUpgradeToAtmosLatestRelease: u.PrintMessageToUpgradeToAtmosLatestRelease,
loadCacheConfig: cfg.LoadCache,
shouldCheckForUpdates: cfg.ShouldCheckForUpdates,
}
}

func (v versionExec) Execute(checkFlag bool) {
func (v versionExec) Execute(checkFlag bool, format string) error {
if format != "" {
return v.displayVersionInFormat(checkFlag, format)
}

Check warning on line 46 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L45-L46

Added lines #L45 - L46 were not covered by tests
// Print a styled Atmos logo to the terminal
v.printMessage("")
err := v.printStyledText("ATMOS")
Expand All @@ -44,12 +59,102 @@

if checkFlag {
v.checkRelease()
return nil
}

if updatedVersion, ok := v.GetLatestVersion(false); ok {
u.PrintMessageToUpgradeToAtmosLatestRelease(updatedVersion)
}

Check warning on line 67 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L66-L67

Added lines #L66 - L67 were not covered by tests
return nil
}

type Version struct {
Version string `json:"version" yaml:"version"`
OS string `json:"os" yaml:"os"`
Arch string `json:"arch" yaml:"arch"`
UpdateVersion string `json:"update_version,omitempty" yaml:"update_version,omitempty"`
}

func (v versionExec) isCheckVersionEnabled(forceCheck bool) bool {
if forceCheck {
// If force checking is enabled, always return true
return true
}

// If version checking is disabled in the configuration, do nothing
if !v.atmosConfig.Version.Check.Enabled {
return false
}

// Load the cache
cacheCfg, err := v.loadCacheConfig()
if err != nil {
log.Warn("Could not load cache", err)
return false
}

Check warning on line 94 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L92-L94

Added lines #L92 - L94 were not covered by tests

// Determine if it's time to check for updates based on frequency and last_checked
if !v.shouldCheckForUpdates(cacheCfg.LastChecked, v.atmosConfig.Version.Check.Frequency) {
// Not due for another check yet, so return without printing anything
return false
}

Check warning on line 100 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L98-L100

Added lines #L98 - L100 were not covered by tests

return true
}

func (v versionExec) GetLatestVersion(forceCheck bool) (string, bool) {
if !v.isCheckVersionEnabled(forceCheck) {
return "", false
}
// Get the latest Atmos release from GitHub
latestReleaseTag, err := v.getLatestGitHubRepoRelease()
if err != nil {
log.Warn("Failed to retrieve latest Atmos release info", err)
return "", false
}

if latestReleaseTag == "" {
log.Warn("No release information available")
return "", false
}

Check warning on line 119 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L117-L119

Added lines #L117 - L119 were not covered by tests

// Trim "v" prefix to compare versions
latestVersion := strings.TrimPrefix(latestReleaseTag, "v")
currentVersion := strings.TrimPrefix(version.Version, "v")

if latestVersion != currentVersion {
return latestVersion, true
}
return "", false
}

func (v versionExec) displayVersionInFormat(forceCheck bool, format string) error {
version := Version{
Version: version.Version,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}
if v, ok := v.GetLatestVersion(forceCheck); ok {
version.UpdateVersion = strings.TrimPrefix(v, "v")
}

Check warning on line 139 in internal/exec/version.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/version.go#L138-L139

Added lines #L138 - L139 were not covered by tests
switch format {
case "json":
if data, err := json.MarshalIndent(version, " ", " "); err == nil {
fmt.Println(string(data))
}
case "yaml":
if data, err := yaml.Marshal(version); err == nil {
fmt.Println(string(data))
}
default:
return ErrInvalidFormat
}
return nil
}

func (v versionExec) checkRelease() {
// Check for the latest Atmos release on GitHub
latestReleaseTag, err := v.getLatestGitHubRepoRelease("cloudposse", "atmos")
latestReleaseTag, err := v.getLatestGitHubRepoRelease()
if err != nil || latestReleaseTag == "" {
log.Debug("Did not get release tag", "err", err, "latestReleaseTag", latestReleaseTag)
return
Expand Down
Loading
Loading