Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BREAKING]Set path of temporary generated schema.json (instead of JSON string) to TBLS_SCHEMA #236

Merged
merged 3 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test_spanner:
test_ext_subcommand: build
@echo hello | env PATH="./testdata/bin:${PATH}" ./tbls echo | grep 'STDIN=hello' > /dev/null
@env PATH="./testdata/bin:${PATH}" ./tbls echo -c ./testdata/ext_subcommand_tbls.yml | grep 'TBLS_DSN=pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable' > /dev/null
@env PATH="./testdata/bin:${PATH}" ./tbls echo -c ./testdata/ext_subcommand_tbls.yml | grep 'TBLS_SCHEMA={' > /dev/null
@env PATH="./testdata/bin:${PATH}" ./tbls echo -c ./testdata/ext_subcommand_tbls.yml | grep 'TBLS_SCHEMA=/' > /dev/null
@env PATH="./testdata/bin:${PATH}" ./tbls echo -c ./testdata/ext_subcommand_tbls.yml | grep 'TBLS_CONFIG_PATH=' | grep 'testdata/ext_subcommand_tbls.yml' > /dev/null
@env PATH="./testdata/bin:${PATH}" TBLS_DSN=pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable ./tbls echo | grep 'TBLS_DSN=pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable' > /dev/null
@echo hello | env PATH="./testdata/bin:${PATH}" ./tbls echo -c ./testdata/ext_subcommand_tbls.yml | grep 'STDIN=hello' > /dev/null
Expand Down
13 changes: 9 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -131,12 +131,17 @@ var rootCmd = &cobra.Command{
envs = append(envs, fmt.Sprintf("TBLS_DSN=%s", cfg.DSN.URL))
envs = append(envs, fmt.Sprintf("TBLS_CONFIG_PATH=%s", cfg.Path))
o := json.New(true)
buf := new(bytes.Buffer)
if err := o.OutputSchema(buf, s); err != nil {
tmpfile, err := ioutil.TempFile("", "TBLS_SCHEMA")
if err != nil {
printError(err)
os.Exit(1)
}
defer os.Remove(tmpfile.Name())
if err := o.OutputSchema(tmpfile, s); err != nil {
printError(err)
os.Exit(1)
}
envs = append(envs, fmt.Sprintf("TBLS_SCHEMA=%s", buf.String()))
envs = append(envs, fmt.Sprintf("TBLS_SCHEMA=%s", tmpfile.Name()))
}

c := exec.Command(path, args...) // #nosec
Expand Down
21 changes: 18 additions & 3 deletions datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -144,10 +146,23 @@ func AnalyzeJSON(urlstr string) (*schema.Schema, error) {
return s, nil
}

// AnalyzeJSONString analyze JSON string
// Deprecated
func AnalyzeJSONString(str string) (*schema.Schema, error) {
s := &schema.Schema{}
buf := bytes.NewBufferString(str)
return AnalyzeJSONStringOrFile(str)
}

// AnalyzeJSONStringOrFile analyze JSON string or JSON file
func AnalyzeJSONStringOrFile(strOrPath string) (s *schema.Schema, err error) {
s = &schema.Schema{}
var buf io.Reader
if strings.HasPrefix(strOrPath, "{") {
buf = bytes.NewBufferString(strOrPath)
} else {
buf, err = os.Open(filepath.Clean(strOrPath))
if err != nil {
return s, errors.WithStack(err)
}
}
dec := json.NewDecoder(buf)
if err := dec.Decode(s); err != nil {
return s, errors.WithStack(err)
Expand Down