From 718af601b9e675bed1a35cc270cbd0e5b711682d Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Fri, 4 Oct 2024 17:57:02 -0400 Subject: [PATCH] Add validation arg to contracts command Will allow contract testing to be done in just one command --- cmd/command/pr/pr.go | 22 ++++++++++++++++++++++ pkg/utils/git/repo.go | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/command/pr/pr.go b/cmd/command/pr/pr.go index 830a3268..adc2be1e 100644 --- a/cmd/command/pr/pr.go +++ b/cmd/command/pr/pr.go @@ -1,6 +1,7 @@ package pr import ( + "fmt" "io" "os" @@ -8,6 +9,7 @@ import ( "github.com/pluralsh/plural-cli/pkg/common" "github.com/pluralsh/plural-cli/pkg/pr" "github.com/pluralsh/plural-cli/pkg/utils" + "github.com/pluralsh/plural-cli/pkg/utils/git" "github.com/samber/lo" "github.com/urfave/cli" ) @@ -107,6 +109,10 @@ func (p *Plural) prCommands() []cli.Command { Usage: "the contract file to run", Required: true, }, + cli.BoolFlag{ + Name: "validate", + Usage: "check if there are any local git changes and fail if so", + }, }, }, } @@ -171,6 +177,22 @@ func handlePrContracts(c *cli.Context) error { } } + if c.Bool("validate") { + changes, err := git.Modified() + if err != nil { + return err + } + + if len(changes) > 0 { + utils.Highlight("Contracts failed due to local git changes, displaying below ===>\n\n") + if err := git.PrintDiff(); err != nil { + return err + } + fmt.Println("") + return fmt.Errorf("contract validation failed") + } + } + return nil } diff --git a/pkg/utils/git/repo.go b/pkg/utils/git/repo.go index 316ac6dc..3422e4d1 100644 --- a/pkg/utils/git/repo.go +++ b/pkg/utils/git/repo.go @@ -2,6 +2,8 @@ package git import ( "bufio" + "os" + "os/exec" "strings" gogit "github.com/go-git/go-git/v5" @@ -20,6 +22,13 @@ func Repo() (*gogit.Repository, error) { return gogit.PlainOpen(root) } +func PrintDiff() error { + cmd := exec.Command("git", "--no-pager", "diff") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + func CurrentBranch() (string, error) { return GitRaw("rev-parse", "--abbrev-ref", "HEAD") }