Skip to content

Commit d477b9f

Browse files
committed
feat(push): Include different information when uploading archives
1 parent d1bbf82 commit d477b9f

File tree

9 files changed

+785
-469
lines changed

9 files changed

+785
-469
lines changed

internal/bundler/multipart.go

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,37 @@ import (
44
"os"
55
"path/filepath"
66

7-
"github.com/sqlc-dev/sqlc/internal/config"
8-
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
7+
"github.com/sqlc-dev/sqlc/internal/plugin"
98
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
109
)
1110

12-
func readInputs(file string, conf *config.Config) ([]*pb.File, error) {
13-
refs := map[string]struct{}{}
14-
refs[filepath.Base(file)] = struct{}{}
15-
16-
for _, pkg := range conf.SQL {
17-
for _, paths := range []config.Paths{pkg.Schema, pkg.Queries} {
18-
files, err := sqlpath.Glob(paths)
19-
if err != nil {
20-
return nil, err
21-
}
22-
for _, file := range files {
23-
refs[file] = struct{}{}
24-
}
25-
}
11+
func readFiles(dir string, paths []string) ([]*plugin.File, error) {
12+
files, err := sqlpath.Glob(paths)
13+
if err != nil {
14+
return nil, err
2615
}
27-
28-
var files []*pb.File
29-
for file, _ := range refs {
30-
contents, err := os.ReadFile(file)
16+
var out []*plugin.File
17+
for _, file := range files {
18+
f, err := readFile(dir, file)
3119
if err != nil {
3220
return nil, err
3321
}
34-
files = append(files, &pb.File{
35-
Name: file,
36-
Contents: contents,
37-
})
22+
out = append(out, f)
3823
}
39-
return files, nil
24+
return out, nil
4025
}
4126

42-
func readOutputs(dir string, output map[string]string) ([]*pb.File, error) {
43-
var files []*pb.File
44-
for filename, contents := range output {
45-
rel, err := filepath.Rel(dir, filename)
46-
if err != nil {
47-
return nil, err
48-
}
49-
files = append(files, &pb.File{
50-
Name: rel,
51-
Contents: []byte(contents),
52-
})
27+
func readFile(dir string, path string) (*plugin.File, error) {
28+
rel, err := filepath.Rel(dir, path)
29+
if err != nil {
30+
return nil, err
31+
}
32+
blob, err := os.ReadFile(path)
33+
if err != nil {
34+
return nil, err
5335
}
54-
return files, nil
36+
return &plugin.File{
37+
Name: rel,
38+
Contents: blob,
39+
}, nil
5540
}

internal/bundler/upload.go

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
8+
"strings"
79

810
"google.golang.org/protobuf/encoding/protojson"
911

1012
"github.com/sqlc-dev/sqlc/internal/config"
1113
"github.com/sqlc-dev/sqlc/internal/info"
14+
"github.com/sqlc-dev/sqlc/internal/plugin"
1215
"github.com/sqlc-dev/sqlc/internal/quickdb"
1316
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
1417
)
@@ -33,6 +36,12 @@ type Uploader struct {
3336
client pb.QuickClient
3437
}
3538

39+
type QuerySetArchive struct {
40+
Queries []string
41+
Schema []string
42+
Request *plugin.GenerateRequest
43+
}
44+
3645
func NewUploader(configPath, dir string, conf *config.Config) *Uploader {
3746
return &Uploader{
3847
configPath: configPath,
@@ -58,23 +67,52 @@ func (up *Uploader) Validate() error {
5867
return nil
5968
}
6069

61-
func (up *Uploader) buildRequest(ctx context.Context, result map[string]string) (*pb.UploadArchiveRequest, error) {
62-
ins, err := readInputs(up.configPath, up.config)
63-
if err != nil {
64-
return nil, err
70+
var envvars = []string{
71+
"GITHUB_REPOSITORY",
72+
"GITHUB_REF",
73+
"GITHUB_REF_NAME",
74+
"GITHUB_REF_TYPE",
75+
"GITHUB_SHA",
76+
}
77+
78+
func annotate() map[string]string {
79+
labels := map[string]string{}
80+
for _, ev := range envvars {
81+
key := strings.ReplaceAll(strings.ToLower(ev), "_", ".")
82+
labels[key] = os.Getenv(ev)
6583
}
66-
outs, err := readOutputs(up.dir, result)
84+
return labels
85+
}
86+
87+
func (up *Uploader) buildRequest(ctx context.Context, results []*QuerySetArchive) (*plugin.UploadArchiveRequest, error) {
88+
conf, err := readFile(up.dir, up.configPath)
6789
if err != nil {
6890
return nil, err
6991
}
70-
return &pb.UploadArchiveRequest{
92+
res := &plugin.UploadArchiveRequest{
7193
SqlcVersion: info.Version,
72-
Inputs: ins,
73-
Outputs: outs,
74-
}, nil
94+
Config: conf,
95+
Annotations: annotate(),
96+
}
97+
for _, result := range results {
98+
schema, err := readFiles(up.dir, result.Schema)
99+
if err != nil {
100+
return nil, err
101+
}
102+
queries, err := readFiles(up.dir, result.Queries)
103+
if err != nil {
104+
return nil, err
105+
}
106+
res.Archives = append(res.Archives, &plugin.QuerySetArchive{
107+
Schema: schema,
108+
Queries: queries,
109+
Request: result.Request,
110+
})
111+
}
112+
return res, nil
75113
}
76114

77-
func (up *Uploader) DumpRequestOut(ctx context.Context, result map[string]string) error {
115+
func (up *Uploader) DumpRequestOut(ctx context.Context, result []*QuerySetArchive) error {
78116
req, err := up.buildRequest(ctx, result)
79117
if err != nil {
80118
return err
@@ -83,16 +121,17 @@ func (up *Uploader) DumpRequestOut(ctx context.Context, result map[string]string
83121
return nil
84122
}
85123

86-
func (up *Uploader) Upload(ctx context.Context, result map[string]string) error {
124+
func (up *Uploader) Upload(ctx context.Context, result []*QuerySetArchive) error {
87125
if err := up.Validate(); err != nil {
88126
return err
89127
}
90128
req, err := up.buildRequest(ctx, result)
91129
if err != nil {
92130
return err
93131
}
94-
if _, err := up.client.UploadArchive(ctx, req); err != nil {
95-
return fmt.Errorf("upload error: %w", err)
96-
}
132+
fmt.Println(protojson.Format(req))
133+
// if _, err := up.client.UploadArchive(ctx, req); err != nil {
134+
// return fmt.Errorf("upload error: %w", err)
135+
// }
97136
return nil
98137
}

internal/cmd/cmd.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func init() {
2828
createDBCmd.Flags().StringP("queryset", "", "", "name of the queryset to use")
29-
uploadCmd.Flags().BoolP("dry-run", "", false, "dump upload request (default: false)")
29+
pushCmd.Flags().BoolP("dry-run", "", false, "dump push request (default: false)")
3030
initCmd.Flags().BoolP("v1", "", false, "generate v1 config yaml file")
3131
initCmd.Flags().BoolP("v2", "", true, "generate v2 config yaml file")
3232
initCmd.MarkFlagsMutuallyExclusive("v1", "v2")
@@ -46,7 +46,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
4646
rootCmd.AddCommand(genCmd)
4747
rootCmd.AddCommand(initCmd)
4848
rootCmd.AddCommand(versionCmd)
49-
rootCmd.AddCommand(uploadCmd)
49+
rootCmd.AddCommand(pushCmd)
5050
rootCmd.AddCommand(NewCmdVet())
5151

5252
rootCmd.SetArgs(args)
@@ -214,18 +214,19 @@ var genCmd = &cobra.Command{
214214
},
215215
}
216216

217-
var uploadCmd = &cobra.Command{
218-
Use: "upload",
219-
Short: "Upload the schema, queries, and configuration for this project",
217+
var pushCmd = &cobra.Command{
218+
Use: "push",
219+
Aliases: []string{"upload"},
220+
Short: "Push the schema, queries, and configuration to your project",
220221
RunE: func(cmd *cobra.Command, args []string) error {
221222
stderr := cmd.ErrOrStderr()
222223
dir, name := getConfigPath(stderr, cmd.Flag("file"))
223224
opts := &Options{
224225
Env: ParseEnv(cmd),
225226
Stderr: stderr,
226227
}
227-
if err := createPkg(cmd.Context(), dir, name, opts); err != nil {
228-
fmt.Fprintf(stderr, "error uploading: %s\n", err)
228+
if err := Push(cmd.Context(), dir, name, opts); err != nil {
229+
fmt.Fprintf(stderr, "error pushing: %s\n", err)
229230
os.Exit(1)
230231
}
231232
return nil

0 commit comments

Comments
 (0)