-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathupload.go
More file actions
87 lines (78 loc) · 1.95 KB
/
upload.go
File metadata and controls
87 lines (78 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package bundler
import (
"context"
"fmt"
"os"
"google.golang.org/protobuf/encoding/protojson"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/info"
"github.com/sqlc-dev/sqlc/internal/quickdb"
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
)
type Uploader struct {
token string
configPath string
config *config.Config
dir string
client pb.QuickClient
}
func NewUploader(configPath, dir string, conf *config.Config) *Uploader {
return &Uploader{
token: os.Getenv("SQLC_AUTH_TOKEN"),
configPath: configPath,
config: conf,
dir: dir,
}
}
func (up *Uploader) Validate() error {
if up.config.Cloud.Project == "" {
return fmt.Errorf("cloud.project is not set")
}
if up.token == "" {
return fmt.Errorf("SQLC_AUTH_TOKEN environment variable is not set")
}
if up.client == nil {
client, err := quickdb.NewClientFromConfig(up.config.Cloud)
if err != nil {
return fmt.Errorf("client init failed: %w", err)
}
up.client = client
}
return nil
}
func (up *Uploader) buildRequest(ctx context.Context, result map[string]string) (*pb.UploadArchiveRequest, error) {
ins, err := readInputs(up.configPath, up.config)
if err != nil {
return nil, err
}
outs, err := readOutputs(up.dir, result)
if err != nil {
return nil, err
}
return &pb.UploadArchiveRequest{
SqlcVersion: info.Version,
Inputs: ins,
Outputs: outs,
}, nil
}
func (up *Uploader) DumpRequestOut(ctx context.Context, result map[string]string) error {
req, err := up.buildRequest(ctx, result)
if err != nil {
return err
}
fmt.Println(protojson.Format(req))
return nil
}
func (up *Uploader) Upload(ctx context.Context, result map[string]string) error {
if err := up.Validate(); err != nil {
return err
}
req, err := up.buildRequest(ctx, result)
if err != nil {
return err
}
if _, err := up.client.UploadArchive(ctx, req); err != nil {
return fmt.Errorf("upload error: %w", err)
}
return nil
}