Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 79
CDTOOL-1328 list version refactor#1774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anthony-gomez-fastly
merged 6 commits into
main
from
rcaril/CDTOOL-1328-list-version-refactorMay 8, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c76483
modified flags.go parse function to no longer use list and instead us…
anthony-gomez-fastly eb112fa
change unset version to first try active, then return latest if no ac…
anthony-gomez-fastly 97fad1f
remove extra calls
anthony-gomez-fastly 63d3a0c
add staged version filter
anthony-gomez-fastly 224206d
added documentation for the same
anthony-gomez-fastly a545634
fix lint
anthony-gomez-fastly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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{ | ||
| 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 | ||
| @@ -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) { | ||
anthony-gomez-fastly marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| version, err := client.CloneVersion(context.TODO(), &fastly.CloneVersionInput{ | ||
| ServiceID: sid, | ||
| ServiceVersion: fastly.ToValue(v.Number), | ||
| @@ -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 { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.