Skip to content

Commit

Permalink
feat: driver additional items parse
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 8, 2022
1 parent 677047c commit ae755db
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
4 changes: 2 additions & 2 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (d *Driver) Drop(ctx context.Context) error {
return nil
}

func (d *Driver) GetAccount() model.Account {
return d.Account
func (d *Driver) GetAddition() driver.Additional {
return d.Addition
}

func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) {
Expand Down
2 changes: 1 addition & 1 deletion drivers/local/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package local
import "github.com/alist-org/alist/v3/internal/driver"

type Addition struct {
RootFolder string `json:"root_folder" type:"string" desc:"root folder path" default:"/"`
RootFolder string `json:"root_folder" type:"string" help:"root folder path" default:"/"`
}

var config = driver.Config{
Expand Down
10 changes: 7 additions & 3 deletions internal/driver/addition.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package driver

type Additional interface {
}
type Additional interface{}

type Item struct {
Name string `json:"name"`
Type string `json:"type"`
Default string `json:"default"`
Values string `json:"values"`
Required bool `json:"required"`
Desc string `json:"desc"`
Help string `json:"help"`
}

type Items struct {
Main []Item `json:"main"`
Additional []Item `json:"additional"`
}
1 change: 1 addition & 0 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Meta interface {
Drop(ctx context.Context) error
// GetAccount transform additional field to string and assign to account's addition
GetAccount() model.Account
GetAddition() Additional
}

type Other interface {
Expand Down
82 changes: 82 additions & 0 deletions internal/driver/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,95 @@ package driver

import (
log "github.com/sirupsen/logrus"
"reflect"
)

type New func() Driver

var driversMap = map[string]New{}
var driverItemsMap = map[string]Items{}

func RegisterDriver(config Config, driver New) {
log.Infof("register driver: [%s]", config.Name)
registerDriverItems(config, driver().GetAddition())
driversMap[config.Name] = driver
}

func registerDriverItems(config Config, addition Additional) {
tAddition := reflect.TypeOf(addition)
mainItems := getMainItems(config)
additionalItems := getAdditionalItems(tAddition)
driverItemsMap[config.Name] = Items{mainItems, additionalItems}
}

func getMainItems(config Config) []Item {
items := []Item{{
Name: "virtual_path",
Type: "string",
Required: true,
Help: "",
}, {
Name: "index",
Type: "int",
Help: "use to sort",
}, {
Name: "down_proxy_url",
Type: "text",
}, {
Name: "webdav_direct",
Type: "bool",
Help: "Transfer the WebDAV of this account through the native without redirect",
}}
if !config.OnlyProxy && !config.OnlyLocal {
items = append(items, []Item{{
Name: "web_proxy",
Type: "bool",
}, {
Name: "webdav_proxy",
Type: "bool",
},
}...)
}
if config.LocalSort {
items = append(items, []Item{{
Name: "order_by",
Type: "select",
Values: "name,size,updated_at",
}, {
Name: "order_direction",
Type: "select",
Values: "ASC,DESC",
}}...)
}
items = append(items, Item{
Name: "extract_folder",
Values: "front,back",
Type: "select",
})
return items
}

func getAdditionalItems(t reflect.Type) []Item {
var items []Item
for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag
ignore, ok := tag.Lookup("ignore")
if !ok || ignore == "false" {
continue
}
item := Item{
Name: tag.Get("json"),
Type: tag.Get("type"),
Default: tag.Get("default"),
Values: tag.Get("values"),
Required: tag.Get("required") == "true",
Help: tag.Get("help"),
}
// set default type to string
if item.Type == "" {
item.Type = "string"
}
items = append(items, item)
}
return items
}
4 changes: 4 additions & 0 deletions internal/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ type Proxy struct {
WebdavDirect bool `json:"webdav_direct"`
DownProxyUrl string `json:"down_proxy_url"`
}

func (a Account) GetAccount() Account {
return a
}

0 comments on commit ae755db

Please sign in to comment.