Skip to content

Commit

Permalink
chore: add new file for repo branch info
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 16, 2022
1 parent 13231fc commit fba9269
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 113 deletions.
1 change: 0 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
git remote -v
git tag -l
env
make
- name: Setup Go Faster
uses: WillAbides/[email protected]
Expand Down
112 changes: 0 additions & 112 deletions info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package gitw

import "fmt"

// some consts for remote info
const (
ProtoSSH = "ssh"
Expand All @@ -23,113 +21,3 @@ type RepoInfo struct {
Branch string
Version string
}

// remote type names
const (
RemoteTypePush = "push"
RemoteTypeFetch = "fetch"
)

// RemoteInfos map. key is type name(see RemoteTypePush)
type RemoteInfos map[string]*RemoteInfo

// RemoteInfo struct
// - http: "https://github.com/gookit/gitw.git"
// - git: "[email protected]:gookit/gitw.git"
type RemoteInfo struct {
// Name the repo remote name, default see DefaultRemoteName
Name string
// Type remote type. allow: push, fetch
Type string
// URL full git remote URL string.
//
// eg:
// - http: "https://github.com/gookit/gitw.git"
// - git: "[email protected]:gookit/gitw.git"
URL string

// ---- details

// Scheme the url scheme. eg: git, http, https
Scheme string
// Host name. eg: "github.com"
Host string
// the group, repo name
Group, Repo string

// Proto the type 'ssh' OR 'http'
Proto string
}

// NewRemoteInfo create
func NewRemoteInfo(name, url, typ string) (*RemoteInfo, error) {
r := &RemoteInfo{
Name: name,
URL: url,
Type: typ,
}

err := ParseRemoteURL(url, r)

if err != nil {
return nil, err
}
return r, nil
}

// NewEmptyRemoteInfo only with URL string.
func NewEmptyRemoteInfo(URL string) *RemoteInfo {
return &RemoteInfo{
Name: DefaultRemoteName,
URL: URL,
Type: RemoteTypePush,
}
}

// Valid check
func (r *RemoteInfo) Valid() bool {
return r.URL != ""
}

// Invalid check
func (r *RemoteInfo) Invalid() bool {
return r.URL == ""
}

// GitURL build. eg: "[email protected]:gookit/gitw.git"
func (r *RemoteInfo) GitURL() string {
return SchemeGIT + "@" + r.Host + ":" + r.Group + "/" + r.Repo + ".git"
}

// RawURLOfHTTP get, if RemoteInfo.URL is git proto, build an https url.
func (r *RemoteInfo) RawURLOfHTTP() string {
if r.Proto == ProtoHTTP {
return r.URL
}
return r.URLOfHTTPS()
}

// URLOfHTTP build
func (r *RemoteInfo) URLOfHTTP() string {
return SchemeHTTP + "://" + r.Host + "/" + r.Group + "/" + r.Repo
}

// URLOfHTTPS build
func (r *RemoteInfo) URLOfHTTPS() string {
return SchemeHTTPS + "://" + r.Host + "/" + r.Group + "/" + r.Repo
}

// Path string
func (r *RemoteInfo) Path() string {
return r.RepoPath()
}

// RepoPath string
func (r *RemoteInfo) RepoPath() string {
return r.Group + "/" + r.Repo
}

// String remote info to string.
func (r *RemoteInfo) String() string {
return fmt.Sprintf("%s %s (%s)", r.Name, r.URL, r.Type)
}
1 change: 1 addition & 0 deletions info_branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package gitw
113 changes: 113 additions & 0 deletions info_remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package gitw

import "fmt"

// remote type names
const (
RemoteTypePush = "push"
RemoteTypeFetch = "fetch"
)

// RemoteInfos map. key is type name(see RemoteTypePush)
type RemoteInfos map[string]*RemoteInfo

// RemoteInfo struct
// - http: "https://github.com/gookit/gitw.git"
// - git: "[email protected]:gookit/gitw.git"
type RemoteInfo struct {
// Name the repo remote name, default see DefaultRemoteName
Name string
// Type remote type. allow: push, fetch
Type string
// URL full git remote URL string.
//
// eg:
// - http: "https://github.com/gookit/gitw.git"
// - git: "[email protected]:gookit/gitw.git"
URL string

// ---- details

// Scheme the url scheme. eg: git, http, https
Scheme string
// Host name. eg: "github.com"
Host string
// the group, repo name
Group, Repo string

// Proto the type 'ssh' OR 'http'
Proto string
}

// NewRemoteInfo create
func NewRemoteInfo(name, url, typ string) (*RemoteInfo, error) {
r := &RemoteInfo{
Name: name,
URL: url,
Type: typ,
}

err := ParseRemoteURL(url, r)

if err != nil {
return nil, err
}
return r, nil
}

// NewEmptyRemoteInfo only with URL string.
func NewEmptyRemoteInfo(URL string) *RemoteInfo {
return &RemoteInfo{
Name: DefaultRemoteName,
URL: URL,
Type: RemoteTypePush,
}
}

// Valid check
func (r *RemoteInfo) Valid() bool {
return r.URL != ""
}

// Invalid check
func (r *RemoteInfo) Invalid() bool {
return r.URL == ""
}

// GitURL build. eg: "[email protected]:gookit/gitw.git"
func (r *RemoteInfo) GitURL() string {
return SchemeGIT + "@" + r.Host + ":" + r.Group + "/" + r.Repo + ".git"
}

// RawURLOfHTTP get, if RemoteInfo.URL is git proto, build an https url.
func (r *RemoteInfo) RawURLOfHTTP() string {
if r.Proto == ProtoHTTP {
return r.URL
}
return r.URLOfHTTPS()
}

// URLOfHTTP build
func (r *RemoteInfo) URLOfHTTP() string {
return SchemeHTTP + "://" + r.Host + "/" + r.Group + "/" + r.Repo
}

// URLOfHTTPS build
func (r *RemoteInfo) URLOfHTTPS() string {
return SchemeHTTPS + "://" + r.Host + "/" + r.Group + "/" + r.Repo
}

// Path string
func (r *RemoteInfo) Path() string {
return r.RepoPath()
}

// RepoPath string
func (r *RemoteInfo) RepoPath() string {
return r.Group + "/" + r.Repo
}

// String remote info to string.
func (r *RemoteInfo) String() string {
return fmt.Sprintf("%s %s (%s)", r.Name, r.URL, r.Type)
}

0 comments on commit fba9269

Please sign in to comment.