Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation arg to contracts command #565

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading