Skip to content

Commit

Permalink
Add validation arg to contracts command
Browse files Browse the repository at this point in the history
Will allow contract testing to be done in just one command
  • Loading branch information
michaeljguarino committed Oct 4, 2024
1 parent 7817475 commit 718af60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/command/pr/pr.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package pr

import (
"fmt"
"io"
"os"

"github.com/pluralsh/plural-cli/pkg/client"
"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"
)
Expand Down Expand Up @@ -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",
},
},
},
}
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package git

import (
"bufio"
"os"
"os/exec"
"strings"

gogit "github.com/go-git/go-git/v5"
Expand All @@ -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")
}
Expand Down

0 comments on commit 718af60

Please sign in to comment.