Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Simplify configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorinvo committed Jun 13, 2017
1 parent 22770e8 commit 6b9a7a6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
10 changes: 5 additions & 5 deletions ghbackup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

// Clone new repo or pull in existing repo
func backup(backupDir, account, secret string, r repo, updates chan Update) error {
repoDir := getRepoDir(backupDir, r.Path, account)
func (c Config) backup(r repo) error {
repoDir := getRepoDir(c.Dir, r.Path, c.Account)

repoExists, err := exists(repoDir)
if err != nil {
Expand All @@ -20,12 +20,12 @@ func backup(backupDir, account, secret string, r repo, updates chan Update) erro

var cmd *exec.Cmd
if repoExists {
updates <- Update{UInfo, fmt.Sprintf("Updating %s", r.Path)}
c.Log.Printf("Updating %s", r.Path)
cmd = exec.Command("git", "remote", "update")
cmd.Dir = repoDir
} else {
updates <- Update{UInfo, fmt.Sprintf("Cloning %s", r.Path)}
cmd = exec.Command("git", "clone", "--mirror", "--no-checkout", getCloneURL(r, secret), repoDir)
c.Log.Printf("Cloning %s", r.Path)
cmd = exec.Command("git", "clone", "--mirror", "--no-checkout", getCloneURL(r, c.Secret), repoDir)
}

out, err := cmd.CombinedOutput()
Expand Down
24 changes: 6 additions & 18 deletions ghbackup/ghbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
// This way you can directly use it from any other Go program.
package ghbackup

import "net/http"
import (
"log"
"net/http"
)

// Config should be passed to Run.
// Only Account, Dir, Updates are required.
type Config struct {
Account string
Dir string
Updates chan Update
// Optional:
Err *log.Logger
Log *log.Logger
Secret string
API string
Workers int
Expand All @@ -24,22 +28,6 @@ type Doer interface {
Do(*http.Request) (*http.Response, error)
}

// Update is the format of updates emitted while running.
type Update struct {
Type UpdateType
Message string
}

// UpdateType helps you to decide what to do with an Update .
type UpdateType int

const (
// UErr occurs when something went wrong, but the backup can keep running.
UErr UpdateType = iota
// UInfo contains progress information that could be optionally logged.
UInfo
)

type repo struct {
Path string `json:"full_name"`
URL string `json:"clone_url"`
Expand Down
18 changes: 12 additions & 6 deletions ghbackup/run.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package ghbackup

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
)

// Run update for the given Config.
func Run(config Config) error {
// Defaults
if config.Log == nil {
config.Log = log.New(ioutil.Discard, "", 0)
}
if config.Err == nil {
config.Err = log.New(ioutil.Discard, "", 0)
}
if config.Workers == 0 {
config.Workers = defaultMaxWorkers
}
Expand All @@ -25,17 +32,16 @@ func Run(config Config) error {
return err
}

config.Updates <- Update{UInfo, fmt.Sprintf("%d repositories:", len(repos))}
config.Log.Printf("%d repositories:", len(repos))

// Backup repositories in parallel
each(repos, config.Workers, func(r repo) {
err := backup(config.Dir, config.Account, config.Secret, r, config.Updates)
if err != nil {
config.Updates <- Update{UErr, err.Error()}
if err := config.backup(r); err != nil {
config.Err.Println(err)
}
})

config.Updates <- Update{UInfo, "All done."}
config.Log.Println("All done.")

return nil
}
Expand Down
21 changes: 8 additions & 13 deletions ghbackup/run_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ghbackup_test

import (
"bytes"
"io/ioutil"
"log"
"os"
"testing"

Expand All @@ -14,29 +16,22 @@ func TestRun(t *testing.T) {
t.Error(err)
}
defer func() {
err := os.RemoveAll(dir)
if err != nil {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()

updates := make(chan ghbackup.Update)
go func() {
for u := range updates {
switch u.Type {
case ghbackup.UErr:
t.Error("Unexpected error:", u.Message)
}
}
}()

var errs bytes.Buffer
err = ghbackup.Run(ghbackup.Config{
Account: "qvl",
Dir: dir,
Secret: os.Getenv("SECRET"),
Updates: updates,
Err: log.New(&errs, "", log.Lshortfile),
})

if errs.Len() != 0 {
t.Error("Unexpected error messages:", errs.String())
}
if err != nil {
t.Error("Unexpected error:", err)
}
Expand Down
24 changes: 9 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"

Expand Down Expand Up @@ -61,26 +63,18 @@ func main() {
os.Exit(1)
}

// Log updates
updates := make(chan ghbackup.Update)
go func() {
for u := range updates {
switch u.Type {
case ghbackup.UErr:
fmt.Fprintln(os.Stderr, u.Message)
case ghbackup.UInfo:
if !*silent {
fmt.Fprintln(os.Stderr, u.Message)
}
}
}
}()
errs := log.New(os.Stderr, "", 0)
logs := errs
if *silent {
logs = log.New(ioutil.Discard, "", 0)
}

err := ghbackup.Run(ghbackup.Config{
Account: *account,
Dir: args[0],
Secret: *secret,
Updates: updates,
Log: logs,
Err: errs,
})

if err != nil {
Expand Down

0 comments on commit 6b9a7a6

Please sign in to comment.