Skip to content

Commit

Permalink
feat(push): Add tag support (#3074)
Browse files Browse the repository at this point in the history
Also support passing a tag to verify
  • Loading branch information
kyleconroy authored Dec 20, 2023
1 parent ce95162 commit e1309fb
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 94 deletions.
13 changes: 7 additions & 6 deletions internal/bundler/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ func annotate() map[string]string {
return labels
}

func BuildRequest(ctx context.Context, dir, configPath string, results []*QuerySetArchive) (*pb.UploadArchiveRequest, error) {
func BuildRequest(ctx context.Context, dir, configPath string, results []*QuerySetArchive, tags []string) (*pb.UploadArchiveRequest, error) {
conf, err := readFile(dir, configPath)
if err != nil {
return nil, err
}
res := &pb.UploadArchiveRequest{
SqlcVersion: info.Version,
Config: conf,
Tags: tags,
Annotations: annotate(),
}
for i, result := range results {
Expand Down Expand Up @@ -126,12 +127,12 @@ func BuildRequest(ctx context.Context, dir, configPath string, results []*QueryS
return res, nil
}

func (up *Uploader) buildRequest(ctx context.Context, results []*QuerySetArchive) (*pb.UploadArchiveRequest, error) {
return BuildRequest(ctx, up.dir, up.configPath, results)
func (up *Uploader) buildRequest(ctx context.Context, results []*QuerySetArchive, tags []string) (*pb.UploadArchiveRequest, error) {
return BuildRequest(ctx, up.dir, up.configPath, results, tags)
}

func (up *Uploader) DumpRequestOut(ctx context.Context, result []*QuerySetArchive) error {
req, err := up.buildRequest(ctx, result)
req, err := up.buildRequest(ctx, result, []string{})
if err != nil {
return err
}
Expand All @@ -148,11 +149,11 @@ func (up *Uploader) DumpRequestOut(ctx context.Context, result []*QuerySetArchiv
return nil
}

func (up *Uploader) Upload(ctx context.Context, result []*QuerySetArchive) error {
func (up *Uploader) Upload(ctx context.Context, result []*QuerySetArchive, tags []string) error {
if err := up.Validate(); err != nil {
return err
}
req, err := up.buildRequest(ctx, result)
req, err := up.buildRequest(ctx, result, tags)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Options struct {
Env Env
Stderr io.Writer
MutateConfig func(*config.Config)
// TODO: Move these to a command-specific struct
Tags []string
Against string
}

func (o *Options) ReadConfig(dir, filename string) (string, *config.Config, error) {
Expand Down
11 changes: 10 additions & 1 deletion internal/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ import (
"github.com/sqlc-dev/sqlc/internal/config"
)

func init() {
pushCmd.Flags().StringSliceP("tag", "t", nil, "tag this push with a value")
}

var pushCmd = &cobra.Command{
Use: "push",
Aliases: []string{"upload"},
Short: "Push 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"))
tags, err := cmd.Flags().GetStringSlice("tag")
if err != nil {
return err
}
opts := &Options{
Env: ParseEnv(cmd),
Stderr: stderr,
Tags: tags,
}
if err := Push(cmd.Context(), dir, name, opts); err != nil {
fmt.Fprintf(stderr, "error pushing: %s\n", err)
Expand Down Expand Up @@ -78,6 +87,6 @@ func Push(ctx context.Context, dir, filename string, opts *Options) error {
if e.DryRun {
return up.DumpRequestOut(ctx, p.results)
} else {
return up.Upload(ctx, p.results)
return up.Upload(ctx, p.results, opts.Tags)
}
}
16 changes: 13 additions & 3 deletions internal/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ import (
quickdbv1 "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
)

func init() {
verifyCmd.Flags().String("against", "", "compare against this tag")
}

var verifyCmd = &cobra.Command{
Use: "verify",
Short: "Verify 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"))
against, err := cmd.Flags().GetString("against")
if err != nil {
return err
}
opts := &Options{
Env: ParseEnv(cmd),
Stderr: stderr,
Env: ParseEnv(cmd),
Stderr: stderr,
Against: against,
}
if err := Verify(cmd.Context(), dir, name, opts); err != nil {
fmt.Fprintf(stderr, "error verifying: %s\n", err)
Expand All @@ -44,7 +53,7 @@ func Verify(ctx context.Context, dir, filename string, opts *Options) error {
if err := Process(ctx, p, dir, filename, opts); err != nil {
return err
}
req, err := bundler.BuildRequest(ctx, dir, configPath, p.results)
req, err := bundler.BuildRequest(ctx, dir, configPath, p.results, nil)
if err != nil {
return err
}
Expand All @@ -56,6 +65,7 @@ func Verify(ctx context.Context, dir, filename string, opts *Options) error {
}

resp, err := client.VerifyQuerySets(ctx, &quickdbv1.VerifyQuerySetsRequest{
Against: opts.Against,
SqlcVersion: req.SqlcVersion,
QuerySets: req.QuerySets,
Config: req.Config,
Expand Down
Loading

0 comments on commit e1309fb

Please sign in to comment.