-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg.go
111 lines (100 loc) · 2.82 KB
/
pkg.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"strconv"
"github.com/finkf/pcwgo/api"
"github.com/spf13/cobra"
)
func init() {
pkgSplitCommand.Flags().BoolVarP(&opts.pkg.split.random, "random", "r",
false, "create random packages")
}
var pkgCommand = cobra.Command{
Use: "pkg",
Short: "Assign and reassign packages.",
}
var pkgAssignCommand = cobra.Command{
Use: "assign ID [USERID]",
Short: "Assign the package ID to the user USERID",
RunE: doAssign,
Args: exactArgs(1, 2),
Long: `
Assign the package ID to a user. If USERID is omitted, the package is
assigned back to its original owner. Otherwise it is assigned to the
user with the given USERID.`,
}
func doAssign(_ *cobra.Command, args []string) error {
var ids []int
for _, arg := range args {
id, err := strconv.Atoi(arg)
if err != nil {
return fmt.Errorf("cannot assign package: invalid id: %s", arg)
}
ids = append(ids, id)
}
c := authenticate()
var err error
switch len(ids) {
case 2:
err = get(c, c.URL("pkg/assign/books/%d?assignto=%d", ids[0], ids[1]), nil)
default:
err = get(c, c.URL("pkg/assign/books/%d", ids[0]), nil)
}
if err != nil {
return fmt.Errorf("cannot assign package %d: %v", ids[0], err)
}
return nil
}
var pkgReassignCommand = cobra.Command{
Use: "reassign ID",
Short: "Reassign packages of book ID to its owner",
RunE: doReassign,
Args: cobra.ExactArgs(1),
Long: `
Reassign all packages of the book ID that are owned by different users
are to the owner of the project.`,
}
func doReassign(cmd *cobra.Command, args []string) error {
var pid int
if n := parseIDs(args[0], &pid); n != 1 {
return fmt.Errorf("cannot reassign: invalid id: %s", args[0])
}
c := authenticate()
if err := get(c, c.URL("pkg/takeback/books/%d", pid), nil); err != nil {
return fmt.Errorf("cannot reassign package %d: %v", pid, err)
}
return nil
}
var pkgSplitCommand = cobra.Command{
Use: "split ID USERID [USERID...]",
Short: "Split the project ID into multiple packages",
RunE: doSplit,
Args: cobra.MinimumNArgs(2),
Long: `
Split the project ID into multiple packages. The project is split
into N packages where N is the number of given USERIDs. Each project
is assigned to the given users in order.
E.g. "pocowebc new pkgs 13 1 2 3" splits the project 13 into 3
packages. The first package is owned by user 1, the second by user 2
and the third by user 3.`,
}
func doSplit(cmd *cobra.Command, args []string) error {
c := authenticate()
var ids []int
for _, arg := range args {
id, err := strconv.Atoi(arg)
if err != nil {
return fmt.Errorf("cannot split: invalid id: %s", arg)
}
ids = append(ids, id)
}
url := c.URL("pkg/split/books/%d", ids[0])
err := post(c, url, api.SplitRequest{
UserIDs: ids[1:],
Random: opts.pkg.split.random,
}, nil)
if err != nil {
return fmt.Errorf("cannot split %d: %v", ids[0], err)
}
return nil
}