Skip to content

Commit ac73f34

Browse files
committed
Absolute path and different error
1 parent 6a24cdf commit ac73f34

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

internal/snapshot/destination.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"path/filepath"
78
"strings"
89
)
910

@@ -16,16 +17,25 @@ type Destination interface {
1617
// ParseDestination returns a Destination for the user-supplied path, or an error for cloud/bare names.
1718
func ParseDestination(dest string) (Destination, error) {
1819
if dest == "" {
19-
return LocalDestination{Path: "ls-state-export"}, nil
20-
}
21-
if strings.Contains(dest, "://") {
20+
dest = "ls-state-export"
21+
} else if strings.Contains(dest, "://") {
22+
return nil, fmt.Errorf("cloud destinations are not yet supported — use a file path like ./my-snapshot")
23+
} else if !strings.HasPrefix(dest, ".") && !strings.HasPrefix(dest, "/") && !strings.HasPrefix(dest, "~") && !strings.Contains(dest, "/") {
24+
// bare name with no path separators: reserved for future cloud pod names
2225
return nil, fmt.Errorf("cloud destinations are not yet supported — use a file path like ./my-snapshot")
2326
}
24-
if strings.HasPrefix(dest, ".") || strings.HasPrefix(dest, "/") || strings.HasPrefix(dest, "~") || strings.Contains(dest, "/") {
25-
return LocalDestination{Path: dest}, nil
27+
if strings.HasPrefix(dest, "~") {
28+
home, err := os.UserHomeDir()
29+
if err != nil {
30+
return nil, fmt.Errorf("resolve home directory: %w", err)
31+
}
32+
dest = home + dest[1:]
33+
}
34+
abs, err := filepath.Abs(dest)
35+
if err != nil {
36+
return nil, fmt.Errorf("resolve path: %w", err)
2637
}
27-
// bare name with no path separators: reserved for future cloud pod names
28-
return nil, fmt.Errorf("cloud destinations are not yet supported — use a file path like ./my-snapshot")
38+
return LocalDestination{Path: abs}, nil
2939
}
3040

3141
// LocalDestination writes snapshot state to a local file.

internal/snapshot/destination_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package snapshot_test
22

33
import (
4+
"os"
5+
"path/filepath"
46
"testing"
57

68
"github.com/localstack/lstk/internal/snapshot"
@@ -9,30 +11,35 @@ import (
911
)
1012

1113
func TestParseDestination(t *testing.T) {
14+
wd, err := os.Getwd()
15+
require.NoError(t, err)
16+
home, err := os.UserHomeDir()
17+
require.NoError(t, err)
18+
1219
tests := []struct {
13-
input string
14-
want snapshot.Destination
15-
wantErr string
20+
input string
21+
wantPath string
22+
wantErr string
1623
}{
1724
{
18-
input: "",
19-
want: snapshot.LocalDestination{Path: "ls-state-export"},
25+
input: "",
26+
wantPath: filepath.Join(wd, "ls-state-export"),
2027
},
2128
{
22-
input: "./my-state",
23-
want: snapshot.LocalDestination{Path: "./my-state"},
29+
input: "./my-state",
30+
wantPath: filepath.Join(wd, "my-state"),
2431
},
2532
{
26-
input: "/tmp/state",
27-
want: snapshot.LocalDestination{Path: "/tmp/state"},
33+
input: "/tmp/state",
34+
wantPath: "/tmp/state",
2835
},
2936
{
30-
input: "~/snapshots/s",
31-
want: snapshot.LocalDestination{Path: "~/snapshots/s"},
37+
input: "~/snapshots/s",
38+
wantPath: filepath.Join(home, "snapshots/s"),
3239
},
3340
{
34-
input: "subdir/state",
35-
want: snapshot.LocalDestination{Path: "subdir/state"},
41+
input: "subdir/state",
42+
wantPath: filepath.Join(wd, "subdir/state"),
3643
},
3744
{
3845
input: "my-pod",
@@ -58,7 +65,7 @@ func TestParseDestination(t *testing.T) {
5865
return
5966
}
6067
require.NoError(t, err)
61-
assert.Equal(t, tc.want, got)
68+
assert.Equal(t, snapshot.LocalDestination{Path: tc.wantPath}, got)
6269
})
6370
}
6471
}

internal/snapshot/save.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Save(ctx context.Context, rt runtime.Runtime, containers []config.Container
4545
w, err := dest.Writer()
4646
if err != nil {
4747
output.EmitSpinnerStop(sink)
48-
return fmt.Errorf("open destination %s: %w", dest, err)
48+
return fmt.Errorf("save to %s: %w", dest, err)
4949
}
5050

5151
if _, err := io.Copy(w, body); err != nil {

internal/snapshot/save_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func TestSave_DestinationDirNotExist(t *testing.T) {
153153

154154
err := snapshot.Save(context.Background(), healthyRunningMock(t), awsContainers, exporter, dest, sink)
155155
require.Error(t, err)
156-
assert.Contains(t, err.Error(), "open destination")
156+
assert.Contains(t, err.Error(), "save to")
157157
}
158158

159159
func TestSave_OverwritesExistingFile(t *testing.T) {

0 commit comments

Comments
 (0)