Skip to content

Commit ae755db

Browse files
committed
feat: driver additional items parse
1 parent 677047c commit ae755db

File tree

6 files changed

+97
-6
lines changed

6 files changed

+97
-6
lines changed

drivers/local/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (d *Driver) Drop(ctx context.Context) error {
3737
return nil
3838
}
3939

40-
func (d *Driver) GetAccount() model.Account {
41-
return d.Account
40+
func (d *Driver) GetAddition() driver.Additional {
41+
return d.Addition
4242
}
4343

4444
func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) {

drivers/local/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package local
33
import "github.com/alist-org/alist/v3/internal/driver"
44

55
type Addition struct {
6-
RootFolder string `json:"root_folder" type:"string" desc:"root folder path" default:"/"`
6+
RootFolder string `json:"root_folder" type:"string" help:"root folder path" default:"/"`
77
}
88

99
var config = driver.Config{

internal/driver/addition.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package driver
22

3-
type Additional interface {
4-
}
3+
type Additional interface{}
54

65
type Item struct {
76
Name string `json:"name"`
87
Type string `json:"type"`
98
Default string `json:"default"`
109
Values string `json:"values"`
1110
Required bool `json:"required"`
12-
Desc string `json:"desc"`
11+
Help string `json:"help"`
12+
}
13+
14+
type Items struct {
15+
Main []Item `json:"main"`
16+
Additional []Item `json:"additional"`
1317
}

internal/driver/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Meta interface {
1919
Drop(ctx context.Context) error
2020
// GetAccount transform additional field to string and assign to account's addition
2121
GetAccount() model.Account
22+
GetAddition() Additional
2223
}
2324

2425
type Other interface {

internal/driver/manage.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,95 @@ package driver
22

33
import (
44
log "github.com/sirupsen/logrus"
5+
"reflect"
56
)
67

78
type New func() Driver
89

910
var driversMap = map[string]New{}
11+
var driverItemsMap = map[string]Items{}
1012

1113
func 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+
}

internal/model/account.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ type Proxy struct {
2424
WebdavDirect bool `json:"webdav_direct"`
2525
DownProxyUrl string `json:"down_proxy_url"`
2626
}
27+
28+
func (a Account) GetAccount() Account {
29+
return a
30+
}

0 commit comments

Comments
 (0)