1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 11:54:41 -05:00

add: output 参数

This commit is contained in:
Nite07 2024-03-11 19:56:50 +08:00
parent cde033324a
commit b8cf0559fa
2 changed files with 39 additions and 5 deletions

View File

@ -1 +1,17 @@
# 开发中... # sub2sing-box
## 使用指南
```
sub2sing-box convert --help
Convert common proxy to json
Usage:
convert [flags]
Flags:
-h, --help help for convert
-p, --proxy strings common proxies
-s, --subscription strings subscription urls
-t, --template string path of template file
```

View File

@ -17,12 +17,13 @@ import (
var convertCmd = &cobra.Command{ var convertCmd = &cobra.Command{
Use: "convert", Use: "convert",
Long: "Convert common proxy format to json", Long: "Convert common proxy to sing-box proxy",
Short: "Convert common proxy format to json", Short: "Convert common proxy to sing-box proxy",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
subscriptions, _ := cmd.Flags().GetStringSlice("subscription") subscriptions, _ := cmd.Flags().GetStringSlice("subscription")
proxies, _ := cmd.Flags().GetStringSlice("proxy") proxies, _ := cmd.Flags().GetStringSlice("proxy")
template, _ := cmd.Flags().GetString("template") template, _ := cmd.Flags().GetString("template")
output, _ := cmd.Flags().GetString("output")
if template == "" { if template == "" {
proxyList, err := ConvertSubscriptionsToSProxy(subscriptions) proxyList, err := ConvertSubscriptionsToSProxy(subscriptions)
if err != nil { if err != nil {
@ -42,7 +43,15 @@ var convertCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
return return
} }
if output != "" {
err = os.WriteFile(output, result, 0666)
if err != nil {
fmt.Println(err)
return
}
} else {
fmt.Println(string(result)) fmt.Println(string(result))
}
} else { } else {
config, err := ConvertWithTemplate(subscriptions, proxies, template) config, err := ConvertWithTemplate(subscriptions, proxies, template)
if err != nil { if err != nil {
@ -54,8 +63,16 @@ var convertCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
return return
} }
if output != "" {
err = os.WriteFile(output, data, 0666)
if err != nil {
fmt.Println(err)
return
}
} else {
fmt.Println(string(data)) fmt.Println(string(data))
} }
}
}, },
} }
@ -63,6 +80,7 @@ func init() {
convertCmd.Flags().StringSliceP("subscription", "s", []string{}, "subscription urls") convertCmd.Flags().StringSliceP("subscription", "s", []string{}, "subscription urls")
convertCmd.Flags().StringSliceP("proxy", "p", []string{}, "common proxies") convertCmd.Flags().StringSliceP("proxy", "p", []string{}, "common proxies")
convertCmd.Flags().StringP("template", "t", "", "path of template file") convertCmd.Flags().StringP("template", "t", "", "path of template file")
convertCmd.Flags().StringP("output", "o", "", "output file path")
RootCmd.AddCommand(convertCmd) RootCmd.AddCommand(convertCmd)
} }