Skip to content

Commit

Permalink
Decode contents by default in workspace export command (#531)
Browse files Browse the repository at this point in the history
## Changes

Also see #525.

The direct download flag has been removed in newer versions because of
the content type issue.

Instead, we can make the command decode the base64 output when the
output mode is text.

```
$ databricks workspace export /some/path/script.sh
#!/bin/bash
echo "this is a script"
```

## Tests

New integration test.
  • Loading branch information
pietern authored Jun 27, 2023
1 parent 54148ff commit f64c442
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/workspace/workspace/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ func init() {
{{header "ID"}} {{header "Type"}} {{header "Language"}} {{header "Path"}}
{{range .}}{{green "%d" .ObjectId}} {{blue "%s" .ObjectType}} {{cyan "%s" .Language}} {{.Path|cyan}}
{{end}}`)

// The export command prints the contents of the file to stdout by default.
exportCmd.Annotations["template"] = `{{.Content | b64_decode}}`
}
21 changes: 21 additions & 0 deletions internal/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -39,6 +40,26 @@ func TestWorkpaceGetStatusErrorWhenNoArguments(t *testing.T) {
assert.Equal(t, "accepts 1 arg(s), received 0", err.Error())
}

func TestWorkpaceExportPrintsContents(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))

ctx := context.Background()
w := databricks.Must(databricks.NewWorkspaceClient())
tmpdir := temporaryWorkspaceDir(t, w)
f, err := filer.NewWorkspaceFilesClient(w, tmpdir)
require.NoError(t, err)

// Write file to workspace
contents := "#!/usr/bin/bash\necho hello, world\n"
err = f.Write(ctx, "file-a", strings.NewReader(contents))
require.NoError(t, err)

// Run export
stdout, stderr := RequireSuccessfulRun(t, "workspace", "export", path.Join(tmpdir, "file-a"))
assert.Equal(t, contents, stdout.String())
assert.Equal(t, "", stderr.String())
}

func setupWorkspaceImportExportTest(t *testing.T) (context.Context, filer.Filer, string) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))

Expand Down
23 changes: 23 additions & 0 deletions libs/cmdio/render.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmdio

import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"strings"
Expand Down Expand Up @@ -93,6 +95,27 @@ func renderTemplate(w io.Writer, tmpl string, v any) error {
"pretty_date": func(t time.Time) string {
return t.Format("2006-01-02T15:04:05Z")
},
"b64_encode": func(in string) (string, error) {
var out bytes.Buffer
enc := base64.NewEncoder(base64.StdEncoding, &out)
_, err := enc.Write([]byte(in))
if err != nil {
return "", err
}
err = enc.Close()
if err != nil {
return "", err
}
return out.String(), nil
},
"b64_decode": func(in string) (string, error) {
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(in))
out, err := io.ReadAll(dec)
if err != nil {
return "", err
}
return string(out), nil
},
}).Parse(tmpl)
if err != nil {
return err
Expand Down

0 comments on commit f64c442

Please sign in to comment.