├── .gitignore ├── .travis.yml ├── README.md ├── main_test.go ├── main.go └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /get 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | sudo: false 4 | 5 | go: 6 | - 1.11 7 | - tip 8 | 9 | # https://github.com/mattn/goveralls#github-integration 10 | before_install: 11 | - go get github.com/mattn/goveralls 12 | - go get golang.org/x/tools/cmd/cover 13 | 14 | script: 15 | - $HOME/gopath/bin/goveralls -service=travis-ci 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | get 2 | ============ 3 | 4 | A wrapper of ghq and go. 5 | 6 | [![Build Status](https://travis-ci.org/pocke/get.svg?branch=master)](https://travis-ci.org/pocke/get) 7 | [![Coverage Status](https://coveralls.io/repos/github/pocke/get/badge.svg?branch=master)](https://coveralls.io/github/pocke/get?branch=master) 8 | 9 | 10 | Installation 11 | ----------- 12 | 13 | ```sh 14 | go get github.com/pocke/get 15 | ``` 16 | 17 | 18 | 19 | 20 | Usage 21 | ----------- 22 | 23 | ```sh 24 | $ get TYPE ADDRESS 25 | ``` 26 | 27 | `get` supports 2 types. 28 | 29 | - [go](https://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies) 30 | - [ghq](https://github.com/motemen/ghq) 31 | 32 | `get` supports 4 styles of address. 33 | 34 | - `https://github.com/pocke/get` 35 | - `https://github.com/pocke/get.git` 36 | - `github.com/pocke/get.git` 37 | - `git@github.com:pocke/get.git` 38 | 39 | ### For example 40 | 41 | ```sh 42 | $ get go https://github.com/pocke/get 43 | $ get ghq github.com/pocke/get.git 44 | ``` 45 | 46 | Advanced Usage 47 | --- 48 | 49 | `Get` supports `-shallow` and `-unshallow` options. They work with `ghq`. `Get` just ignores them with `go`. 50 | 51 | When `-shallow` option is given, `get` clones the specified repository shallowly. 52 | When `-unshallow` option is given, `get` executes `git fetch --unshallow` asynchronously. 53 | 54 | They improves cloning speed. If you specify `-shallow` and `-unshallow`, you can clone repository faster, and get whole repository after a while. 55 | For example: 56 | 57 | ```bash 58 | $ get -shallow -unshallow ghq https://github.com/pocke/get 59 | ``` 60 | 61 | If you'd like to enable this feature by default, put a config file to `~/.config/get/args` with the below content. 62 | 63 | ``` 64 | -shallow -unshallow 65 | ``` 66 | 67 | Links 68 | ------- 69 | 70 | - [go get / ghq get でのアドレス形式の違いから人類を解放した - pockestrap](http://pocke.hatenablog.com/entry/2016/08/22/170516) 71 | - [git: shallow cloneしてすぐunshallowする - pockestrap](https://pocke.hatenablog.com/entry/2018/12/19/015644) 72 | 73 | 74 | License 75 | ------- 76 | 77 | These codes are licensed under CC0. 78 | 79 | [![CC0](http://i.creativecommons.org/p/zero/1.0/88x31.png "CC0")](http://creativecommons.org/publicdomain/zero/1.0/deed.en) 80 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func TestParseCmdArg_Simple(t *testing.T) { 9 | args := []string{"get", "go", "github.com/pocke/get"} 10 | c, err := ParseCmdArg(args) 11 | if err != nil { 12 | t.Fatal(err) 13 | } 14 | 15 | if c.Name != "get" { 16 | t.Errorf("Name should be `get`, but got %s", c.Name) 17 | } 18 | if c.Debug { 19 | t.Errorf("Debug should be false, but got %v", c.Debug) 20 | } 21 | if c.Shallow { 22 | t.Errorf("Shallow should be false, but got %v", c.Shallow) 23 | } 24 | if c.Type != "go" { 25 | t.Errorf("Type should be `go`, but got %s", c.Type) 26 | } 27 | if !reflect.DeepEqual(c.Args, []string{"github.com/pocke/get"}) { 28 | t.Errorf("Args should have an addr, but got %v", c.Args) 29 | } 30 | } 31 | 32 | func TestParseCmdArg_DebugOption(t *testing.T) { 33 | args := []string{"get", "--debug", "go", "github.com/pocke/get"} 34 | c, err := ParseCmdArg(args) 35 | if err != nil { 36 | t.Fatal(err) 37 | } 38 | 39 | if c.Name != "get" { 40 | t.Errorf("Name should be `get`, but got %s", c.Name) 41 | } 42 | if !c.Debug { 43 | t.Errorf("Debug should be true, but got %v", c.Debug) 44 | } 45 | if c.Shallow { 46 | t.Errorf("Shallow should be false, but got %v", c.Shallow) 47 | } 48 | if c.Type != "go" { 49 | t.Errorf("Type should be `go`, but got %s", c.Type) 50 | } 51 | if !reflect.DeepEqual(c.Args, []string{"github.com/pocke/get"}) { 52 | t.Errorf("Args should have an addr, but got %v", c.Args) 53 | } 54 | } 55 | 56 | func TestParseCmdArg_UpdateOption(t *testing.T) { 57 | args := []string{"get", "--debug", "go", "-u", "github.com/pocke/get"} 58 | c, err := ParseCmdArg(args) 59 | if err != nil { 60 | t.Fatal(err) 61 | } 62 | 63 | if c.Name != "get" { 64 | t.Errorf("Name should be `get`, but got %s", c.Name) 65 | } 66 | if !c.Debug { 67 | t.Errorf("Debug should be true, but got %v", c.Debug) 68 | } 69 | if c.Shallow { 70 | t.Errorf("Shallow should be false, but got %v", c.Shallow) 71 | } 72 | if c.Type != "go" { 73 | t.Errorf("Type should be `go`, but got %s", c.Type) 74 | } 75 | if !reflect.DeepEqual(c.Args, []string{"-u", "github.com/pocke/get"}) { 76 | t.Errorf("Args should have some args, but got %v", c.Args) 77 | } 78 | } 79 | 80 | func TestParseCmdArg_ShallowOption(t *testing.T) { 81 | args := []string{"get", "--shallow", "go", "github.com/pocke/get"} 82 | c, err := ParseCmdArg(args) 83 | if err != nil { 84 | t.Fatal(err) 85 | } 86 | 87 | if c.Name != "get" { 88 | t.Errorf("Name should be `get`, but got %s", c.Name) 89 | } 90 | if c.Debug { 91 | t.Errorf("Debug should be false, but got %v", c.Debug) 92 | } 93 | if !c.Shallow { 94 | t.Errorf("Shallow should be true, but got %v", c.Shallow) 95 | } 96 | if c.Type != "go" { 97 | t.Errorf("Type should be `go`, but got %s", c.Type) 98 | } 99 | if !reflect.DeepEqual(c.Args, []string{"github.com/pocke/get"}) { 100 | t.Errorf("Args should have some args, but got %v", c.Args) 101 | } 102 | } 103 | 104 | func TestParseAddr_HTTPS(t *testing.T) { 105 | addrStr := "https://github.com/pocke/get" 106 | addr, err := ParseAddr(addrStr) 107 | if err != nil { 108 | t.Fatal(err) 109 | } 110 | 111 | if addr.Host != "github.com" { 112 | t.Errorf("Host should be `github.com`, but got `%s`", addr.Host) 113 | } 114 | if addr.User != "pocke" { 115 | t.Errorf("User should be `pocke`, but got `%s`", addr.User) 116 | } 117 | if addr.RepoName != "get" { 118 | t.Errorf("User should be `get`, but got `%s`", addr.RepoName) 119 | } 120 | } 121 | 122 | func TestParseAddr_HTTPSWithGit(t *testing.T) { 123 | addrStr := "https://github.com/pocke/get.git" 124 | addr, err := ParseAddr(addrStr) 125 | if err != nil { 126 | t.Fatal(err) 127 | } 128 | 129 | if addr.Host != "github.com" { 130 | t.Errorf("Host should be `github.com`, but got `%s`", addr.Host) 131 | } 132 | if addr.User != "pocke" { 133 | t.Errorf("User should be `pocke`, but got `%s`", addr.User) 134 | } 135 | if addr.RepoName != "get" { 136 | t.Errorf("User should be `get`, but got `%s`", addr.RepoName) 137 | } 138 | } 139 | 140 | func TestParseAddr_SSH(t *testing.T) { 141 | addrStr := "git@github.com:pocke/get.git" 142 | addr, err := ParseAddr(addrStr) 143 | if err != nil { 144 | t.Fatal(err) 145 | } 146 | 147 | if addr.Host != "github.com" { 148 | t.Errorf("Host should be `github.com`, but got `%s`", addr.Host) 149 | } 150 | if addr.User != "pocke" { 151 | t.Errorf("User should be `pocke`, but got `%s`", addr.User) 152 | } 153 | if addr.RepoName != "get" { 154 | t.Errorf("User should be `get`, but got `%s`", addr.RepoName) 155 | } 156 | } 157 | 158 | func TestParseAddr_GoStyle(t *testing.T) { 159 | addrStr := "github.com/pocke/get" 160 | addr, err := ParseAddr(addrStr) 161 | if err != nil { 162 | t.Fatal(err) 163 | } 164 | 165 | if addr.Host != "github.com" { 166 | t.Errorf("Host should be `github.com`, but got `%s`", addr.Host) 167 | } 168 | if addr.User != "pocke" { 169 | t.Errorf("User should be `pocke`, but got `%s`", addr.User) 170 | } 171 | if addr.RepoName != "get" { 172 | t.Errorf("User should be `get`, but got `%s`", addr.RepoName) 173 | } 174 | } 175 | 176 | func TestParseAddr_GitHub(t *testing.T) { 177 | addrStr := "pocke/get" 178 | addr, err := ParseAddr(addrStr) 179 | if err != nil { 180 | t.Fatal(err) 181 | } 182 | 183 | if addr.Host != "github.com" { 184 | t.Errorf("Host should be `github.com`, but got `%s`", addr.Host) 185 | } 186 | if addr.User != "pocke" { 187 | t.Errorf("User should be `pocke`, but got `%s`", addr.User) 188 | } 189 | if addr.RepoName != "get" { 190 | t.Errorf("User should be `get`, but got `%s`", addr.RepoName) 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "io/ioutil" 7 | "os" 8 | "os/exec" 9 | "regexp" 10 | "strings" 11 | 12 | homedir "github.com/mitchellh/go-homedir" 13 | ) 14 | 15 | func main() { 16 | if err := Main(os.Args); err != nil { 17 | fmt.Fprintln(os.Stderr, err) 18 | os.Exit(1) 19 | } 20 | } 21 | 22 | func Main(args []string) error { 23 | configArgs, err := LoadConfig() 24 | if err != nil { 25 | return err 26 | } 27 | 28 | args2 := []string{args[0]} 29 | args2 = append(args2, configArgs...) 30 | args2 = append(args2, args[1:]...) 31 | 32 | c, err := ParseCmdArg(args2) 33 | if err != nil { 34 | return err 35 | } 36 | 37 | fn, ok := Getters[c.Type] 38 | if !ok { 39 | return fmt.Errorf("Type %s doesn't exist", c.Type) 40 | } 41 | 42 | return fn(c) 43 | } 44 | 45 | type CmdArg struct { 46 | Name string // get 47 | Debug bool 48 | Shallow bool 49 | Unshallow bool 50 | Type string // go or ghq 51 | Args []string // [-u github.com/pocke/get] 52 | } 53 | 54 | func LoadConfig() ([]string, error) { 55 | confPath, err := homedir.Expand("~/.config/get/args") 56 | if err != nil { 57 | return nil, err 58 | } 59 | if !FileExists(confPath) { 60 | return []string{}, nil 61 | } 62 | b, err := ioutil.ReadFile(confPath) 63 | if err != nil { 64 | return nil, err 65 | } 66 | return strings.Split(strings.TrimRight(string(b), "\n"), " "), nil 67 | 68 | } 69 | 70 | func ParseCmdArg(args []string) (*CmdArg, error) { 71 | cmdArg := new(CmdArg) 72 | fs := flag.NewFlagSet("get", flag.ContinueOnError) 73 | fs.BoolVar(&cmdArg.Shallow, "shallow", false, "Shallow clone") 74 | fs.BoolVar(&cmdArg.Unshallow, "unshallow", false, "Make the repository unshallow after cloned") 75 | fs.BoolVar(&cmdArg.Debug, "debug", false, "Debug mode") 76 | fs.Parse(args[1:]) 77 | cmdArg.Name = args[0] 78 | 79 | parsedArgs := fs.Args() 80 | 81 | if len(parsedArgs) == 0 { 82 | return nil, fmt.Errorf("Too few arguments. Please `get [-d] TYPE ADDR ...`") 83 | } 84 | 85 | cmdArg.Type = parsedArgs[0] 86 | cmdArg.Args = parsedArgs[1:] 87 | 88 | return cmdArg, nil 89 | } 90 | 91 | var Getters = map[string]func(opt *CmdArg) error{ 92 | "ghq": func(opt *CmdArg) error { 93 | args := []string{"get"} 94 | if opt.Shallow { 95 | args = append(args, "--shallow") 96 | } 97 | addrs := make([]*Addr, 0, len(opt.Args)) 98 | for _, a := range opt.Args { 99 | addr, err := ParseAddr(a) 100 | if err != nil { 101 | args = append(args, a) 102 | } else { 103 | addrs = append(addrs, addr) 104 | args = append(args, addr.ToSSH()) 105 | } 106 | } 107 | 108 | c := exec.Command("ghq", args...) 109 | c.Stdin = os.Stdin 110 | c.Stderr = os.Stderr 111 | c.Stdout = os.Stdout 112 | if opt.Debug { 113 | fmt.Println(strings.Join(c.Args, " ")) 114 | } 115 | err := c.Run() 116 | if err != nil { 117 | return err 118 | } 119 | if opt.Unshallow { 120 | for _, addr := range addrs { 121 | b, err := exec.Command("ghq", "list", "-e", "-p", addr.ToGoStyle()).Output() 122 | if err != nil { 123 | return err 124 | } 125 | 126 | unshallowCmd := exec.Command("git", "fetch", "--unshallow") 127 | unshallowCmd.Dir = strings.TrimRight(string(b), "\n") 128 | err = unshallowCmd.Start() 129 | if err != nil { 130 | return err 131 | } 132 | } 133 | } 134 | return nil 135 | }, 136 | "go": func(opt *CmdArg) error { 137 | args := []string{"get"} 138 | for _, a := range opt.Args { 139 | addr, err := ParseAddr(a) 140 | if err != nil { 141 | args = append(args, a) 142 | } else { 143 | args = append(args, addr.ToGoStyle()) 144 | } 145 | } 146 | c := exec.Command("go", args...) 147 | c.Stdin = os.Stdin 148 | c.Stderr = os.Stderr 149 | c.Stdout = os.Stdout 150 | if opt.Debug { 151 | fmt.Println(strings.Join(c.Args, " ")) 152 | } 153 | return c.Run() 154 | }, 155 | } 156 | 157 | type Addr struct { 158 | Host string 159 | User string 160 | RepoName string 161 | // TODO: Parse dir. e.g.) https://github.com/pocke/lemonade/tree/master/client 162 | // Dir string 163 | } 164 | 165 | func (a *Addr) ToSSH() string { 166 | return fmt.Sprintf("git@%s:%s/%s.git", a.Host, a.User, a.RepoName) 167 | } 168 | 169 | func (a *Addr) ToGoStyle() string { 170 | return fmt.Sprintf("%s/%s/%s", a.Host, a.User, a.RepoName) 171 | } 172 | 173 | func ParseAddr(addrStr string) (*Addr, error) { 174 | mats := []AddrMatcher{ 175 | // https://github.com/pocke/get 176 | // https://github.com/pocke/get.git 177 | {`^https://([^/]+)/([^/]+)/([^/]+?)(?:\.git)?$`, 1, 2, 3}, 178 | // git@github.com:pocke/get.git 179 | {`^git\@([^:]+):([^/]+)/(.+)\.git$`, 1, 2, 3}, 180 | // github.com/pocke/get 181 | {`^([^/]+)/([^/]+)/([^/]+)$`, 1, 2, 3}, 182 | // pocke/get 183 | {`^([^/]+)/([^/]+)$`, -1, 1, 2}, 184 | } 185 | for _, m := range mats { 186 | addr := m.Parse(addrStr) 187 | if addr == nil { 188 | continue 189 | } 190 | return addr, nil 191 | } 192 | 193 | return nil, fmt.Errorf("Can't parse %s as address", addrStr) 194 | } 195 | 196 | func includeString(s []string, t string) bool { 197 | for _, v := range s { 198 | if v == t { 199 | return true 200 | } 201 | } 202 | return false 203 | } 204 | 205 | type AddrMatcher struct { 206 | Pattern string 207 | HostIdx int 208 | UserIdx int 209 | RepoNameIdx int 210 | } 211 | 212 | func (m *AddrMatcher) Parse(addrStr string) *Addr { 213 | re := regexp.MustCompile(m.Pattern) 214 | ma := re.FindStringSubmatch(addrStr) 215 | if len(ma) == 0 { 216 | return nil 217 | } 218 | 219 | addr := &Addr{ 220 | Host: "github.com", 221 | } 222 | 223 | if m.HostIdx > 0 { 224 | addr.Host = ma[m.HostIdx] 225 | } 226 | if m.UserIdx > 0 { 227 | addr.User = ma[m.UserIdx] 228 | } 229 | if m.RepoNameIdx > 0 { 230 | addr.RepoName = ma[m.RepoNameIdx] 231 | } 232 | 233 | return addr 234 | } 235 | 236 | func FileExists(filename string) bool { 237 | _, err := os.Stat(filename) 238 | return err == nil 239 | } 240 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | 117 | --------------------------------------------------------------------------------