Skip to content

Commit

Permalink
feat: added top level repository pkg
Browse files Browse the repository at this point in the history
Added top level repository package, along with docs usage, and example
client.
  • Loading branch information
retr0h committed Jun 21, 2024
1 parent 00fd5f7 commit 0e2c534
Show file tree
Hide file tree
Showing 22 changed files with 718 additions and 196 deletions.
4 changes: 0 additions & 4 deletions docs/docs/resources/_category_.yml

This file was deleted.

33 changes: 0 additions & 33 deletions docs/docs/resources/file.md

This file was deleted.

63 changes: 63 additions & 0 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,66 @@ sidebar_position: 4
---

# Usage

## Package

### Parse a Repository

See example client in `examples/go-client/`.

```go
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())
}
```
12 changes: 0 additions & 12 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ const config = {
src: 'img/logo.png',
},
items: [
{
type: 'dropdown',
label: 'Resources',
position: 'left',
items: [
{
type: 'doc',
label: 'File',
docId: 'resources/file',
},
],
},
// {
// type: 'localeDropdown',
// position: 'left',
Expand Down
12 changes: 12 additions & 0 deletions examples/go-client/go.mod
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
12 changes: 12 additions & 0 deletions examples/go-client/go.sum
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=
54 changes: 54 additions & 0 deletions examples/go-client/main.go
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())
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/retr0h/git-url-parse

go 1.22

require github.com/stretchr/testify v1.9.0
require (
github.com/chainguard-dev/git-urls v1.0.2
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
31 changes: 31 additions & 0 deletions internal/parser.go
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)
}
45 changes: 45 additions & 0 deletions internal/repositories/github/github.go
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
}
93 changes: 93 additions & 0 deletions internal/repositories/github/github_public_test.go
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))
}
Loading

0 comments on commit 0e2c534

Please sign in to comment.