-
Notifications
You must be signed in to change notification settings - Fork 79
/
add.go
43 lines (39 loc) · 940 Bytes
/
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
package root
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/synctv-org/synctv/internal/bootstrap"
"github.com/synctv-org/synctv/internal/db"
)
var AddCmd = &cobra.Command{
Use: "add",
Short: "add root by user id",
Long: `add root by user id`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add(
bootstrap.InitDiscardLog,
bootstrap.InitConfig,
bootstrap.InitDatabase,
).Run()
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("missing user id")
}
u, err := db.GetUserByID(args[0])
if err != nil {
fmt.Printf("get user failed: %s", err)
return nil
}
if err := db.AddRoot(u); err != nil {
fmt.Printf("add root failed: %s", err)
return nil
}
fmt.Printf("add root success: %s\n", u.Username)
return nil
},
}
func init() {
RootCmd.AddCommand(AddCmd)
}