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

Fix CLI Client Handling Non-Interactive Terminal and Error Message for Non-Canonical Repo #247

Merged
merged 6 commits into from
Sep 24, 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
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]
zhijie-yang marked this conversation as resolved.
Show resolved Hide resolved
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
Loading