Skip to content

Commit

Permalink
Migrate to Go Modules, Travic CI added, and fix lint also change pack…
Browse files Browse the repository at this point in the history
…age name
  • Loading branch information
supanadit committed Apr 20, 2020
1 parent 4100f59 commit e943953
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
language: go

matrix:
fast_finish: true
include:
- go: 1.14.x
- go: master

git:
depth: 10

before_install:
- if [[ "${GO111MODULE}" = "on" ]]; then mkdir "${HOME}/go"; export GOPATH="${HOME}/go"; fi

install:
- if [[ "${GO111MODULE}" = "on" ]]; then go mod download; fi
- if [[ "${GO111MODULE}" = "on" ]]; then export PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"; fi
- if [[ "${GO111MODULE}" = "on" ]]; then make tools; fi

go_import_path: github.com/supanadit/git-type

after_success:
- bash <(curl -s https://codecov.io/bash)
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Git Type

[![Build Status](https://travis-ci.com/supanadit/git-type.svg?branch=master)](https://travis-ci.com/supanadit/git-type)
[![Go Report Card](https://goreportcard.com/badge/github.com/supanadit/git-type)](https://goreportcard.com/report/github.com/supanadit/git-type)

Check Git URL protocol easily whether is SSH, HTTP or HTTPS
Check Git URL protocol easily whether is SSH, HTTP or HTTPS with Zero Dependencies

```golang
type GitType struct {
type Type struct {
Url string
Type string
RepositoryName string
Expand All @@ -15,34 +16,34 @@ type GitType struct {
## Installation

```shell script
go get -u -v github.com/supanadit/git-type
go get -u -v github.com/supanadit/gity
```

## How To Use
```golang
import "github.com/supanadit/git-type"
import "github.com/supanadit/gity"
```

### For SSH
```golang
gitType, err := gittype.NewGitType("git@github.com:supanadit/devops-factory.git")
gity, err := gity.Check("git@github.com:supanadit/jwt-go.git")
if err != nil {
panic(err)
}
fmt.Println(gitType.IsHTTPORS()) // false
fmt.Println(gitType.IsHTTP()) // false
fmt.Println(gitType.IsHTTPS()) // false
fmt.Println(gitType.IsSSH()) // true
fmt.Println(gity.IsHTTPORS()) // false
fmt.Println(gity.IsHTTP()) // false
fmt.Println(gity.IsHTTPS()) // false
fmt.Println(gity.IsSSH()) // true
```

### For HTTP / HTTPS
```golang
gitType, err := gittype.NewGitType("https://github.com/supanadit/devops-factory.git")
type, err := gity.Check("https://github.com/supanadit/jwt-go.git")
if err != nil {
panic(err)
}
fmt.Println(gitType.IsHTTPORS()) // true
fmt.Println(gitType.IsHTTP()) // false
fmt.Println(gitType.IsHTTPS()) // true
fmt.Println(gitType.IsSSH()) // false
fmt.Println(type.IsHTTPORS()) // true
fmt.Println(type.IsHTTP()) // false
fmt.Println(type.IsHTTPS()) // true
fmt.Println(type.IsSSH()) // false
```
49 changes: 29 additions & 20 deletions gittype.go → gity.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package gittype
package gity

import (
"errors"
"strings"
)

var sshType string = "ssh"
var httpType string = "http"
var httpsType string = "https"
// sshType is constant variable
const sshType string = "ssh"

type GitType struct {
// httpType is constant variable
const httpType string = "http"

// httpsType is constant variable
const httpsType string = "https"

// Type is the default model provided by the library
type Type struct {
Url string
Type string
RepositoryName string
}

func NewGitType(url string) (GitType, error) {
var gitType GitType
var err error = nil
// Check is the function to check type of git
func Check(url string) (t Type, err error) {
knownType := false

httpsProtocol := httpsType + "://"
Expand All @@ -28,12 +33,12 @@ func NewGitType(url string) (GitType, error) {
repositoryNameSplit := strings.Split(splitPath[len(splitPath)-1], ".")
if len(repositoryNameSplit) == 2 {
if repositoryNameSplit[1] == "git" {
gitType.Url = url
gitType.RepositoryName = repositoryNameSplit[0]
t.Url = url
t.RepositoryName = repositoryNameSplit[0]
if url[0:len(httpsProtocol)] == httpsProtocol {
gitType.Type = httpsType
t.Type = httpsType
} else {
gitType.Type = httpType
t.Type = httpType
}
knownType = true
}
Expand All @@ -49,9 +54,9 @@ func NewGitType(url string) (GitType, error) {
repositoryNameSplit := strings.Split(splitPath[len(splitPath)-1], ".")
if len(repositoryNameSplit) == 2 {
if repositoryNameSplit[1] == "git" {
gitType.Url = url
gitType.RepositoryName = repositoryNameSplit[0]
gitType.Type = sshType
t.Url = url
t.RepositoryName = repositoryNameSplit[0]
t.Type = sshType
knownType = true
}
}
Expand All @@ -63,30 +68,34 @@ func NewGitType(url string) (GitType, error) {
if !knownType {
err = errors.New("unknown URL type")
}
return gitType, err
return t, err
}

func (gitType GitType) IsHTTPS() bool {
// IsHTTPS will return true if this url is type of HTTPS
func (gitType Type) IsHTTPS() bool {
valid := false
if gitType.Type == httpsType {
valid = true
}
return valid
}

func (gitType GitType) IsHTTP() bool {
// IsHTTP will return true if this url is type of HTTP
func (gitType Type) IsHTTP() bool {
valid := false
if gitType.Type == httpType {
valid = true
}
return valid
}

func (gitType GitType) IsHTTPORS() bool {
// IsHTTPORS will return true if this url is type of HTTP or HTTPS
func (gitType Type) IsHTTPORS() bool {
return gitType.IsHTTPS() || gitType.IsHTTP()
}

func (gitType GitType) IsSSH() bool {
// IsSSH will return true if this url is type of SSH
func (gitType Type) IsSSH() bool {
valid := false
if gitType.Type == sshType {
valid = true
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/supanadit/gity

go 1.14

0 comments on commit e943953

Please sign in to comment.