Skip to content

feat(cmd/vet): Simplify environment variable substiution #2393

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

Merged
merged 1 commit into from
Jun 29, 2023
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
6 changes: 3 additions & 3 deletions examples/authors/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"queries": "postgresql/query.sql",
"engine": "postgresql",
"database": {
"url": "'postgresql://%s:%s@%s:%s/authors'.format([env.PG_USER, env.PG_PASSWORD, env.PG_HOST, env.PG_PORT])"
"url": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/authors"
},
"gen": {
"go": {
Expand All @@ -20,7 +20,7 @@
"queries": "mysql/query.sql",
"engine": "mysql",
"database": {
"url": "'root:%s@tcp(%s:%s)/authors?multiStatements=true&parseTime=true'.format([env.MYSQL_ROOT_PASSWORD, env.MYSQL_HOST, env.MYSQL_PORT])"
"url": "root:${MYSQL_ROOT_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/authors?multiStatements=true&parseTime=true"
},
"gen": {
"go": {
Expand All @@ -34,7 +34,7 @@
"queries": "sqlite/query.sql",
"engine": "sqlite",
"database": {
"url": "'file:authors?mode=memory&cache=shared'"
"url": "file:authors?mode=memory&cache=shared"
},
"gen": {
"go": {
Expand Down
2 changes: 1 addition & 1 deletion examples/batch/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"queries": "postgresql/query.sql",
"engine": "postgresql",
"database": {
"url": "'postgresql://%s:%s@%s:%s/batch'.format([env.PG_USER, env.PG_PASSWORD, env.PG_HOST, env.PG_PORT])"
"url": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/batch"
},
"sql_package": "pgx/v4",
"emit_json_tags": true,
Expand Down
6 changes: 3 additions & 3 deletions examples/booktest/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"queries": "postgresql/query.sql",
"engine": "postgresql",
"database": {
"url": "'postgresql://%s:%s@%s:%s/booktest'.format([env.PG_USER, env.PG_PASSWORD, env.PG_HOST, env.PG_PORT])"
"url": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/booktest"
}
},
{
Expand All @@ -18,7 +18,7 @@
"queries": "mysql/query.sql",
"engine": "mysql",
"database": {
"url": "'root:%s@tcp(%s:%s)/booktest?multiStatements=true&parseTime=true'.format([env.MYSQL_ROOT_PASSWORD, env.MYSQL_HOST, env.MYSQL_PORT])"
"url": "root:${MYSQL_ROOT_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/booktest?multiStatements=true&parseTime=true"
}
},
{
Expand All @@ -28,7 +28,7 @@
"queries": "sqlite/query.sql",
"engine": "sqlite",
"database": {
"url": "'file:booktest?mode=memory&cache=shared'"
"url": "file:booktest?mode=memory&cache=shared"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion examples/jets/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"queries": "postgresql/query-building.sql",
"engine": "postgresql",
"database": {
"url": "'postgresql://%s:%s@%s:%s/jets'.format([env.PG_USER, env.PG_PASSWORD, env.PG_HOST, env.PG_PORT])"
"url": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/jets"
}
}
]
Expand Down
6 changes: 3 additions & 3 deletions examples/ondeck/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"queries": "postgresql/query",
"engine": "postgresql",
"database": {
"url": "'postgresql://%s:%s@%s:%s/ondeck'.format([env.PG_USER, env.PG_PASSWORD, env.PG_HOST, env.PG_PORT])"
"url": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/ondeck"
},
"emit_json_tags": true,
"emit_prepared_queries": true,
Expand All @@ -21,7 +21,7 @@
"queries": "mysql/query",
"engine": "mysql",
"database": {
"url": "'root:%s@tcp(%s:%s)/ondeck?multiStatements=true&parseTime=true'.format([env.MYSQL_ROOT_PASSWORD, env.MYSQL_HOST, env.MYSQL_PORT])"
"url": "root:${MYSQL_ROOT_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/ondeck?multiStatements=true&parseTime=true"
},
"emit_json_tags": true,
"emit_prepared_queries": true,
Expand All @@ -34,7 +34,7 @@
"queries": "sqlite/query",
"engine": "sqlite",
"database": {
"url": "'file:ondeck?mode=memory&cache=shared'"
"url": "file:ondeck?mode=memory&cache=shared"
},
"emit_json_tags": true,
"emit_prepared_queries": true,
Expand Down
39 changes: 4 additions & 35 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/opts"
"github.com/kyleconroy/sqlc/internal/plugin"
"github.com/kyleconroy/sqlc/internal/shfmt"
"github.com/kyleconroy/sqlc/internal/sql/ast"
)

Expand Down Expand Up @@ -107,21 +108,9 @@ func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) err
msgs[c.Name] = c.Msg
}

dbenv, err := cel.NewEnv(
cel.StdLib(),
ext.Strings(ext.StringsVersion(1)),
cel.Variable("env",
cel.MapType(cel.StringType, cel.StringType),
),
)
if err != nil {
return fmt.Errorf("new dbenv; %s", err)
}

c := checker{
Checks: checks,
Conf: conf,
Dbenv: dbenv,
Dir: dir,
Env: env,
Envmap: map[string]string{},
Expand Down Expand Up @@ -197,41 +186,22 @@ func (p *dbPreparer) Prepare(ctx context.Context, name, query string) error {
type checker struct {
Checks map[string]cel.Program
Conf *config.Config
Dbenv *cel.Env
Dir string
Env *cel.Env
Envmap map[string]string
Msgs map[string]string
Stderr io.Writer
}

func (c *checker) DSN(expr string) (string, error) {
ast, issues := c.Dbenv.Compile(expr)
if issues != nil && issues.Err() != nil {
return "", fmt.Errorf("type-check error: database url %s", issues.Err())
}
prg, err := c.Dbenv.Program(ast)
if err != nil {
return "", fmt.Errorf("program construction error: database url %s", err)
}
func (c *checker) DSN(dsn string) (string, error) {
// Populate the environment variable map if it is empty
if len(c.Envmap) == 0 {
for _, e := range os.Environ() {
k, v, _ := strings.Cut(e, "=")
c.Envmap[k] = v
}
}
out, _, err := prg.Eval(map[string]any{
"env": c.Envmap,
})
if err != nil {
return "", fmt.Errorf("expression error: %s", err)
}
dsn, ok := out.Value().(string)
if !ok {
return "", fmt.Errorf("expression returned non-string value: %v", out.Value())
}
return dsn, nil
return shfmt.Replace(dsn, c.Envmap), nil
}

func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
Expand Down Expand Up @@ -312,9 +282,8 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
if prep != nil && prepareable(s, original.RawStmt) {
name := fmt.Sprintf("sqlc_vet_%d_%d", time.Now().Unix(), i)
if err := prep.Prepare(ctx, name, query.Text); err != nil {
fmt.Fprintf(c.Stderr, "%s: error preparing %s: %s\n", query.Filename, query.Name, err)
fmt.Fprintf(c.Stderr, "%s: error preparing %s on %s: %s\n", query.Filename, query.Name, s.Engine, err)
errored = true
continue
}
}
q := vetQuery(query)
Expand Down
16 changes: 16 additions & 0 deletions internal/shfmt/shfmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package shfmt

import (
"regexp"
"strings"
)

var pat = regexp.MustCompile(`\$\{[A-Z_]+\}`)

func Replace(f string, vars map[string]string) string {
return pat.ReplaceAllStringFunc(f, func(s string) string {
s = strings.TrimPrefix(s, "${")
s = strings.TrimSuffix(s, "}")
return vars[s]
})
}
17 changes: 17 additions & 0 deletions internal/shfmt/shfmt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package shfmt

import "testing"

func TestReplace(t *testing.T) {
s := "POSTGRES_SQL://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/AUTHORS"
env := map[string]string{
"PG_USER": "user",
"PG_PASSWORD": "password",
"PG_HOST": "host",
"PG_PORT": "port",
}
e := "POSTGRES_SQL://user:password@host:port/AUTHORS"
if v := Replace(s, env); v != e {
t.Errorf("%s != %s", v, e)
}
}