Skip to content

Commit

Permalink
feat: auto generate drivers language json
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 26, 2022
1 parent d9ee174 commit 7425e00
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 27 deletions.
83 changes: 83 additions & 0 deletions cmd/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Package cmd
Copyright © 2022 Noah Hsu<[email protected]>
*/
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")
}
6 changes: 6 additions & 0 deletions drivers/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ import (
_ "github.com/alist-org/alist/v3/drivers/local"
_ "github.com/alist-org/alist/v3/drivers/virtual"
)

// All do nothing,just for import
// same as _ import
func All() {

}
9 changes: 5 additions & 4 deletions drivers/virtual/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (

type Addition struct {
driver.RootFolderPath
NumFile int `json:"num_file" 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"`
MinFileSize int64 `json:"min_file_size" type:"number" default:"1048576" required:"true"`
NumFile int `json:"num_file" 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"`
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{
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Item struct {
Name string `json:"name"`
Type string `json:"type"`
Default string `json:"default"`
Values string `json:"values"`
Options string `json:"options"`
Required bool `json:"required"`
Help string `json:"help"`
}
Expand Down
14 changes: 7 additions & 7 deletions internal/model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const (
)

type SettingItem struct {
Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key
Value string `json:"value"` // value
Help string `json:"help"` // help message
Type string `json:"type"` // string, number, bool, select
Values string `json:"values"` // values for select
Group int `json:"group"` // use to group setting in frontend
Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc.
Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key
Value string `json:"value"` // value
Help string `json:"help"` // help message
Type string `json:"type"` // string, number, bool, select
Options string `json:"options"` // values for select
Group int `json:"group"` // use to group setting in frontend
Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc.
}

func (s SettingItem) IsDeprecated() bool {
Expand Down
24 changes: 12 additions & 12 deletions internal/operations/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func getMainItems(config driver.Config) []driver.Item {
}, {
Name: "webdav_policy",
Type: conf.TypeSelect,
Values: "302_redirect, use_proxy_url, native_proxy",
Options: "302_redirect, use_proxy_url, native_proxy",
Default: "302_redirect",
Required: true,
},
Expand All @@ -95,25 +95,25 @@ func getMainItems(config driver.Config) []driver.Item {
Name: "webdav_policy",
Type: conf.TypeSelect,
Default: "native_proxy",
Values: "use_proxy_url, native_proxy",
Options: "use_proxy_url, native_proxy",
Required: true,
})
}
if config.LocalSort {
items = append(items, []driver.Item{{
Name: "order_by",
Type: conf.TypeSelect,
Values: "name,size,modified",
Name: "order_by",
Type: conf.TypeSelect,
Options: "name,size,modified",
}, {
Name: "order_direction",
Type: conf.TypeSelect,
Values: "asc,desc",
Name: "order_direction",
Type: conf.TypeSelect,
Options: "asc,desc",
}}...)
}
items = append(items, driver.Item{
Name: "extract_folder",
Type: conf.TypeSelect,
Values: "front,back",
Name: "extract_folder",
Type: conf.TypeSelect,
Options: "front,back",
})
return items
}
Expand All @@ -135,7 +135,7 @@ func getAdditionalItems(t reflect.Type, defaultRoot string) []driver.Item {
Name: tag.Get("json"),
Type: strings.ToLower(field.Type.Name()),
Default: tag.Get("default"),
Values: tag.Get("values"),
Options: tag.Get("options"),
Required: tag.Get("required") == "true",
Help: tag.Get("help"),
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
var Json = json.ConfigCompatibleWithStandardLibrary

// WriteJsonToFile write struct to json file
func WriteJsonToFile(src string, conf interface{}) bool {
data, err := Json.MarshalIndent(conf, "", " ")
func WriteJsonToFile(dst string, data interface{}) bool {
str, err := Json.MarshalIndent(data, "", " ")
if err != nil {
log.Errorf("failed convert Conf to []byte:%s", err.Error())
return false
}
err = ioutil.WriteFile(src, data, 0777)
err = ioutil.WriteFile(dst, str, 0777)
if err != nil {
log.Errorf("failed to write json file:%s", err.Error())
return false
Expand Down

0 comments on commit 7425e00

Please sign in to comment.