diff --git a/cmd/lang.go b/cmd/lang.go new file mode 100644 index 00000000000..19e5cd20bd8 --- /dev/null +++ b/cmd/lang.go @@ -0,0 +1,83 @@ +/* +Package cmd +Copyright © 2022 Noah Hsu +*/ +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") +} diff --git a/drivers/all.go b/drivers/all.go index 17cdcee9461..f93a3ad4f53 100644 --- a/drivers/all.go +++ b/drivers/all.go @@ -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() { + +} diff --git a/drivers/virtual/meta.go b/drivers/virtual/meta.go index 13575f938d2..5b621766742 100644 --- a/drivers/virtual/meta.go +++ b/drivers/virtual/meta.go @@ -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{ diff --git a/internal/driver/item.go b/internal/driver/item.go index 264c2eeddd8..b1ec55a1d22 100644 --- a/internal/driver/item.go +++ b/internal/driver/item.go @@ -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"` } diff --git a/internal/model/setting.go b/internal/model/setting.go index 97dc4625ef4..0452bf4ac1c 100644 --- a/internal/model/setting.go +++ b/internal/model/setting.go @@ -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 { diff --git a/internal/operations/driver.go b/internal/operations/driver.go index ee52a731b77..b704dd944b0 100644 --- a/internal/operations/driver.go +++ b/internal/operations/driver.go @@ -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, }, @@ -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 } @@ -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"), } diff --git a/pkg/utils/json.go b/pkg/utils/json.go index f4389e6bb24..cfb1b77906a 100644 --- a/pkg/utils/json.go +++ b/pkg/utils/json.go @@ -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