Quellcode durchsuchen

Introduce a config struct and cmdline flags

Lucas Stadler vor 10 Jahren
Ursprung
Commit
bef8023f7c
1 geänderte Dateien mit 19 neuen und 8 gelöschten Zeilen
  1. 19 8
      go/stars/stars.go

+ 19 - 8
go/stars/stars.go

@ -3,6 +3,7 @@ package main
3 3
4 4
import (
5 5
	"encoding/json"
6
	"flag"
6 7
	"fmt"
7 8
	"os"
8 9
	"os/exec"
@ -12,11 +13,21 @@ import (
12 13
	"time"
13 14
)
14 15
15
var userName = "heyLu"
16
var directory = "github-stars"
17
var concurrency = 10
16
var config struct {
17
	userName    string
18
	directory   string
19
	concurrency int
20
}
21
22
func init() {
23
	flag.StringVar(&config.userName, "user", "heyLu", "The GitHub user to fetch stars for")
24
	flag.StringVar(&config.directory, "directory", "github-stars", "The directory to store the repos in")
25
	flag.IntVar(&config.concurrency, "concurrency", 10, "The number of repos to update concurrently")
26
}
18 27
19 28
func main() {
29
	flag.Parse()
30
20 31
	var stars []starInfo
21 32
	decoder := json.NewDecoder(os.Stdin)
22 33
	err := decoder.Decode(&stars)
@ -24,7 +35,7 @@ func main() {
24 35
		panic(err)
25 36
	}
26 37
27
	sem := make(chan bool, 10)
38
	sem := make(chan bool, config.concurrency)
28 39
	var wg sync.WaitGroup
29 40
30 41
	wg.Add(len(stars))
@ -49,7 +60,7 @@ func main() {
49 60
}
50 61
51 62
func updateRepo(info starInfo) error {
52
	f, err := os.Open(path.Join(directory, info.RepoName))
63
	f, err := os.Open(path.Join(config.directory, info.RepoName))
53 64
	if err != nil {
54 65
		if os.IsNotExist(err) {
55 66
			return gitClone(info)
@ -73,17 +84,17 @@ func updateRepo(info starInfo) error {
73 84
74 85
func gitClone(info starInfo) error {
75 86
	cmd := exec.Command("git", "clone", info.CloneUrl,
76
		path.Join(directory, info.RepoName))
87
		path.Join(config.directory, info.RepoName))
77 88
	return cmd.Run()
78 89
}
79 90
80 91
func gitPull(info starInfo) error {
81
	cmd := exec.Command("git", "-C", path.Join(directory, info.RepoName), "pull")
92
	cmd := exec.Command("git", "-C", path.Join(config.directory, info.RepoName), "pull")
82 93
	return cmd.Run()
83 94
}
84 95
85 96
func gitLastCommit(info starInfo) (time.Time, error) {
86
	cmd := exec.Command("git", "-C", path.Join(directory, info.RepoName),
97
	cmd := exec.Command("git", "-C", path.Join(config.directory, info.RepoName),
87 98
		"log", "-n", "1", "--format=%cd", "--date=iso8601-strict")
88 99
	out, err := cmd.Output()
89 100
	if err != nil {