@@ -2,13 +2,95 @@ package driver
22
33import (
44 log "github.com/sirupsen/logrus"
5+ "reflect"
56)
67
78type New func () Driver
89
910var driversMap = map [string ]New {}
11+ var driverItemsMap = map [string ]Items {}
1012
1113func RegisterDriver (config Config , driver New ) {
1214 log .Infof ("register driver: [%s]" , config .Name )
15+ registerDriverItems (config , driver ().GetAddition ())
1316 driversMap [config .Name ] = driver
1417}
18+
19+ func registerDriverItems (config Config , addition Additional ) {
20+ tAddition := reflect .TypeOf (addition )
21+ mainItems := getMainItems (config )
22+ additionalItems := getAdditionalItems (tAddition )
23+ driverItemsMap [config .Name ] = Items {mainItems , additionalItems }
24+ }
25+
26+ func getMainItems (config Config ) []Item {
27+ items := []Item {{
28+ Name : "virtual_path" ,
29+ Type : "string" ,
30+ Required : true ,
31+ Help : "" ,
32+ }, {
33+ Name : "index" ,
34+ Type : "int" ,
35+ Help : "use to sort" ,
36+ }, {
37+ Name : "down_proxy_url" ,
38+ Type : "text" ,
39+ }, {
40+ Name : "webdav_direct" ,
41+ Type : "bool" ,
42+ Help : "Transfer the WebDAV of this account through the native without redirect" ,
43+ }}
44+ if ! config .OnlyProxy && ! config .OnlyLocal {
45+ items = append (items , []Item {{
46+ Name : "web_proxy" ,
47+ Type : "bool" ,
48+ }, {
49+ Name : "webdav_proxy" ,
50+ Type : "bool" ,
51+ },
52+ }... )
53+ }
54+ if config .LocalSort {
55+ items = append (items , []Item {{
56+ Name : "order_by" ,
57+ Type : "select" ,
58+ Values : "name,size,updated_at" ,
59+ }, {
60+ Name : "order_direction" ,
61+ Type : "select" ,
62+ Values : "ASC,DESC" ,
63+ }}... )
64+ }
65+ items = append (items , Item {
66+ Name : "extract_folder" ,
67+ Values : "front,back" ,
68+ Type : "select" ,
69+ })
70+ return items
71+ }
72+
73+ func getAdditionalItems (t reflect.Type ) []Item {
74+ var items []Item
75+ for i := 0 ; i < t .NumField (); i ++ {
76+ tag := t .Field (i ).Tag
77+ ignore , ok := tag .Lookup ("ignore" )
78+ if ! ok || ignore == "false" {
79+ continue
80+ }
81+ item := Item {
82+ Name : tag .Get ("json" ),
83+ Type : tag .Get ("type" ),
84+ Default : tag .Get ("default" ),
85+ Values : tag .Get ("values" ),
86+ Required : tag .Get ("required" ) == "true" ,
87+ Help : tag .Get ("help" ),
88+ }
89+ // set default type to string
90+ if item .Type == "" {
91+ item .Type = "string"
92+ }
93+ items = append (items , item )
94+ }
95+ return items
96+ }
0 commit comments