mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
get -u
This commit is contained in:
parent
4b9c9f7af5
commit
b8b3479eb0
5
Makefile
5
Makefile
|
|
@ -12,4 +12,7 @@ deps:
|
|||
testdeps:
|
||||
go get -t $(VERBOSE_FLAG) ./...
|
||||
|
||||
.PHONY: build test deps testdeps
|
||||
install: deps
|
||||
go build $(VERBOSE_FLAG) ./...
|
||||
|
||||
.PHONY: build test deps testdeps install
|
||||
|
|
|
|||
120
main.go
120
main.go
|
|
@ -26,6 +26,9 @@ func main() {
|
|||
Name: "get",
|
||||
Usage: "Clone/sync with a remote repository",
|
||||
Action: CommandGet,
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{"update, u", "Update local repository if cloned already"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
|
|
@ -33,51 +36,15 @@ func main() {
|
|||
Action: CommandList,
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{"exact, e", "Perform an exact match"},
|
||||
cli.BoolFlag{"full-path, p", "Show full paths"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "pocket",
|
||||
Action: func(c *cli.Context) {
|
||||
accessToken, err := GitConfig("ghq.pocket.token")
|
||||
mustBeOkay(err)
|
||||
|
||||
if accessToken == "" {
|
||||
receiverURL, ch, err := pocket.StartAccessTokenReceiver()
|
||||
mustBeOkay(err)
|
||||
|
||||
authRequest, err := pocket.ObtainRequestToken(receiverURL)
|
||||
mustBeOkay(err)
|
||||
|
||||
url := pocket.GenerateAuthorizationURL(authRequest.Code, receiverURL)
|
||||
utils.Log("open", url)
|
||||
|
||||
<-ch
|
||||
|
||||
authorized, err := pocket.ObtainAccessToken(authRequest.Code)
|
||||
mustBeOkay(err)
|
||||
|
||||
accessToken = authorized.AccessToken
|
||||
Git("config", "ghq.pocket.token", authorized.AccessToken)
|
||||
}
|
||||
|
||||
res, err := pocket.RetrieveGitHubEntries(accessToken)
|
||||
mustBeOkay(err)
|
||||
|
||||
for _, item := range res.List {
|
||||
u, err := ParseGitHubURL(item.ResolvedURL)
|
||||
if err != nil {
|
||||
utils.Log("error", err.Error())
|
||||
continue
|
||||
} else if u.User == "blog" {
|
||||
utils.Log("skip", fmt.Sprintf("%s: is not a repository", u))
|
||||
continue
|
||||
} else if u.Extra != "" {
|
||||
utils.Log("skip", fmt.Sprintf("%s: is not project home", u))
|
||||
continue
|
||||
}
|
||||
|
||||
GetGitHubRepository(u)
|
||||
}
|
||||
Name: "pocket",
|
||||
Usage: "Does get for all github entries in Pocket",
|
||||
Action: CommandPocket,
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{"update, u", "Update local repository if cloned already"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -95,6 +62,7 @@ func mustBeOkay(err error) {
|
|||
|
||||
func CommandGet(c *cli.Context) {
|
||||
argUrl := c.Args().Get(0)
|
||||
doUpdate := c.Bool("update")
|
||||
|
||||
if argUrl == "" {
|
||||
cli.ShowCommandHelp(c, "get")
|
||||
|
|
@ -104,10 +72,10 @@ func CommandGet(c *cli.Context) {
|
|||
u, err := ParseGitHubURL(argUrl)
|
||||
mustBeOkay(err)
|
||||
|
||||
GetGitHubRepository(u)
|
||||
GetGitHubRepository(u, doUpdate)
|
||||
}
|
||||
|
||||
func GetGitHubRepository(u *GitHubURL) {
|
||||
func GetGitHubRepository(u *GitHubURL, doUpdate bool) {
|
||||
path := pathForRepository(u)
|
||||
|
||||
newPath := false
|
||||
|
|
@ -127,7 +95,7 @@ func GetGitHubRepository(u *GitHubURL) {
|
|||
dir, _ := filepath.Split(path)
|
||||
mustBeOkay(os.MkdirAll(dir, 0755))
|
||||
Git("clone", u.String(), path)
|
||||
} else {
|
||||
} else if doUpdate {
|
||||
utils.Log("update", path)
|
||||
|
||||
mustBeOkay(os.Chdir(path))
|
||||
|
|
@ -138,6 +106,7 @@ func GetGitHubRepository(u *GitHubURL) {
|
|||
func CommandList(c *cli.Context) {
|
||||
query := c.Args().First()
|
||||
exact := c.Bool("exact")
|
||||
showFullPath := c.Bool("full-path")
|
||||
|
||||
var filterFn func(string, string, string) bool
|
||||
if query == "" {
|
||||
|
|
@ -148,16 +117,69 @@ func CommandList(c *cli.Context) {
|
|||
filterFn = func(relPath, user, repo string) bool { return strings.Contains(relPath, query) }
|
||||
}
|
||||
|
||||
walkLocalRepositories(func(relPath, user, repo string) {
|
||||
walkLocalRepositories(func(fullPath, relPath, user, repo string) {
|
||||
if filterFn(relPath, user, repo) == false {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(relPath)
|
||||
if showFullPath {
|
||||
fmt.Println(fullPath)
|
||||
} else {
|
||||
fmt.Println(relPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func walkLocalRepositories(callback func(string, string, string)) {
|
||||
func CommandPocket(c *cli.Context) {
|
||||
accessToken, err := GitConfig("ghq.pocket.token")
|
||||
mustBeOkay(err)
|
||||
|
||||
if accessToken == "" {
|
||||
receiverURL, ch, err := pocket.StartAccessTokenReceiver()
|
||||
mustBeOkay(err)
|
||||
utils.Log("pocket", "Waiting for Pocket authentication callback at "+receiverURL)
|
||||
|
||||
utils.Log("pocket", "Obtaining request token")
|
||||
authRequest, err := pocket.ObtainRequestToken(receiverURL)
|
||||
mustBeOkay(err)
|
||||
|
||||
url := pocket.GenerateAuthorizationURL(authRequest.Code, receiverURL)
|
||||
utils.Log("open", url)
|
||||
|
||||
<-ch
|
||||
|
||||
utils.Log("pocket", "Obtaining access token")
|
||||
authorized, err := pocket.ObtainAccessToken(authRequest.Code)
|
||||
mustBeOkay(err)
|
||||
|
||||
utils.Log("authorized", authorized.Username)
|
||||
|
||||
accessToken = authorized.AccessToken
|
||||
Git("config", "ghq.pocket.token", authorized.AccessToken)
|
||||
}
|
||||
|
||||
utils.Log("pocket", "Retrieving github.com entries")
|
||||
res, err := pocket.RetrieveGitHubEntries(accessToken)
|
||||
mustBeOkay(err)
|
||||
|
||||
for _, item := range res.List {
|
||||
u, err := ParseGitHubURL(item.ResolvedURL)
|
||||
if err != nil {
|
||||
utils.Log("error", err.Error())
|
||||
continue
|
||||
} else if u.User == "blog" {
|
||||
utils.Log("skip", fmt.Sprintf("%s: is not a repository", u))
|
||||
continue
|
||||
} else if u.Extra != "" {
|
||||
utils.Log("skip", fmt.Sprintf("%s: is not project home", u))
|
||||
continue
|
||||
}
|
||||
|
||||
GetGitHubRepository(u, c.Bool("update"))
|
||||
}
|
||||
}
|
||||
|
||||
func walkLocalRepositories(callback func(string, string, string, string)) {
|
||||
root := reposRoot()
|
||||
filepath.Walk(root, func(path string, fileInfo os.FileInfo, err error) error {
|
||||
rel, err := filepath.Rel(root, path)
|
||||
|
|
@ -168,7 +190,7 @@ func walkLocalRepositories(callback func(string, string, string)) {
|
|||
return nil
|
||||
}
|
||||
|
||||
callback(rel, user, repo)
|
||||
callback(path, rel, user, repo)
|
||||
|
||||
return filepath.SkipDir
|
||||
})
|
||||
|
|
|
|||
|
|
@ -8,8 +8,6 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/motemen/ghq/utils"
|
||||
)
|
||||
|
||||
var consumerKey = "27000-330870baad7ffbb5ab2fa6b2"
|
||||
|
|
@ -67,8 +65,6 @@ func requestAPI(action string, params url.Values, v interface{}) error {
|
|||
}
|
||||
|
||||
func ObtainRequestToken(redirectURL string) (*OAuthRequestAPIResponse, error) {
|
||||
utils.Log("pocket", "Obtaining request token")
|
||||
|
||||
res := &OAuthRequestAPIResponse{}
|
||||
err := requestAPI(
|
||||
"/v3/oauth/request",
|
||||
|
|
@ -103,14 +99,11 @@ func StartAccessTokenReceiver() (string, <-chan bool, error) {
|
|||
}()
|
||||
|
||||
url := "http://" + listener.Addr().String()
|
||||
utils.Log("pocket", "Waiting for Pocket authentication callback at "+url)
|
||||
|
||||
return url, ch, nil
|
||||
}
|
||||
|
||||
func ObtainAccessToken(requestToken string) (*OAuthAuthorizeAPIResponse, error) {
|
||||
utils.Log("pocket", "Obtaining access token")
|
||||
|
||||
res := &OAuthAuthorizeAPIResponse{}
|
||||
err := requestAPI(
|
||||
"/v3/oauth/authorize",
|
||||
|
|
@ -124,8 +117,6 @@ func ObtainAccessToken(requestToken string) (*OAuthAuthorizeAPIResponse, error)
|
|||
return nil, err
|
||||
}
|
||||
|
||||
utils.Log("authorized", res.Username)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
|
@ -135,8 +126,6 @@ func GenerateAuthorizationURL(token, redirectURL string) string {
|
|||
}
|
||||
|
||||
func RetrieveGitHubEntries(accessToken string) (*RetrieveAPIResponse, error) {
|
||||
utils.Log("pocket", "Retrieving github.com entries")
|
||||
|
||||
res := &RetrieveAPIResponse{}
|
||||
err := requestAPI(
|
||||
"/v3/get",
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ func Log(prefix string, message string) {
|
|||
} else if prefix == "open" {
|
||||
ct.ChangeColor(ct.Yellow, true, ct.None, false)
|
||||
} else if prefix == "authorized" || prefix == "skip" {
|
||||
ct.ChangeColor(ct.Blue, false, ct.None, false)
|
||||
ct.ChangeColor(ct.Blue, true, ct.None, false)
|
||||
} else {
|
||||
ct.ChangeColor(ct.Green, true, ct.None, false)
|
||||
ct.ChangeColor(ct.Green, false, ct.None, false)
|
||||
}
|
||||
fmt.Printf("%10s", prefix)
|
||||
ct.ResetColor()
|
||||
|
|
|
|||
12
utils/log_test.go
Normal file
12
utils/log_test.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
Log("default", "shows this color")
|
||||
Log("error", "shows this color")
|
||||
Log("open", "shows this color")
|
||||
Log("authorized", "shows this color")
|
||||
Log("skip", "shows this color")
|
||||
Log("git", "shows this color")
|
||||
}
|
||||
Loading…
Reference in a new issue