forked from liuzhuoling2011/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (108 loc) · 2.73 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
118
package main
import (
"fmt"
"os"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
_ "github.com/iikira/BaiduPCS-Go/internal/pcsinit"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/internal/pcsweb"
"github.com/iikira/BaiduPCS-Go/pcsverbose"
"github.com/urfave/cli"
)
var (
Version = "3.6.1"
reloadFn = func(c *cli.Context) error {
err := pcsconfig.Config.Reload()
if err != nil {
fmt.Printf("重载配置错误: %s\n", err)
}
return nil
}
)
func init() {
pcsutil.ChWorkDir()
err := pcsconfig.Config.Init()
switch err {
case nil:
case pcsconfig.ErrConfigFileNoPermission, pcsconfig.ErrConfigContentsParseError:
fmt.Fprintf(os.Stderr, "FATAL ERROR: config file error: %s\n", err)
os.Exit(1)
default:
fmt.Printf("WARNING: config init error: %s\n", err)
}
// 启动缓存回收
requester.TCPAddrCache.GC()
}
func main() {
defer pcsconfig.Config.Close()
app := cli.NewApp()
app.Name = "BaiduPCS-Go"
app.Version = Version
liuzhuoling := cli.Author{
Name: "liuzhuoling",
Email: "[email protected]",
}
iikira := cli.Author{
Name: "iikira",
Email: "[email protected]",
}
app.Authors = []cli.Author{liuzhuoling, iikira}
app.Description = "这个软件可以让你高效的使用百度云"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "启用调试",
EnvVar: pcsverbose.EnvVerbose,
Destination: &pcsverbose.IsVerbose,
},
}
app.Action = func(c *cli.Context) {
fmt.Printf("打开浏览器, 输入 http://localhost:5299 查看效果\n")
if err := pcsweb.StartServer(5299); err != nil{
fmt.Println(err.Error())
}
}
app.Commands = []cli.Command{
{
Name: "web",
Usage: "启用 web 客户端",
Category: "其他",
Before: reloadFn,
Action: func(c *cli.Context) error {
fmt.Printf("打开浏览器, 输入: http://localhost:%d 查看效果\n", c.Uint("port"))
fmt.Println(pcsweb.StartServer(c.Uint("port")))
return nil
},
Flags: []cli.Flag{
cli.UintFlag{
Name: "port",
Usage: "自定义端口",
Value: 5299,
},
},
},
{
Name: "env",
Usage: "显示程序环境变量",
Category: "其他",
Action: func(c *cli.Context) error {
envStr := "%s=\"%s\"\n"
envVar, ok := os.LookupEnv(pcsverbose.EnvVerbose)
if ok {
fmt.Printf(envStr, pcsverbose.EnvVerbose, envVar)
} else {
fmt.Printf(envStr, pcsverbose.EnvVerbose, "0")
}
envVar, ok = os.LookupEnv(pcsconfig.EnvConfigDir)
if ok {
fmt.Printf(envStr, pcsconfig.EnvConfigDir, envVar)
} else {
fmt.Printf(envStr, pcsconfig.EnvConfigDir, pcsconfig.GetConfigDir())
}
return nil
},
},
}
app.Run(os.Args)
}