Skip to content

Commit 64b6b1f

Browse files
committed
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.
1 parent 79fbf70 commit 64b6b1f

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

internal/bundler/metadata.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ package bundler
22

33
import (
44
"runtime"
5-
"time"
65

76
"github.com/kyleconroy/sqlc/internal/info"
87
)
98

109
func projectMetadata() ([][2]string, error) {
11-
now := time.Now().UTC()
1210
return [][2]string{
1311
{"sqlc_version", info.Version},
1412
{"go_version", runtime.Version()},
1513
{"goos", runtime.GOOS},
1614
{"goarch", runtime.GOARCH},
17-
{"created", now.Format(time.RFC3339)},
1815
}, nil
1916
}

internal/bundler/upload.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"mime/multipart"
99
"net/http"
10+
"net/http/httputil"
1011
"os"
1112

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

41-
func (up *Uploader) Upload(ctx context.Context, result map[string]string) error {
42+
func (up *Uploader) buildRequest(ctx context.Context, result map[string]string) (*http.Request, error) {
4243
if err := up.Validate(); err != nil {
43-
return err
44+
return nil, err
4445
}
4546
body := bytes.NewBuffer([]byte{})
4647

4748
w := multipart.NewWriter(body)
4849
defer w.Close()
4950
if err := writeInputs(w, up.configPath, up.config); err != nil {
50-
return err
51+
return nil, err
5152
}
5253
if err := writeOutputs(w, up.dir, result); err != nil {
53-
return err
54+
return nil, err
5455
}
5556
w.Close()
5657

5758
req, err := http.NewRequest("POST", "https://api.sqlc.dev/upload", body)
5859
if err != nil {
59-
return err
60+
return nil, err
6061
}
6162

6263
// Set sqlc-version header
6364
req.Header.Set("Content-Type", w.FormDataContentType())
6465
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", up.token))
65-
req = req.WithContext(ctx)
66+
return req.WithContext(ctx), nil
67+
}
6668

69+
func (up *Uploader) DumpRequestOut(ctx context.Context, result map[string]string) error {
70+
req, err := up.buildRequest(ctx, result)
71+
if err != nil {
72+
return err
73+
}
74+
dump, err := httputil.DumpRequest(req, true)
75+
if err != nil {
76+
return err
77+
}
78+
os.Stdout.Write(dump)
79+
return nil
80+
}
81+
82+
func (up *Uploader) Upload(ctx context.Context, result map[string]string) error {
83+
req, err := up.buildRequest(ctx, result)
84+
if err != nil {
85+
return err
86+
}
6787
client := &http.Client{}
6888
resp, err := client.Do(req)
6989
if err != nil {

internal/cmd/cmd.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
2929
rootCmd.AddCommand(genCmd)
3030
rootCmd.AddCommand(initCmd)
3131
rootCmd.AddCommand(versionCmd)
32-
rootCmd.AddCommand(packageCmd)
32+
uploadCmd.Flags().BoolP("dry-run", "", false, "dump upload request (default: false)")
33+
rootCmd.AddCommand(uploadCmd)
3334

3435
rootCmd.SetArgs(args)
3536
rootCmd.SetIn(stdin)
@@ -96,11 +97,16 @@ var initCmd = &cobra.Command{
9697

9798
type Env struct {
9899
ExperimentalFeatures bool
100+
DryRun bool
99101
}
100102

101103
func ParseEnv(c *cobra.Command) Env {
102104
x := c.Flag("experimental")
103-
return Env{ExperimentalFeatures: x != nil && x.Changed}
105+
dr := c.Flag("dry-run")
106+
return Env{
107+
ExperimentalFeatures: x != nil && x.Changed,
108+
DryRun: dr != nil && dr.Changed,
109+
}
104110
}
105111

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

161+
var uploadCmd = &cobra.Command{
162+
Use: "upload",
163+
Short: "Upload the schema, queries, and configuration for this project",
164+
RunE: func(cmd *cobra.Command, args []string) error {
165+
stderr := cmd.ErrOrStderr()
166+
dir, name := getConfigPath(stderr, cmd.Flag("file"))
167+
if err := createPkg(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
168+
fmt.Fprintf(stderr, "error uploading: %s\n", err)
169+
os.Exit(1)
170+
}
171+
return nil
172+
},
173+
}
174+
155175
var checkCmd = &cobra.Command{
156176
Use: "compile",
157177
Short: "Statically check SQL for syntax and type errors",

internal/cmd/package.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,12 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"io"
76
"os"
87

9-
"github.com/spf13/cobra"
10-
118
"github.com/kyleconroy/sqlc/internal/bundler"
129
)
1310

14-
var packageCmd = &cobra.Command{
15-
Use: "upload",
16-
Short: "Upload the schema, queries, and configuration for this project",
17-
RunE: func(cmd *cobra.Command, args []string) error {
18-
stderr := cmd.ErrOrStderr()
19-
dir, name := getConfigPath(stderr, cmd.Flag("file"))
20-
if err := createPkg(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
21-
fmt.Fprintf(stderr, "error uploading: %s\n", err)
22-
os.Exit(1)
23-
}
24-
return nil
25-
},
26-
}
27-
2811
func createPkg(ctx context.Context, e Env, dir, filename string, stderr io.Writer) error {
2912
configPath, conf, err := readConfig(stderr, dir, filename)
3013
if err != nil {
@@ -38,5 +21,9 @@ func createPkg(ctx context.Context, e Env, dir, filename string, stderr io.Write
3821
if err != nil {
3922
os.Exit(1)
4023
}
41-
return up.Upload(ctx, output)
24+
if e.DryRun {
25+
return up.DumpRequestOut(ctx, output)
26+
} else {
27+
return up.Upload(ctx, output)
28+
}
4229
}

0 commit comments

Comments
 (0)