Skip to content

Commit

Permalink
Fix CLI Client Handling Non-Interactive Terminal and Error Message fo…
Browse files Browse the repository at this point in the history
…r Non-Canonical Repo (#247)

* fix: handle interactive terminal and non canonical repo

* Update tools/cli-client/internals/cli/cli_main.go

Co-authored-by: Linostar <[email protected]>

* feat: not enforce .git suffix and enfore match group length

* feat: update regex to filter .git out from match item 1

* Update tools/cli-client/internals/trigger/build_metadata.go

Co-authored-by: Cristovao Cordeiro <[email protected]>

* fix: use rocks-toolbox for mock-rock

---------

Co-authored-by: Linostar <[email protected]>
Co-authored-by: Cristovao Cordeiro <[email protected]>
  • Loading branch information
3 people authored Sep 24, 2024
1 parent 5631445 commit 77149f1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tools/cli-client/internals/cli/cli_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func addHelp(p *flags.Parser) {
func blockForConfirm(s string) error {
// check if is a tty
fi, err := os.Stdin.Stat()
if err != nil || fi.Mode()&os.ModeNamedPipe == 0 {
if err != nil && fi.Mode()&os.ModeNamedPipe != 0 {
return fmt.Errorf("non-interactive terminal detected, run with -y option")
} else if err != nil {
return err
Expand Down
14 changes: 9 additions & 5 deletions tools/cli-client/internals/trigger/build_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,36 @@ func InferBuildMetadata() BuildMetadata {
repo, err := git.PlainOpenWithOptions(".",
&git.PlainOpenOptions{DetectDotGit: true, EnableDotGitCommonDir: false})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open repository: %v", err)
fmt.Fprintf(os.Stderr, "Unable to open repository: %v\n", err)
os.Exit(1)
}

// find the source URL
remotes, err := repo.Remotes()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to obtain remotes: %v", err)
fmt.Fprintf(os.Stderr, "Unable to obtain remotes: %v\n", err)
os.Exit(1)
}
if len(remotes) < 1 || len(remotes[0].Config().URLs) < 1 {
fmt.Fprintf(os.Stderr, "No valid remote exists for this repo")
fmt.Fprintf(os.Stderr, "No valid remote exists for this repo\n")
os.Exit(1)
}
remoteURL := remotes[0].Config().URLs[0]
logger.Debugf("Remote URL: %s", remoteURL)

// use regex to match the repo location
regex := regexp.MustCompile("github.com[:/](canonical/.*).git")
regex := regexp.MustCompile(`github.com[:\/](canonical\/[A-Za-z0-9_-]*)(\.git)?`)
matches := regex.FindStringSubmatch(remoteURL)
if len(matches) < 3 {
fmt.Fprintf(os.Stderr, "oci-factory must be called in a git local repository belonging to the organization [canonical]\n")
os.Exit(1)
}
source := matches[1]
logger.Debugf("Source: %s", source)

headSha256, err := repo.ResolveRevision("HEAD")
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to resolve HEAD: %v", err)
fmt.Fprintf(os.Stderr, "Unable to resolve HEAD: %v\n", err)
os.Exit(1)
}
logger.Debugf("HEAD SHA-256: %s", headSha256)
Expand Down
8 changes: 4 additions & 4 deletions tools/cli-client/internals/trigger/build_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ func (s *BuildMetadataSuite) TestGetBuildMetadataCustomDirector(c *C) {
c.Fatal("git not installed")
}
repoPath := filepath.Join(s.dir, "tester-path")
cmd = exec.Command("git", "clone", "https://github.com/canonical/oci-factory.git", repoPath)
cmd = exec.Command("git", "clone", "https://github.com/canonical/rocks-toolbox.git", repoPath)
var errBuf bytes.Buffer
cmd.Stderr = &errBuf
if err := cmd.Run(); err != nil {
c.Logf("stderr: %s", errBuf.String())
c.Fatal(err)
}

err := os.Chdir(filepath.Join(repoPath, "examples", "mock-rock", "1.0"))
err := os.Chdir(filepath.Join(repoPath, "mock_rock", "1.0"))
c.Assert(err, IsNil)

result := trigger.InferBuildMetadata()

prefix := filepath.Join("examples", "mock-rock", "1.0") + "/"
prefix := filepath.Join("mock_rock", "1.0") + "/"
head, err := exec.Command("git", "rev-parse", "HEAD").Output()
headStr := strings.TrimSpace(string(head))
c.Assert(err, IsNil)
source := "canonical/oci-factory"
source := "canonical/rocks-toolbox"
expected := trigger.BuildMetadata{
Source: source,
Directory: prefix,
Expand Down

0 comments on commit 77149f1

Please sign in to comment.