-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
54 lines (45 loc) · 1.24 KB
/
add.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
package main
import (
"fmt"
"github.com/spf13/cobra"
"os"
)
var AddCmd = &cobra.Command{
Use: "add <owner/name>",
Short: "Cache an additional repository's issues",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Invalid number of arguments.")
os.Exit(-1)
}
targetOwner, targetName := ParseRepo(args[0])
if len(targetOwner) == 0 {
fmt.Printf("Invalid repo name %q\n", args[0])
os.Exit(-1)
}
data, err := LoadData()
if err != nil {
fmt.Printf("Failed to load data with error: %q\n", err.Error())
os.Exit(-1)
}
for _, repo := range data.Repositories {
if repo.Owner.Name == targetOwner && repo.Name == targetName {
fmt.Printf("Repository %q is already cached.\nTo update the repository use:\n\tgir update %s/%s\n", args[0], targetOwner, targetName)
os.Exit(-1)
}
}
repo, err := LoadRepo(targetOwner, targetName)
if err != nil {
fmt.Printf("Failed to cache repository %s/%s: %q\n", targetOwner, targetName, err.Error())
os.Exit(-1)
}
data.Repositories = append(data.Repositories, repo)
if err := SaveData(data); err != nil {
fmt.Printf("Failed to write to disk: %q\n", err.Error())
os.Exit(-1)
}
},
}
func init() {
RootCmd.AddCommand(AddCmd)
}