Skip to content

Commit

Permalink
Complete first milestone. Clone a single repo to local machine.
Browse files Browse the repository at this point in the history
  • Loading branch information
tempor1s committed Mar 9, 2020
1 parent d778ada commit b414b20
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A tool that allows you to backup all yourGitHub repos onto your local machine, a

## Installation

A quick guide on how to install the tool. Not currenlty available.
A quick guide on how to install the tool. Not currently available.

```bash
brew tap tempor1s/gobackup
Expand All @@ -25,7 +25,7 @@ TODO

## Milestones

- [ ] Clone a single repository to your computer through CLI.
- [x] Clone a single repository to your computer through CLI.
- [ ] Clone multiple repositories to local computer
- [ ] Clone multiple reposiories using concurrency
- [ ] Upload cloned repositories to other services like GitLab or BitBucket
Expand Down
38 changes: 37 additions & 1 deletion cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package cmd

import (
"fmt"
"log"
"os"
"path"
"strings"

"github.com/spf13/cobra"
"gopkg.in/src-d/go-git.v4"
)

func init() {
Expand All @@ -20,5 +25,36 @@ var backupCommand = &cobra.Command{

// TODO: Put this into another file as to not muddy up the cmd package that should only be used for managing commands
func backup(cmd *cobra.Command, args []string) {
fmt.Println("Hello, from the backup command!")
if len(args) == 0 {
fmt.Println("Please enter a URL for a GitHub user. Example: `gobackup backup github.com/tempor1s`")
return
}

// Get the URL to clone
repoURL := args[0]

// Get the users github name for the directory
dirName := path.Base(repoURL)

// Make sure it has the https prefix, and add it if it does not
if !strings.HasPrefix(repoURL, "https://") {
repoURL = "https://" + repoURL
}

// Create username dir to put all cloned repos in.
err := os.MkdirAll(dirName, os.ModePerm)

if err != nil {
log.Fatal(err)
}

// Clone the repo into username folder that we just created - will keep git information because not bare
_, err = git.PlainClone(dirName, false, &git.CloneOptions{
URL: repoURL,
Progress: os.Stdout,
})

if err != nil {
log.Fatal(err)
}
}
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ module github.com/tempor1s/gobackup

go 1.14

require github.com/spf13/cobra v0.0.6
require (
github.com/kr/pretty v0.2.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/cobra v0.0.6
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
)

0 comments on commit b414b20

Please sign in to comment.