-
Notifications
You must be signed in to change notification settings - Fork 6
Replace the local snapshot extension .zip to .snapshot #304
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
Open
anisaoshafi
wants to merge
2
commits into
main
Choose a base branch
from
devx-848-replace-snapshot-file-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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 |
|---|---|---|
|
|
@@ -23,6 +23,20 @@ var ( | |
| validPodName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9-]*$`) | ||
| ) | ||
|
|
||
| const ( | ||
| snapshotExt = ".snapshot" // user-facing extension for local snapshots | ||
| legacySnapshotExt = ".zip" // accepted on load for backward compatibility | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: Thanks for adding this! 🙏 |
||
| ) | ||
|
|
||
| // withSnapshotExt forces the .snapshot extension, replacing any other the user gave. | ||
| func withSnapshotExt(path string) string { | ||
| ext := filepath.Ext(path) | ||
| if strings.EqualFold(ext, snapshotExt) { | ||
| return path | ||
| } | ||
| return strings.TrimSuffix(path, ext) + snapshotExt | ||
| } | ||
|
|
||
| // DestinationKind distinguishes local file paths from remote pod destinations. | ||
| type DestinationKind int | ||
|
|
||
|
|
@@ -32,7 +46,7 @@ const ( | |
| ) | ||
|
|
||
| // Destination is the parsed result of a user-supplied snapshot destination. | ||
| // For KindLocal, Value is an absolute local file path with a .zip extension. | ||
| // For KindLocal, Value is an absolute local file path with a .snapshot extension. | ||
| // For KindPod, Value is the validated pod name (without the "pod:" prefix). | ||
| type Destination struct { | ||
| Kind DestinationKind | ||
|
|
@@ -46,9 +60,7 @@ func ParseRemovable(ref, cwd, home string) (Destination, error) { | |
| lower := strings.ToLower(ref) | ||
| if !strings.HasPrefix(lower, "pod:") && !strings.Contains(lower, "://") { | ||
| abs, _ := filepath.Abs(ref) | ||
| if !strings.EqualFold(filepath.Ext(abs), ".zip") { | ||
| abs += ".zip" | ||
| } | ||
| abs = withSnapshotExt(abs) | ||
| return Destination{}, fmt.Errorf("'%s' resolves to a local file (%s); CLI cannot delete local files", ref, displayPath(abs, cwd, home)) | ||
| } | ||
| return ParseSource(ref, home) | ||
|
|
@@ -72,7 +84,8 @@ func displayPath(abs, cwd, home string) string { | |
|
|
||
| // ParseSource resolves a user-supplied source REF for loading a snapshot. | ||
| // Unlike ParseDestination it never auto-generates a name: REF is required. | ||
| // For local paths, the file must exist; if no extension is given, .zip is tried as a fallback. | ||
| // For local paths, the file must exist; if no matching file is found, .snapshot and | ||
| // then .zip (legacy) are tried as fallbacks. | ||
| // home is used to expand a leading "~" or "~/"; pass "" to disable tilde expansion. | ||
| func ParseSource(ref, home string) (Destination, error) { | ||
| if ref == "" { | ||
|
|
@@ -110,25 +123,27 @@ func ParseSource(ref, home string) (Destination, error) { | |
| return Destination{}, fmt.Errorf("resolve path: %w", err) | ||
| } | ||
|
|
||
| // Try the path as-is first, then with .zip appended as a fallback for bare | ||
| // names (e.g. "my-snapshot" → "my-snapshot.zip" since that is what save produces). | ||
| // Try the path as-is first, then with .snapshot appended as a fallback for bare | ||
| // names (e.g. "my-snapshot" → "my-snapshot.snapshot" since that is what save | ||
| // produces), and finally .zip for snapshots saved by older lstk versions. | ||
| resolved, err := resolveSourcePath(abs) | ||
| if err != nil { | ||
| return Destination{}, err | ||
| } | ||
| return Destination{Kind: KindLocal, Value: resolved}, nil | ||
| } | ||
|
|
||
| // resolveSourcePath returns the first existing path among: abs as-is, then abs+".zip". | ||
| // resolveSourcePath returns the first existing path among: abs as-is, then | ||
| // abs+".snapshot", then abs+".zip" (legacy). | ||
| func resolveSourcePath(abs string) (string, error) { | ||
| if _, err := os.Stat(abs); err == nil { | ||
| return abs, nil | ||
| } | ||
| withZip := abs + ".zip" | ||
| if _, err := os.Stat(withZip); err == nil { | ||
| return withZip, nil | ||
| withSnapshot := abs + snapshotExt | ||
| withZip := abs + legacySnapshotExt | ||
| for _, candidate := range []string{abs, withSnapshot, withZip} { | ||
| if _, err := os.Stat(candidate); err == nil { | ||
| return candidate, nil | ||
| } | ||
| } | ||
| return "", fmt.Errorf("snapshot file not found: %q (also tried %q)", abs, withZip) | ||
| return "", fmt.Errorf("snapshot file not found: %q (also tried %q and %q)", abs, withSnapshot, withZip) | ||
| } | ||
|
|
||
| // ParseDestination resolves a user-supplied destination to a local path (KindLocal) or validated pod name (KindPod). | ||
|
|
@@ -187,9 +202,7 @@ func ParseDestination(dest, home string, now time.Time) (Destination, error) { | |
| return Destination{}, fmt.Errorf("%q is a directory — specify a file path like ./my-snapshot", abs) | ||
| } | ||
|
|
||
| if !strings.EqualFold(filepath.Ext(abs), ".zip") { | ||
| abs += ".zip" | ||
| } | ||
| abs = withSnapshotExt(abs) | ||
|
|
||
| return Destination{Kind: KindLocal, Value: abs}, nil | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Couldn't find a better line to add this comment, but loading an invalid file returns an error. Could we update that and possibly remove the file extension reference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, will change 🙏🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, this error was returned from emulator in this line, we only prefix it with

load failed for service:.I modified it to look like this. let me know if it needs more tweaking. thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, this matches the error pattern and style we introduced for other cases, too. 💯