From fba92696b3a243c9412e976f856d2cb8a6c3c566 Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 16 Jun 2022 14:16:31 +0800 Subject: [PATCH] chore: add new file for repo branch info --- .github/workflows/go.yml | 1 - info.go | 112 -------------------------------------- info_branch.go | 1 + info_remote.go | 113 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 113 deletions(-) create mode 100644 info_branch.go create mode 100644 info_remote.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 13ef134..f231305 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -32,7 +32,6 @@ jobs: git remote -v git tag -l env - make - name: Setup Go Faster uses: WillAbides/setup-go-faster@v1.7.0 diff --git a/info.go b/info.go index d5a14ac..b91db42 100644 --- a/info.go +++ b/info.go @@ -1,7 +1,5 @@ package gitw -import "fmt" - // some consts for remote info const ( ProtoSSH = "ssh" @@ -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: "git@github.com: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: "git@github.com: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: "git@github.com: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) -} diff --git a/info_branch.go b/info_branch.go new file mode 100644 index 0000000..8a6f3bc --- /dev/null +++ b/info_branch.go @@ -0,0 +1 @@ +package gitw diff --git a/info_remote.go b/info_remote.go new file mode 100644 index 0000000..a3a67d4 --- /dev/null +++ b/info_remote.go @@ -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: "git@github.com: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: "git@github.com: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: "git@github.com: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) +}