Skip to content

Commit 6bb4d82

Browse files
committed
Improve rendering of the path in the success message
1 parent 658341d commit 6bb4d82

5 files changed

Lines changed: 89 additions & 2 deletions

File tree

internal/snapshot/destination.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ var (
1717
ErrUnknownScheme = errors.New("unrecognized destination scheme")
1818
)
1919

20+
// displayPath shortens abs for human-readable output:
21+
// under cwd → ./rel, under home → ~/rel, otherwise unchanged.
22+
func displayPath(abs, cwd, home string) string {
23+
if cwd != "" {
24+
if rel, err := filepath.Rel(cwd, abs); err == nil && !strings.HasPrefix(rel, "..") {
25+
return "./" + filepath.ToSlash(rel)
26+
}
27+
}
28+
if home != "" {
29+
if rel, err := filepath.Rel(home, abs); err == nil && !strings.HasPrefix(rel, "..") {
30+
return "~/" + filepath.ToSlash(rel)
31+
}
32+
}
33+
return abs
34+
}
35+
2036
// ParseDestination resolves the user-supplied path to an absolute local path.
2137
// When dest is empty, a default name based on now (UTC) is used, e.g.
2238
// "snapshot-2026-05-11T21-04-32-a3f.zip", saved in the current working directory.

internal/snapshot/destination_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,72 @@ import (
1616

1717

1818

19+
func TestDisplayPath(t *testing.T) {
20+
t.Parallel()
21+
22+
base := t.TempDir()
23+
cwd := filepath.Join(base, "projects", "lstk")
24+
home := filepath.Join(base, "home")
25+
26+
tests := []struct {
27+
name string
28+
abs string
29+
cwd string
30+
home string
31+
want string
32+
}{
33+
{
34+
name: "under cwd",
35+
abs: filepath.Join(cwd, "snap.zip"),
36+
cwd: cwd, home: home,
37+
want: "./snap.zip",
38+
},
39+
{
40+
name: "under cwd subdir",
41+
abs: filepath.Join(cwd, "exports", "snap.zip"),
42+
cwd: cwd, home: home,
43+
want: "./exports/snap.zip",
44+
},
45+
{
46+
name: "under home but not cwd",
47+
abs: filepath.Join(home, "snap.zip"),
48+
cwd: cwd, home: home,
49+
want: "~/snap.zip",
50+
},
51+
{
52+
name: "under home subdir",
53+
abs: filepath.Join(home, "downloads", "snap.zip"),
54+
cwd: cwd, home: home,
55+
want: "~/downloads/snap.zip",
56+
},
57+
{
58+
name: "unrelated to both",
59+
abs: filepath.Join(base, "other", "snap.zip"),
60+
cwd: cwd, home: home,
61+
want: filepath.Join(base, "other", "snap.zip"),
62+
},
63+
{
64+
name: "empty cwd falls back to home",
65+
abs: filepath.Join(home, "snap.zip"),
66+
cwd: "", home: home,
67+
want: "~/snap.zip",
68+
},
69+
{
70+
name: "empty cwd and home returns absolute",
71+
abs: filepath.Join(base, "snap.zip"),
72+
cwd: "", home: "",
73+
want: filepath.Join(base, "snap.zip"),
74+
},
75+
}
76+
77+
for _, tc := range tests {
78+
t.Run(tc.name, func(t *testing.T) {
79+
t.Parallel()
80+
assert.Equal(t, tc.want, snapshot.DisplayPath(tc.abs, tc.cwd, tc.home))
81+
})
82+
}
83+
}
84+
1985
func TestParseDestination(t *testing.T) {
2086
t.Parallel()
2187
wd, err := os.Getwd()

internal/snapshot/export_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package snapshot
2+
3+
var DisplayPath = displayPath

internal/snapshot/save.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func Save(ctx context.Context, rt runtime.Runtime, containers []config.Container
4444
defer func() {
4545
sink.Emit(output.SpinnerStop())
4646
if retErr == nil {
47-
sink.Emit(output.MessageEvent{Severity: output.SeveritySuccess, Text: fmt.Sprintf("Snapshot saved to %s", dest)})
47+
cwd, _ := os.Getwd()
48+
home, _ := os.UserHomeDir()
49+
sink.Emit(output.MessageEvent{Severity: output.SeveritySuccess, Text: fmt.Sprintf("Snapshot saved to %s", displayPath(dest, cwd, home))})
4850
}
4951
}()
5052

test/integration/snapshot_save_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestSnapshotSaveCustomPath(t *testing.T) {
9090
)
9191
require.NoError(t, err, "lstk snapshot save failed: %s", stderr)
9292
assert.Contains(t, stdout, "Snapshot saved")
93-
assert.Contains(t, stdout, outPath)
93+
assert.Contains(t, stdout, "./my-snap.zip")
9494

9595
data, err := os.ReadFile(outPath)
9696
require.NoError(t, err, "output file should exist")

0 commit comments

Comments
 (0)