-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (104 loc) · 4.03 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
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
112
113
114
115
116
117
package main
import (
"fmt"
"syscall"
"github.com/go-resty/resty/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/term"
)
var (
isDebug, skip bool
client = resty.New()
Logger = logrus.New()
ImageExtension = []string{".jpg", ".jpeg", ".png", ".gif", ".webm", ".mp4"}
)
var (
userToken string
host, userId, userPass string
safety string
userCookieName, userCookieValue string
)
func main() {
var (
cmdUpload = &cobra.Command{
Use: "upload <forder path> <tag>",
Long: "upload one folder to szurubooru host\n* forder path: path to target directory\n* tag: tag which will be assigned to images, can use comma seperated multiple tags",
Short: "upload one folder to szurubooru host",
Args: cobra.MinimumNArgs(1),
Run: execUpload,
PersistentPreRun: credentialInput,
}
cmdBatchUpload = &cobra.Command{
Use: "bupload <root forder path> <handler>",
Long: "batch upload multiple folder to szurubooru host\n* forder path: path to root target directory\n*handler: pixiv=%artist (%user_number), name=%artist, split=%tag1 %tag2...",
Short: "batch upload multiple folder to szurubooru host",
Args: cobra.ExactArgs(2),
Run: execBatchUpload,
PersistentPreRun: credentialInput,
}
cmdDelete = &cobra.Command{
Use: "delete <tag> <except favorite>",
Long: "delete posts which have given tag\n* tag: tag for deleting\n* except favorite: true=delete except favorited post, false = delete all posts",
Short: "delete posts which have given tag",
Args: cobra.ExactArgs(2),
Run: execDelete,
PersistentPreRun: credentialInput,
}
cmdSanitize = &cobra.Command{
Use: "sanitize <implication>",
Long: "delete posts which have belong on tag without favorited which has specific implication tag\n* implication: target implication tag",
Short: "sanitize posts belong in appointed condition",
Args: cobra.ExactArgs(1),
Run: execBatchDelete,
PersistentPreRun: credentialInput,
}
)
Logger.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableColors: false,
ForceQuote: false,
})
var rootCmd = &cobra.Command{Use: "app",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
},
}
cobra.OnInitialize(func() {
if isDebug {
Logger.Infof("Debug logging is enabled!")
Logger.SetLevel(logrus.DebugLevel)
client.SetLogger(Logger)
client.SetDebug(true)
}
})
rootCmd.AddCommand(cmdUpload, cmdBatchUpload, cmdDelete, cmdSanitize)
rootCmd.PersistentFlags().StringVar(&host, "host", "http://localhost", "address of host")
rootCmd.PersistentFlags().StringVar(&safety, "safety", "unsafe", "safety of images in directory")
rootCmd.PersistentFlags().StringVar(&userId, "uid", "", "user's login id")
rootCmd.PersistentFlags().StringVar(&userPass, "upw", "", "user's login password")
rootCmd.PersistentFlags().BoolVar(&isDebug, "debug", false, "print debug log")
rootCmd.PersistentFlags().StringVar(&userCookieName, "ckname", "", "user cookie name (when set, ./cookie.txt is read and used as value. Disabled if blank)")
rootCmd.PersistentFlags().BoolVar(&skip, "y", false, "assume every continue checking input as 'yes")
rootCmd.Execute()
}
func credentialInput(cmd *cobra.Command, args []string) {
var err error
Logger.Infof("using szurubooru host as '%s'\n", host)
if len(userId) < 1 {
fmt.Print("enter user id : ")
fmt.Scanln(&userId)
}
if len(userPass) < 1 {
fmt.Print("enter user password : ")
if bytepw, err := term.ReadPassword(int(syscall.Stdin)); err != nil {
Logger.WithError(err).Fatalln("fail to read password input")
} else {
userPass = string(bytepw)
fmt.Println("")
}
}
if userToken, err = login(host, userId, userPass); err != nil {
Logger.WithError(err).Fatalln("fail to log in")
}
Logger.Infoln("login successfully")
}