Skip to content

Commit

Permalink
bundler: Add flag to dump request data
Browse files Browse the repository at this point in the history
Add the `--dry-run` flag to see the exact HTTP request that sqlc will
make to the the upload endpoint.
  • Loading branch information
kyleconroy committed Mar 7, 2022
1 parent 79fbf70 commit 64b6b1f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
3 changes: 0 additions & 3 deletions internal/bundler/metadata.go
Expand Up @@ -2,18 +2,15 @@ package bundler

import (
"runtime"
"time"

"github.com/kyleconroy/sqlc/internal/info"
)

func projectMetadata() ([][2]string, error) {
now := time.Now().UTC()
return [][2]string{
{"sqlc_version", info.Version},
{"go_version", runtime.Version()},
{"goos", runtime.GOOS},
{"goarch", runtime.GOARCH},
{"created", now.Format(time.RFC3339)},
}, nil
}
32 changes: 26 additions & 6 deletions internal/bundler/upload.go
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/http/httputil"
"os"

"github.com/kyleconroy/sqlc/internal/config"
Expand Down Expand Up @@ -38,32 +39,51 @@ func (up *Uploader) Validate() error {
return nil
}

func (up *Uploader) Upload(ctx context.Context, result map[string]string) error {
func (up *Uploader) buildRequest(ctx context.Context, result map[string]string) (*http.Request, error) {
if err := up.Validate(); err != nil {
return err
return nil, err
}
body := bytes.NewBuffer([]byte{})

w := multipart.NewWriter(body)
defer w.Close()
if err := writeInputs(w, up.configPath, up.config); err != nil {
return err
return nil, err
}
if err := writeOutputs(w, up.dir, result); err != nil {
return err
return nil, err
}
w.Close()

req, err := http.NewRequest("POST", "https://api.sqlc.dev/upload", body)
if err != nil {
return err
return nil, err
}

// Set sqlc-version header
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", up.token))
req = req.WithContext(ctx)
return req.WithContext(ctx), nil
}

func (up *Uploader) DumpRequestOut(ctx context.Context, result map[string]string) error {
req, err := up.buildRequest(ctx, result)
if err != nil {
return err
}
dump, err := httputil.DumpRequest(req, true)
if err != nil {
return err
}
os.Stdout.Write(dump)
return nil
}

func (up *Uploader) Upload(ctx context.Context, result map[string]string) error {
req, err := up.buildRequest(ctx, result)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down
24 changes: 22 additions & 2 deletions internal/cmd/cmd.go
Expand Up @@ -29,7 +29,8 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
rootCmd.AddCommand(genCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(packageCmd)
uploadCmd.Flags().BoolP("dry-run", "", false, "dump upload request (default: false)")
rootCmd.AddCommand(uploadCmd)

rootCmd.SetArgs(args)
rootCmd.SetIn(stdin)
Expand Down Expand Up @@ -96,11 +97,16 @@ var initCmd = &cobra.Command{

type Env struct {
ExperimentalFeatures bool
DryRun bool
}

func ParseEnv(c *cobra.Command) Env {
x := c.Flag("experimental")
return Env{ExperimentalFeatures: x != nil && x.Changed}
dr := c.Flag("dry-run")
return Env{
ExperimentalFeatures: x != nil && x.Changed,
DryRun: dr != nil && dr.Changed,
}
}

func getConfigPath(stderr io.Writer, f *pflag.Flag) (string, string) {
Expand Down Expand Up @@ -152,6 +158,20 @@ var genCmd = &cobra.Command{
},
}

var uploadCmd = &cobra.Command{
Use: "upload",
Short: "Upload the schema, queries, and configuration for this project",
RunE: func(cmd *cobra.Command, args []string) error {
stderr := cmd.ErrOrStderr()
dir, name := getConfigPath(stderr, cmd.Flag("file"))
if err := createPkg(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
fmt.Fprintf(stderr, "error uploading: %s\n", err)
os.Exit(1)
}
return nil
},
}

var checkCmd = &cobra.Command{
Use: "compile",
Short: "Statically check SQL for syntax and type errors",
Expand Down
23 changes: 5 additions & 18 deletions internal/cmd/package.go
Expand Up @@ -2,29 +2,12 @@ package cmd

import (
"context"
"fmt"
"io"
"os"

"github.com/spf13/cobra"

"github.com/kyleconroy/sqlc/internal/bundler"
)

var packageCmd = &cobra.Command{
Use: "upload",
Short: "Upload the schema, queries, and configuration for this project",
RunE: func(cmd *cobra.Command, args []string) error {
stderr := cmd.ErrOrStderr()
dir, name := getConfigPath(stderr, cmd.Flag("file"))
if err := createPkg(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
fmt.Fprintf(stderr, "error uploading: %s\n", err)
os.Exit(1)
}
return nil
},
}

func createPkg(ctx context.Context, e Env, dir, filename string, stderr io.Writer) error {
configPath, conf, err := readConfig(stderr, dir, filename)
if err != nil {
Expand All @@ -38,5 +21,9 @@ func createPkg(ctx context.Context, e Env, dir, filename string, stderr io.Write
if err != nil {
os.Exit(1)
}
return up.Upload(ctx, output)
if e.DryRun {
return up.DumpRequestOut(ctx, output)
} else {
return up.Upload(ctx, output)
}
}

0 comments on commit 64b6b1f

Please sign in to comment.