-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds ssh support, refactors regex matchers, tests
- Loading branch information
1 parent
c9cb496
commit c82780d
Showing
14 changed files
with
322 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
github_key: | ||
github_username: | ||
git_user: | ||
git_key: | ||
git_ssh_key_path: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,17 @@ Copyright © 2024 Agastya Dev Addepally ([email protected]) | |
package cmd | ||
|
||
import ( | ||
"github.com/schollz/progressbar/v3" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"io/fs" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/schollz/progressbar/v3" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// checkForUpdatesCmd represents the checkForUpdates command | ||
|
@@ -123,8 +124,8 @@ func init() { | |
checkForUpdatesCmd.Flags().StringArrayP("ignore", "i", []string{".git", ".idea"}, "Directories to ignore when searching for the One Ring(modules and their sources.") | ||
checkForUpdatesCmd.Flags().StringP("output", "o", "csv", "Output format. Supports \"csv\" and \"json\". Default value is csv.") | ||
checkForUpdatesCmd.Flags().StringP("output-filename", "f", "module_report", "Output file name.") | ||
checkForUpdatesCmd.Flags().Bool("ci", false, "Set this flag for usage in CI systems. Does not generate a report. Prints JSON to Stdout and returns exit code 1 if modules are outdated.") | ||
checkForUpdatesCmd.Flags().Bool("allow-failure", true, "Set this flag for usage in CI systems. If true, does NOT exit code 1 when modules are outdated.") | ||
//checkForUpdatesCmd.Flags().Bool("ci", false, "Set this flag for usage in CI systems. Does not generate a report. Prints JSON to Stdout and returns exit code 1 if modules are outdated.") | ||
//checkForUpdatesCmd.Flags().Bool("allow-failure", true, "Set this flag for usage in CI systems. If true, does NOT exit code 1 when modules are outdated.") | ||
|
||
err := checkForUpdatesCmd.MarkFlagRequired("path") | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package errorHandlers | ||
|
||
const CheckOutputFormatError = "output format not supported. Please use csv or json" | ||
const CloningErrorPrefix = "unable to clone repo" | ||
const CloningErrorPrefix = "unable to clone repo " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"log/slog" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadDirectoryWithSlash(t *testing.T) { | ||
|
@@ -14,19 +16,47 @@ func TestReadDirectoryWithoutSlash(t *testing.T) { | |
assert.Equal(t, "./test_dir", fixTrailingSlashForPath("./test_dir"), "no slash at the end") | ||
} | ||
|
||
func TestGetNamedMatchesForRegex(t *testing.T) { | ||
submoduleRegex = regexp.MustCompile("(?P<base_url>.*/.*)//(?P<submodule>.*)") | ||
|
||
//nonSubmoduleMatch := getNamedMatchesForRegex(submoduleRegex, "github.com/hashicorp/example?ref=1.0.0") | ||
submoduleMatch := getNamedMatchesForRegex(submoduleRegex, "github.com/hashicorp/example//test_1/test_2?ref=1.0.0") | ||
doubleSlashSubmoduleMatch := getNamedMatchesForRegex(submoduleRegex, "https://github.com/hashicorp/example//test_1/test_2?ref=1.0.0") | ||
|
||
// assert.NotEmpty(t, nonSubmoduleMatch, "readFiles_test :: getNamedMatchesForRegex :: github.com/hashicorp/example?ref=1.0.0 was not matched for baseurl") | ||
// assert.Equal(t, "github.com/hashicorp/example?ref=1.0.0", nonSubmoduleMatch["base_url"], "readFiles_test :: getNamedMatchesForRegex :: unable to extract base url") | ||
// assert.Empty(t, nonSubmoduleMatch["submodule"], "readFiles_test :: getNamedMatchesForRegex :: incorrectly extracts submodule") | ||
|
||
assert.NotEmpty(t, submoduleMatch, "readFiles_test :: getNamedMatchesForRegex :: github.com/hashicorp/example//test_1/test_2?ref=1.0.0 was not matched for baseurl") | ||
assert.Equal(t, "github.com/hashicorp/example", submoduleMatch["base_url"], "readFiles_test :: getNamedMatchesForRegex :: unable to extract base url") | ||
assert.Equal(t, "test_1/test_2?ref=1.0.0", submoduleMatch["submodule"], "readFiles_test :: getNamedMatchesForRegex :: unable to extract submodule") | ||
|
||
assert.NotEmpty(t, doubleSlashSubmoduleMatch, "readFiles_test :: getNamedMatchesForRegex :: https://github.com/hashicorp/example//test_1/test_2?ref=1.0.0 was not matched for baseurl") | ||
assert.Equal(t, "https://github.com/hashicorp/example", doubleSlashSubmoduleMatch["base_url"], "readFiles_test :: getNamedMatchesForRegex :: unable to extract base url") | ||
assert.Equal(t, "test_1/test_2?ref=1.0.0", doubleSlashSubmoduleMatch["submodule"], "readFiles_test :: getNamedMatchesForRegex :: unable to extract submodule") | ||
|
||
} | ||
|
||
func TestGetTagFromUrl(t *testing.T) { | ||
assert.Equal(t, "1.0.0", getTagFromUrl("github.com/hashicorp/example?ref=1.0.0"), "ref param not extracted") | ||
assert.Empty(t, getTagFromUrl("github.com/hashicorp/example.git"), "ref param found") | ||
assert.Equal(t, "1.0.0", getTagFromUrl("github.com/hashicorp/example?ref=1.0.0"), "ref param not extracted for github.com/hashicorp/example?ref=1.0.0") | ||
assert.Equal(t, "1.0.0", getTagFromUrl("[email protected]:hashicorp/example?ref=1.0.0"), "ref param not extracted for [email protected]:hashicorp/example?ref=1.0.0") | ||
assert.Empty(t, getTagFromUrl("github.com/hashicorp/example.git"), "ref param found for github.com/hashicorp/example.git") | ||
assert.Empty(t, getTagFromUrl("github.com/hashicorp/example?depth=1"), "ref param found") | ||
assert.Empty(t, getTagFromUrl("https://github.com/hashicorp/example?depth=1"), "ref param found") | ||
assert.Equal(t, "2.0.0", getTagFromUrl("https://github.com/hashicorp/example?ref=2.0.0"), "ref param not found") | ||
assert.Equal(t, "1.0.1", getTagFromUrl("https://github.com/hashicorp/example?depth=&ref=1.0.1"), "ref param found") | ||
|
||
assert.Equal(t, "2.0.0", getTagFromUrl("https://github.com/hashicorp/example?ref=2.0.0"), "ref param not found for https://github.com/hashicorp/example?ref=2.0.0") | ||
} | ||
func TestExtractRefAndPathRepo(t *testing.T) { | ||
gitHubExampleRepo, _, _ := extractRefAndPath("github.com/hashicorp/example?ref=1.0.0") | ||
assert.Equal(t, "github.com/hashicorp/example", gitHubExampleRepo, "readFiles :: extractRefAndPath :: repo :: "+gitHubExampleRepo) | ||
|
||
// gitHubSSHExampleRepo, gitHubSSHExampleTag := extractRefAndPath("[email protected]:hashicorp/example.git") | ||
//assert.Equal(t, "[email protected]:hashicorp/example.git", gitHubSSHExampleRepo, "readFiles :: extractRefAndPath :: repo :: "+gitHubSSHExampleRepo+" :: "+gitHubSSHExampleTag) | ||
gitHubSSHExampleRepo, _, _ := extractRefAndPath("[email protected]:hashicorp/example.git") | ||
assert.Equal(t, "[email protected]:hashicorp/example.git", gitHubSSHExampleRepo, "readFiles :: extractRefAndPath :: repo :: "+gitHubSSHExampleRepo) | ||
|
||
gitHubSSHExampleRepoWithRef, _, _ := extractRefAndPath("[email protected]:hashicorp/example.git?ref=test") | ||
assert.Equal(t, "[email protected]:hashicorp/example.git", gitHubSSHExampleRepoWithRef, "readFiles :: extractRefAndPath :: repo :: "+gitHubSSHExampleRepo) | ||
|
||
bitbucketExampleRepo, _, _ := extractRefAndPath("bitbucket.org/hashicorp/terraform-consul-aws?ref=1.0.0&test=woho") | ||
assert.Equal(t, "bitbucket.org/hashicorp/terraform-consul-aws", bitbucketExampleRepo, "readFiles :: extractRefAndPath :: repo :: "+bitbucketExampleRepo) | ||
|
||
|
@@ -42,6 +72,8 @@ func TestExtractRefAndPathTag(t *testing.T) { | |
_, gitHubExampleTag, _ := extractRefAndPath("github.com/hashicorp/example?ref=1.0.0") | ||
assert.Equal(t, "1.0.0", gitHubExampleTag, "readFiles :: extractRefAndPath :: tag :: "+gitHubExampleTag) | ||
|
||
//_, gitHubSSHExampleTag, _ := extractRefAndPath("[email protected]:hashicorp/example.git") | ||
|
||
_, gitHubExampleNoTag, _ := extractRefAndPath("github.com/hashicorp/example") | ||
assert.Equal(t, "", gitHubExampleNoTag, "readFiles :: extractRefAndPath :: no tag :: ") | ||
|
||
|
@@ -58,6 +90,10 @@ func TestExtractRefAndPathSubmodule(t *testing.T) { | |
assert.Equal(t, "", gitHubExampleSubmodule, "readFiles :: extractRefAndPath :: submodule :: "+gitHubExampleSubmodule) | ||
assert.Equal(t, "github.com/hashicorp/example", gitHubExample, "readFiles :: extractRefAndPath :: repo :: "+gitHubExample) | ||
|
||
gitHubSSHExample, _, gitHubSSHExampleSubmodule := extractRefAndPath("[email protected]:hashicorp/example//module/test") | ||
assert.Equal(t, "module/test", gitHubSSHExampleSubmodule, "readFiles :: extractRefAndPath :: submodule :: "+gitHubSSHExampleSubmodule) | ||
assert.Equal(t, "[email protected]:hashicorp/example", gitHubSSHExample, "readFiles :: extractRefAndPath :: repo :: "+gitHubSSHExample) | ||
|
||
gitHubExampleRepo, _, gitHubExampleRepoSubmodule := extractRefAndPath("https://github.com/org/repo//submodules/folder?ref=1.1.1\n") | ||
assert.Equal(t, "submodules/folder", gitHubExampleRepoSubmodule, "readFiles :: extractRefAndPath :: submodule :: "+gitHubExampleRepoSubmodule) | ||
assert.Equal(t, "https://github.com/org/repo", gitHubExampleRepo, "readFiles :: extractRefAndPath :: repo :: "+gitHubExampleRepo) | ||
|
@@ -68,20 +104,27 @@ func TestExtractRefAndPathSubmodule(t *testing.T) { | |
|
||
} | ||
func TestExtractModuleSource(t *testing.T) { | ||
nonGitSource := extractModuleSource("source=\"Terraform-VMWare-Modules/vm/vsphere\"") | ||
assert.Empty(t, nonGitSource, "readFiles :: extractRefAndPath :: repo :: non git terraform source extracted incorrectly") | ||
|
||
gitHubExampleSource := extractModuleSource("source=\"github.com/hashicorp/example?ref=1.0.0\"") | ||
assert.Equal(t, "github.com/hashicorp/example?ref=1.0.0", gitHubExampleSource, "readFiles :: extractRefAndPath :: repo :: "+gitHubExampleSource) | ||
// gitHubSSHExampleSource := extractModuleSource("source=\"[email protected]:hashicorp/example.git") | ||
//assert.Equal(t, "[email protected]:hashicorp/example.git", gitHubSSHExampleSource, "readFiles :: extractRefAndPath :: repo :: "+gitHubSSHExampleSource+" :: "+gitHubSSHExampleTag) | ||
|
||
gitHubSSHExampleSource := extractModuleSource("source=\"[email protected]:hashicorp/example.git\"") | ||
assert.Equal(t, "[email protected]:hashicorp/example.git", gitHubSSHExampleSource, "readFiles :: extractRefAndPath :: repo :: "+gitHubSSHExampleSource) | ||
|
||
gitGitHubExampleSource := extractModuleSource("source=\"git::https://github.com/test_repo_labala?ref=1.3.1\"") | ||
assert.Equal(t, "https://github.com/test_repo_labala?ref=1.3.1", gitGitHubExampleSource, "readFiles :: extractRefAndPath :: repo :: "+gitGitHubExampleSource) | ||
|
||
bitbucketExampleSource := extractModuleSource("source=\"bitbucket.org/hashicorp/terraform-consul-aws?ref=1.0.0&test=woho\"") | ||
assert.Equal(t, "bitbucket.org/hashicorp/terraform-consul-aws?ref=1.0.0&test=woho", bitbucketExampleSource, "readFiles :: extractRefAndPath :: repo :: "+bitbucketExampleSource) | ||
|
||
genericGitExampleSource := extractModuleSource("source=\"https://example.com/vpc.git?ref=1.1.0&test=woho\"") | ||
assert.Equal(t, "https://example.com/vpc.git?ref=1.1.0&test=woho", genericGitExampleSource, "readFiles :: extractRefAndPath :: repo :: "+genericGitExampleSource) | ||
|
||
gitParamExampleSource := extractModuleSource("source=\"https://example.com/vpc.git?depth=1&ref=v1.2.0\"") | ||
assert.Equal(t, "https://example.com/vpc.git?depth=1&ref=v1.2.0", gitParamExampleSource, "readFiles :: extractRefAndPath :: repo :: "+gitParamExampleSource) | ||
|
||
slog.Debug("repos and refs", gitHubExampleSource, bitbucketExampleSource, genericGitExampleSource, gitParamExampleSource) | ||
} | ||
|
||
|
@@ -103,5 +146,3 @@ func TestExtractSubmoduleFromSource(t *testing.T) { | |
assert.Equal(t, "submodule/folder1/folder2", submoduleWithDepths) | ||
|
||
} | ||
|
||
//gitHubSSHExampleTag, gitHubSSHExampleRepo, |
Oops, something went wrong.