feat: auto generate drivers language json
This commit is contained in:
parent
d9ee174dd3
commit
7425e001db
83
cmd/lang.go
Normal file
83
cmd/lang.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
Package cmd
|
||||||
|
Copyright © 2022 Noah Hsu<i@nn.ci>
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
_ "github.com/alist-org/alist/v3/drivers"
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
"github.com/alist-org/alist/v3/internal/operations"
|
||||||
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
type KV[V any] map[string]V
|
||||||
|
|
||||||
|
type Drivers KV[KV[interface{}]]
|
||||||
|
|
||||||
|
func firstUpper(s string) string {
|
||||||
|
if s == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.ToUpper(s[:1]) + s[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func convert(s string) string {
|
||||||
|
ss := strings.Split(s, "_")
|
||||||
|
ans := strings.Join(ss, " ")
|
||||||
|
return firstUpper(ans)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateDriversJson() {
|
||||||
|
drivers := make(Drivers)
|
||||||
|
drivers["drivers"] = make(KV[interface{}])
|
||||||
|
driverItemsMap := operations.GetDriverItemsMap()
|
||||||
|
for k, v := range driverItemsMap {
|
||||||
|
drivers["drivers"][k] = k
|
||||||
|
items := make(KV[interface{}])
|
||||||
|
for i := range v.Additional {
|
||||||
|
item := v.Additional[i]
|
||||||
|
items[item.Name] = convert(item.Name)
|
||||||
|
if item.Help != "" {
|
||||||
|
items[fmt.Sprintf("%s-tips", item.Name)] = item.Help
|
||||||
|
}
|
||||||
|
if item.Type == conf.TypeSelect && len(item.Options) > 0 {
|
||||||
|
options := make(KV[string])
|
||||||
|
_options := strings.Split(item.Options, ",")
|
||||||
|
for _, o := range _options {
|
||||||
|
options[o] = convert(o)
|
||||||
|
}
|
||||||
|
items[fmt.Sprintf("%ss", item.Name)] = options
|
||||||
|
}
|
||||||
|
}
|
||||||
|
drivers[k] = items
|
||||||
|
}
|
||||||
|
utils.WriteJsonToFile("drivers.json", drivers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// langCmd represents the lang command
|
||||||
|
var langCmd = &cobra.Command{
|
||||||
|
Use: "lang",
|
||||||
|
Short: "Generate language json file",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
generateDriversJson()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(langCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// langCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// langCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
@ -4,3 +4,9 @@ import (
|
|||||||
_ "github.com/alist-org/alist/v3/drivers/local"
|
_ "github.com/alist-org/alist/v3/drivers/local"
|
||||||
_ "github.com/alist-org/alist/v3/drivers/virtual"
|
_ "github.com/alist-org/alist/v3/drivers/virtual"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// All do nothing,just for import
|
||||||
|
// same as _ import
|
||||||
|
func All() {
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -7,10 +7,11 @@ import (
|
|||||||
|
|
||||||
type Addition struct {
|
type Addition struct {
|
||||||
driver.RootFolderPath
|
driver.RootFolderPath
|
||||||
NumFile int `json:"num_file" type:"number" default:"30" required:"true"`
|
NumFile int `json:"num_file" type:"number" default:"30" required:"true"`
|
||||||
NumFolder int `json:"num_folder" type:"number" default:"30" required:"true"`
|
NumFolder int `json:"num_folder" type:"number" default:"30" required:"true"`
|
||||||
MaxFileSize int64 `json:"max_file_size" type:"number" default:"1073741824" required:"true"`
|
MaxFileSize int64 `json:"max_file_size" type:"number" default:"1073741824" required:"true"`
|
||||||
MinFileSize int64 `json:"min_file_size" type:"number" default:"1048576" required:"true"`
|
MinFileSize int64 `json:"min_file_size" type:"number" default:"1048576" required:"true"`
|
||||||
|
TestSelect string `json:"test_select" type:"select" options:"a,b,c"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = driver.Config{
|
var config = driver.Config{
|
||||||
|
@ -8,7 +8,7 @@ type Item struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Default string `json:"default"`
|
Default string `json:"default"`
|
||||||
Values string `json:"values"`
|
Options string `json:"options"`
|
||||||
Required bool `json:"required"`
|
Required bool `json:"required"`
|
||||||
Help string `json:"help"`
|
Help string `json:"help"`
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SettingItem struct {
|
type SettingItem struct {
|
||||||
Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key
|
Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key
|
||||||
Value string `json:"value"` // value
|
Value string `json:"value"` // value
|
||||||
Help string `json:"help"` // help message
|
Help string `json:"help"` // help message
|
||||||
Type string `json:"type"` // string, number, bool, select
|
Type string `json:"type"` // string, number, bool, select
|
||||||
Values string `json:"values"` // values for select
|
Options string `json:"options"` // values for select
|
||||||
Group int `json:"group"` // use to group setting in frontend
|
Group int `json:"group"` // use to group setting in frontend
|
||||||
Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc.
|
Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SettingItem) IsDeprecated() bool {
|
func (s SettingItem) IsDeprecated() bool {
|
||||||
|
@ -85,7 +85,7 @@ func getMainItems(config driver.Config) []driver.Item {
|
|||||||
}, {
|
}, {
|
||||||
Name: "webdav_policy",
|
Name: "webdav_policy",
|
||||||
Type: conf.TypeSelect,
|
Type: conf.TypeSelect,
|
||||||
Values: "302_redirect, use_proxy_url, native_proxy",
|
Options: "302_redirect, use_proxy_url, native_proxy",
|
||||||
Default: "302_redirect",
|
Default: "302_redirect",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
@ -95,25 +95,25 @@ func getMainItems(config driver.Config) []driver.Item {
|
|||||||
Name: "webdav_policy",
|
Name: "webdav_policy",
|
||||||
Type: conf.TypeSelect,
|
Type: conf.TypeSelect,
|
||||||
Default: "native_proxy",
|
Default: "native_proxy",
|
||||||
Values: "use_proxy_url, native_proxy",
|
Options: "use_proxy_url, native_proxy",
|
||||||
Required: true,
|
Required: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if config.LocalSort {
|
if config.LocalSort {
|
||||||
items = append(items, []driver.Item{{
|
items = append(items, []driver.Item{{
|
||||||
Name: "order_by",
|
Name: "order_by",
|
||||||
Type: conf.TypeSelect,
|
Type: conf.TypeSelect,
|
||||||
Values: "name,size,modified",
|
Options: "name,size,modified",
|
||||||
}, {
|
}, {
|
||||||
Name: "order_direction",
|
Name: "order_direction",
|
||||||
Type: conf.TypeSelect,
|
Type: conf.TypeSelect,
|
||||||
Values: "asc,desc",
|
Options: "asc,desc",
|
||||||
}}...)
|
}}...)
|
||||||
}
|
}
|
||||||
items = append(items, driver.Item{
|
items = append(items, driver.Item{
|
||||||
Name: "extract_folder",
|
Name: "extract_folder",
|
||||||
Type: conf.TypeSelect,
|
Type: conf.TypeSelect,
|
||||||
Values: "front,back",
|
Options: "front,back",
|
||||||
})
|
})
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
@ -135,7 +135,7 @@ func getAdditionalItems(t reflect.Type, defaultRoot string) []driver.Item {
|
|||||||
Name: tag.Get("json"),
|
Name: tag.Get("json"),
|
||||||
Type: strings.ToLower(field.Type.Name()),
|
Type: strings.ToLower(field.Type.Name()),
|
||||||
Default: tag.Get("default"),
|
Default: tag.Get("default"),
|
||||||
Values: tag.Get("values"),
|
Options: tag.Get("options"),
|
||||||
Required: tag.Get("required") == "true",
|
Required: tag.Get("required") == "true",
|
||||||
Help: tag.Get("help"),
|
Help: tag.Get("help"),
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,13 @@ import (
|
|||||||
var Json = json.ConfigCompatibleWithStandardLibrary
|
var Json = json.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
// WriteJsonToFile write struct to json file
|
// WriteJsonToFile write struct to json file
|
||||||
func WriteJsonToFile(src string, conf interface{}) bool {
|
func WriteJsonToFile(dst string, data interface{}) bool {
|
||||||
data, err := Json.MarshalIndent(conf, "", " ")
|
str, err := Json.MarshalIndent(data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed convert Conf to []byte:%s", err.Error())
|
log.Errorf("failed convert Conf to []byte:%s", err.Error())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(src, data, 0777)
|
err = ioutil.WriteFile(dst, str, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to write json file:%s", err.Error())
|
log.Errorf("failed to write json file:%s", err.Error())
|
||||||
return false
|
return false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user