From 77149f17e36362e5180ac283614421b9e9cfc3e9 Mon Sep 17 00:00:00 2001 From: zhijie-yang Date: Tue, 24 Sep 2024 14:40:51 +0200 Subject: [PATCH] Fix CLI Client Handling Non-Interactive Terminal and Error Message for 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 * 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 * fix: use rocks-toolbox for mock-rock --------- Co-authored-by: Linostar Co-authored-by: Cristovao Cordeiro --- tools/cli-client/internals/cli/cli_main.go | 2 +- .../cli-client/internals/trigger/build_metadata.go | 14 +++++++++----- .../internals/trigger/build_metadata_test.go | 8 ++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/cli-client/internals/cli/cli_main.go b/tools/cli-client/internals/cli/cli_main.go index 5ebe3304..d6db002e 100644 --- a/tools/cli-client/internals/cli/cli_main.go +++ b/tools/cli-client/internals/cli/cli_main.go @@ -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 diff --git a/tools/cli-client/internals/trigger/build_metadata.go b/tools/cli-client/internals/trigger/build_metadata.go index 02044f20..50cec384 100644 --- a/tools/cli-client/internals/trigger/build_metadata.go +++ b/tools/cli-client/internals/trigger/build_metadata.go @@ -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) diff --git a/tools/cli-client/internals/trigger/build_metadata_test.go b/tools/cli-client/internals/trigger/build_metadata_test.go index 427a9ec8..f228eb23 100644 --- a/tools/cli-client/internals/trigger/build_metadata_test.go +++ b/tools/cli-client/internals/trigger/build_metadata_test.go @@ -39,7 +39,7 @@ 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 { @@ -47,16 +47,16 @@ func (s *BuildMetadataSuite) TestGetBuildMetadataCustomDirector(c *C) { 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,