Skip to content

Commit

Permalink
feat: add --clone flag
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 7, 2020
1 parent be85817 commit 54194ec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ Or [download a binary manually](https://github.com/egoist/spin/releases).
spin user/repo new-project
```

### Private Repository

Use `--clone` which uses `git clone` under the clone to download your repository:

```bash
spin user/private-repo new-project --clone
```

## License

MIT © [EGOIST (Kevin Titor)](https://egoist.sh)
26 changes: 17 additions & 9 deletions spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func main() {
repo := kingpin.Arg("repo", "Repository slug").Required().String()
target := kingpin.Arg("out-dir", "Output directory").Default(".").String()
clone := kingpin.Flag("clone", "Clone repository").Bool()

kingpin.Parse()

Expand All @@ -27,16 +28,23 @@ func main() {
cacheDir := filepath.Join(homedir, fmt.Sprintf(".spin/templates/github/%s", *repo))

if !utils.PathExists(cacheDir) {
url := fmt.Sprintf("https://github.com/%s/archive/master/archive.zip", *repo)

zipFile, err := utils.DownloadFile(url)

if err != nil {
fmt.Println("error", err)
return
if *clone == true {
if err := utils.GitClone(*repo, cacheDir); err != nil {
fmt.Println("error", err)
return
}
} else {
url := fmt.Sprintf("https://github.com/%s/archive/master/archive.zip", *repo)

zipFile, err := utils.DownloadFile(url)

if err != nil {
fmt.Println("error", err)
return
}

utils.Unzip(zipFile.Name(), cacheDir)
}

utils.Unzip(zipFile.Name(), cacheDir)
}

if err := utils.Copy(cacheDir, *target); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"fmt"
"os"
"os/exec"
"path/filepath"
)

// GitClone clones a repo with depth 1
func GitClone(repo string, outDir string) (err error) {
os.MkdirAll(filepath.Dir(outDir), os.ModePerm)
cmd := exec.Command("git", "clone", "[email protected]:"+repo+".git", outDir, "--depth=1")
fmt.Printf("Running git clone and waiting for it to finish...\n")
err = cmd.Run()
if err != nil {
fmt.Printf("Command finished with error: %v\n", err)
return
}
err = os.RemoveAll(filepath.Join(outDir, ".git"))
return
}

0 comments on commit 54194ec

Please sign in to comment.