Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- fix(compute/deploy): remove compute trial activation code because trials no longer exist ([#1730](https://github.com/fastly/cli/pull/1730))
- fix(auth): SSO token expiration status now reflects the actual API token lifetime (~12 hours) instead of the internal JWT refresh token (~30 minutes), preventing spurious warnings and premature re-authentication [#1728](https://github.com/fastly/cli/pull/1728)
- fix(argparser): skip ListVersions API call for numeric versions [#1774](https://github.com/fastly/cli/pull/1774)

### Enhancements:

Expand Down
2 changes: 1 addition & 1 deletion pkg/argparser/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
// FlagVersionName is the flag name.
FlagVersionName = "version"
// FlagVersionDesc is the flag description.
FlagVersionDesc = "'latest', 'active', or the number of a specific Fastly service version"
FlagVersionDesc = "'latest', 'active', 'staged', or the number of a specific Fastly service version"
)

// PaginationDirection is a list of directions the page results can be displayed.
Expand Down
118 changes: 66 additions & 52 deletions pkg/argparser/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,42 +115,81 @@ type OptionalServiceVersion struct {
}

// Parse returns a service version based on the given user input.
//
// Supported values:
// - Numeric version (e.g., "1", "2", "42"): Returns the specified version
// - "active": Returns the currently active version
// - "staged": Returns the currently staged version
// - "latest": Returns the highest version number (latest version)
// - Omitted (no flag provided): Returns active version, falls back to latest if no active version exists
func (sv *OptionalServiceVersion) Parse(sid string, client api.Interface) (*fastly.Version, error) {
vs, err := client.ListVersions(context.TODO(), &fastly.ListVersionsInput{
ServiceID: sid,
})
if err != nil {
return nil, fmt.Errorf("error listing service versions: %w", err)
}
if len(vs) == 0 {
return nil, errors.New("error listing service versions: no versions available")
// When no --version flag is provided (WasSet=false), default to "active" to preserve
// the original behavior of trying active version first, with fallback to latest.
if sv.Value == "" && !sv.WasSet {
sv.Value = "active"
}

// Sort versions into descending order.
sort.Slice(vs, func(i, j int) bool {
return fastly.ToValue(vs[i].Number) > fastly.ToValue(vs[j].Number)
})

var v *fastly.Version
// When a specific numeric version is provided, use it directly.
if n, err := strconv.Atoi(sv.Value); err == nil {
return client.GetVersion(context.TODO(), &fastly.GetVersionInput{
ServiceID: sid,
ServiceVersion: n,
})
}

switch strings.ToLower(sv.Value) {
case "active":
serviceDetails, err := client.GetServiceDetails(context.TODO(), &fastly.GetServiceDetailsInput{
ServiceID: sid,
Filters: []fastly.ServiceDetailsFilter{
{Key: "versions.active", Value: true},
},
})
if err != nil {
return nil, fmt.Errorf("error getting service details: %w", err)
}
// If active version exists, return it
if serviceDetails.ActiveVersion != nil {
return serviceDetails.ActiveVersion, nil
}
// If flag was explicitly set to "active" but no active version exists, return error
if sv.WasSet {
return nil, fmt.Errorf("no active service version found")
}
// If flag was not explicitly set and there's no active version, fall through to latest
fallthrough
case "latest":
vs, err := client.ListVersions(context.TODO(), &fastly.ListVersionsInput{
Comment thread
anthony-gomez-fastly marked this conversation as resolved.
ServiceID: sid,
})
if err != nil {
return nil, fmt.Errorf("error listing service versions: %w", err)
}
if len(vs) == 0 {
return nil, errors.New("no service versions available")
}
// Sort versions into descending order to ensure we get the latest
sort.Slice(vs, func(i, j int) bool {
return fastly.ToValue(vs[i].Number) > fastly.ToValue(vs[j].Number)
})
return vs[0], nil
case "active":
v, err = GetActiveVersion(vs)
case "": // no --version flag provided
v, err = GetActiveVersion(vs)
case "staged":
serviceDetails, err := client.GetServiceDetails(context.TODO(), &fastly.GetServiceDetailsInput{
ServiceID: sid,
Filters: []fastly.ServiceDetailsFilter{
{Key: "versions.staged", Value: true},
},
})
if err != nil {
return vs[0], nil //lint:ignore nilerr if no active version, return latest version
return nil, fmt.Errorf("error getting service details: %w", err)
}
if serviceDetails.Version == nil {
return nil, fmt.Errorf("no staged service version found")
}
return serviceDetails.Version, nil
default:
v, err = GetSpecifiedVersion(vs, sv.Value)
return nil, fmt.Errorf("invalid version value %q: must be a version number, \"latest\", \"active\", or \"staged\"", sv.Value)
}
if err != nil {
return nil, err
}

return v, nil
}

// OptionalServiceNameID represents a mapping between a Fastly service name and
Expand Down Expand Up @@ -249,7 +288,8 @@ func (ac *OptionalAutoClone) Parse(v *fastly.Version, sid string, verbose bool,
Remediation: fsterr.AutoCloneRemediation,
}
}
if ac.Value && (v.Active != nil && *v.Active || v.Locked != nil && *v.Locked) {
stateUnknown := v.Active == nil && v.Locked == nil
if ac.Value && (stateUnknown || v.Active != nil && *v.Active || v.Locked != nil && *v.Locked) {
Comment thread
anthony-gomez-fastly marked this conversation as resolved.
version, err := client.CloneVersion(context.TODO(), &fastly.CloneVersionInput{
ServiceID: sid,
ServiceVersion: fastly.ToValue(v.Number),
Expand All @@ -269,32 +309,6 @@ func (ac *OptionalAutoClone) Parse(v *fastly.Version, sid string, verbose bool,
return v, nil
}

// GetActiveVersion returns the active service version.
func GetActiveVersion(vs []*fastly.Version) (*fastly.Version, error) {
for _, v := range vs {
if fastly.ToValue(v.Active) {
return v, nil
}
}
return nil, fmt.Errorf("no active service version found")
}

// GetSpecifiedVersion returns the specified service version.
func GetSpecifiedVersion(vs []*fastly.Version, version string) (*fastly.Version, error) {
i, err := strconv.Atoi(version)
if err != nil {
return nil, err
}

for _, v := range vs {
if fastly.ToValue(v.Number) == i {
return v, nil
}
}

return nil, fmt.Errorf("specified service version not found: %s", version)
}

// Content determines if the given flag value is a file path, and if so read
// the contents from disk, otherwise presume the given value is the content.
func Content(flagval string) string {
Expand Down
Loading
Loading