4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
+ "io"
7
8
"mime/multipart"
8
9
"net/http"
9
10
"os"
@@ -12,13 +13,15 @@ import (
12
13
)
13
14
14
15
type Uploader struct {
16
+ token string
15
17
configPath string
16
18
config * config.Config
17
19
dir string
18
20
}
19
21
20
22
func NewUploader (configPath , dir string , conf * config.Config ) * Uploader {
21
23
return & Uploader {
24
+ token : os .Getenv ("SQLC_AUTH_TOKEN" ),
22
25
configPath : configPath ,
23
26
config : conf ,
24
27
dir : dir ,
@@ -27,7 +30,10 @@ func NewUploader(configPath, dir string, conf *config.Config) *Uploader {
27
30
28
31
func (up * Uploader ) Validate () error {
29
32
if up .config .Project .ID == "" {
30
- return fmt .Errorf ("project ID is not set" )
33
+ return fmt .Errorf ("project.id is not set" )
34
+ }
35
+ if up .token == "" {
36
+ return fmt .Errorf ("SQLC_AUTH_TOKEN environment variable is not set" )
31
37
}
32
38
return nil
33
39
}
@@ -40,24 +46,22 @@ func (up *Uploader) Upload(ctx context.Context, result map[string]string) error
40
46
41
47
w := multipart .NewWriter (body )
42
48
defer w .Close ()
43
-
44
49
if err := writeInputs (w , up .configPath , up .config ); err != nil {
45
50
return err
46
51
}
47
52
if err := writeOutputs (w , up .dir , result ); err != nil {
48
53
return err
49
54
}
50
-
51
55
w .Close ()
52
56
53
- req , err := http .NewRequest ("POST" , "http ://localhost:8090 /upload" , body )
57
+ req , err := http .NewRequest ("POST" , "https ://api.sqlc.dev /upload" , body )
54
58
if err != nil {
55
59
return err
56
60
}
57
61
58
62
// Set sqlc-version header
59
63
req .Header .Set ("Content-Type" , w .FormDataContentType ())
60
- req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , os . Getenv ( "SQLC_AUTH_TOKEN" ) ))
64
+ req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , up . token ))
61
65
req = req .WithContext (ctx )
62
66
63
67
client := & http.Client {}
@@ -66,7 +70,12 @@ func (up *Uploader) Upload(ctx context.Context, result map[string]string) error
66
70
return err
67
71
}
68
72
if resp .StatusCode >= 400 {
69
- return fmt .Errorf ("upload endpiont returned non-200 status code: %d" , resp .StatusCode )
73
+ body , err := io .ReadAll (resp .Body )
74
+ defer resp .Body .Close ()
75
+ if err != nil {
76
+ return fmt .Errorf ("upload error: endpoint returned non-200 status code: %d" , resp .StatusCode )
77
+ }
78
+ return fmt .Errorf ("upload error: %d: %s" , resp .StatusCode , string (body ))
70
79
}
71
80
return nil
72
81
}
0 commit comments