From 64b6b1f3add0e3cba718c98a9d4854beba2eaa43 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Sun, 6 Mar 2022 22:22:50 -0800 Subject: [PATCH] bundler: Add flag to dump request data Add the `--dry-run` flag to see the exact HTTP request that sqlc will make to the the upload endpoint. --- internal/bundler/metadata.go | 3 --- internal/bundler/upload.go | 32 ++++++++++++++++++++++++++------ internal/cmd/cmd.go | 24 ++++++++++++++++++++++-- internal/cmd/package.go | 23 +++++------------------ 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/internal/bundler/metadata.go b/internal/bundler/metadata.go index 10ad8a91a3..415b131ef0 100644 --- a/internal/bundler/metadata.go +++ b/internal/bundler/metadata.go @@ -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 } diff --git a/internal/bundler/upload.go b/internal/bundler/upload.go index 899eb0e4d7..58d14ff954 100644 --- a/internal/bundler/upload.go +++ b/internal/bundler/upload.go @@ -7,6 +7,7 @@ import ( "io" "mime/multipart" "net/http" + "net/http/httputil" "os" "github.com/kyleconroy/sqlc/internal/config" @@ -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 { diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 9e0333bf7d..4c36e96b3e 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -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) @@ -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) { @@ -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", diff --git a/internal/cmd/package.go b/internal/cmd/package.go index 99c0595c52..63a4e00501 100644 --- a/internal/cmd/package.go +++ b/internal/cmd/package.go @@ -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 { @@ -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) + } }