-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added top level repository pkg
Added top level repository package, along with docs usage, and example client.
- Loading branch information
Showing
22 changed files
with
718 additions
and
196 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module example.com/client | ||
|
||
go 1.22.2 | ||
|
||
replace github.com/retr0h/git-url-parse => ../../../git-url-parse/ | ||
|
||
require ( | ||
github.com/lmittmann/tint v1.0.4 | ||
github.com/retr0h/git-url-parse v0.0.0-00010101000000-000000000000 | ||
) | ||
|
||
require github.com/chainguard-dev/git-urls v1.0.2 // indirect |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/chainguard-dev/git-urls v1.0.2 h1:pSpT7ifrpc5X55n4aTTm7FFUE+ZQHKiqpiwNkJrVcKQ= | ||
github.com/chainguard-dev/git-urls v1.0.2/go.mod h1:rbGgj10OS7UgZlbzdUQIQpT0k/D4+An04HJY7Ol+Y/o= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc= | ||
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"time" | ||
|
||
"github.com/lmittmann/tint" | ||
|
||
"github.com/retr0h/git-url-parse/pkg" | ||
"github.com/retr0h/git-url-parse/pkg/repository" | ||
) | ||
|
||
type repositoryManager interface { | ||
RegisterParser(url string) error | ||
Parse() (pkg.RepositoryManager, error) | ||
} | ||
|
||
func getLogger(debug bool) *slog.Logger { | ||
logLevel := slog.LevelInfo | ||
if debug { | ||
logLevel = slog.LevelDebug | ||
} | ||
|
||
logger := slog.New( | ||
tint.NewHandler(os.Stderr, &tint.Options{ | ||
Level: logLevel, | ||
TimeFormat: time.Kitchen, | ||
}), | ||
) | ||
|
||
return logger | ||
} | ||
|
||
func main() { | ||
debug := true | ||
logger := getLogger(debug) | ||
var r repositoryManager = repository.New(logger) | ||
|
||
r.RegisterParser("https://github.com/retr0h/foo") | ||
repo, err := r.Parse() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logger.Info(repo.GetProtocol()) | ||
logger.Info(repo.GetResourceName()) | ||
logger.Info(repo.GetHREF()) | ||
logger.Info(repo.GetOwnerName()) | ||
logger.Info(repo.GetRepoName()) | ||
logger.Info(repo.GetPath()) | ||
logger.Info(repo.GetBranchName()) | ||
logger.Info(repo.GetProviderName()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2024 John Dewey | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to | ||
// deal in the Software without restriction, including without limitation the | ||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
package internal | ||
|
||
import ( | ||
"github.com/retr0h/git-url-parse/pkg/api" | ||
) | ||
|
||
// ParserManager manager responsible for each Repository parsing operations. | ||
type ParserManager interface { | ||
IsGitHub(host string) bool | ||
Parse(url string) (*api.Repository, error) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2024 John Dewey | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to | ||
// deal in the Software without restriction, including without limitation the | ||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
package github | ||
|
||
import ( | ||
"log/slog" | ||
) | ||
|
||
const ( | ||
defaultHost string = "github.com" | ||
rawHost string = "raw.githubusercontent.com" | ||
wwwHost string = "www.github.com" | ||
) | ||
|
||
// New factory to create a new GitHub instance. | ||
func New( | ||
logger *slog.Logger, | ||
) *GitHub { | ||
return &GitHub{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
// IsGitHub determine if the provided URL belongs to GitHub. | ||
func (gh *GitHub) IsGitHub(host string) bool { | ||
return host == defaultHost || host == rawHost || host == wwwHost | ||
} |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2024 John Dewey | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to | ||
// deal in the Software without restriction, including without limitation the | ||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
package github_test | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/retr0h/git-url-parse/internal" | ||
"github.com/retr0h/git-url-parse/internal/repositories/github" | ||
) | ||
|
||
type GitHubPublicTestSuite struct { | ||
suite.Suite | ||
|
||
rm internal.ParserManager | ||
|
||
logger *slog.Logger | ||
} | ||
|
||
func (suite *GitHubPublicTestSuite) SetupTest() { | ||
suite.logger = slog.New(slog.NewTextHandler(os.Stdout, nil)) | ||
|
||
suite.rm = github.New(suite.logger) | ||
} | ||
|
||
func (suite *GitHubPublicTestSuite) TestIsGitHub() { | ||
type test struct { | ||
input string | ||
want bool | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
input: "github.com", | ||
want: true, | ||
}, | ||
{ | ||
input: "raw.githubusercontent.com", | ||
want: true, | ||
}, | ||
{ | ||
input: "www.github.com", | ||
want: true, | ||
}, | ||
{ | ||
input: "example.com", | ||
want: false, | ||
}, | ||
{ | ||
input: "example", | ||
want: false, | ||
}, | ||
{ | ||
input: ".com", | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
got := suite.rm.IsGitHub(tc.input) | ||
|
||
assert.Equal(suite.T(), got, tc.want) | ||
} | ||
} | ||
|
||
// In order for `go test` to run this suite, we need to create | ||
// a normal test function and pass our suite to suite.Run. | ||
func TestGitHubPublicTestSuite(t *testing.T) { | ||
suite.Run(t, new(GitHubPublicTestSuite)) | ||
} |
Oops, something went wrong.