forked from qvl/ghbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (68 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Package main is the entry point for the ghbackup binary.
// Here is where you can find argument parsing, usage information and the actual execution.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"github.com/fujisanmagazine/ghbackup/ghbackup"
)
// Can be set in build step using -ldflags
var version string
const (
// Printed for -help, -h or with wrong number of arguments
usage = `Embarrassing simple GitHub backup tool
Usage: %s [flags] directory
directory path to save the repositories to
At least one of -account or -secret must be specified.
Flags:
`
more = "\nFor more visit https://qvl.io/ghbackup."
accountUsage = `GitHub user or organization name to get repositories from.
If not specified, all repositories the authenticated user has access to will be loaded.`
secretUsage = `Authentication secret for GitHub API.
Can use the users password or a personal access token (https://github.com/settings/tokens).
Authentication increases rate limiting (https://developer.github.com/v3/#rate-limiting) and enables backup of private repositories.`
)
// Get command line arguments and start updating repositories
func main() {
// Flags
account := flag.String("account", "", accountUsage)
secret := flag.String("secret", "", secretUsage)
versionFlag := flag.Bool("version", false, "Print binary version")
silent := flag.Bool("silent", false, "Suppress all output")
// Parse args
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage, os.Args[0])
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, more)
}
flag.Parse()
if *versionFlag {
fmt.Printf("ghbackup %s %s %s\n", version, runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
args := flag.Args()
if len(args) != 1 || (*account == "" && *secret == "") {
flag.Usage()
os.Exit(1)
}
logger := log.New(os.Stdout, "", 0)
if *silent {
logger = log.New(ioutil.Discard, "", 0)
}
err := ghbackup.Run(ghbackup.Config{
Account: *account,
Dir: args[0],
Secret: *secret,
Log: logger,
Err: log.New(os.Stderr, "", 0),
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}