mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-17 10:35:11 +00:00
cmd/syncthing: Refactor CLI code to allow passing CLI arguments via function parameter (#8485)
Currently the `Run()` function of the CLI always uses `os.Args` directly. This change adds an additional `RunWithArgs()` function that allows passing arguments as `[]string` instead. This is useful when linking against Syncthing as a library to be able to also expose the CLI.
This commit is contained in:
parent
209e68c1ba
commit
373859be83
@ -38,7 +38,16 @@ func Run() error {
|
|||||||
// add flags there...
|
// add flags there...
|
||||||
c := preCli{}
|
c := preCli{}
|
||||||
parseFlags(&c)
|
parseFlags(&c)
|
||||||
|
return runInternal(c, os.Args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunWithArgs(cliArgs []string) error {
|
||||||
|
c := preCli{}
|
||||||
|
parseFlagsWithArgs(cliArgs, &c)
|
||||||
|
return runInternal(c, cliArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runInternal(c preCli, cliArgs []string) error {
|
||||||
// Not set as default above because the strings can be really long.
|
// Not set as default above because the strings can be really long.
|
||||||
err := cmdutil.SetConfigDataLocationsFromFlags(c.HomeDir, c.ConfDir, c.DataDir)
|
err := cmdutil.SetConfigDataLocationsFromFlags(c.HomeDir, c.ConfDir, c.DataDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -107,8 +116,8 @@ func Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Drop the `-` not to recurse into self.
|
// Drop the `-` not to recurse into self.
|
||||||
args := make([]string, len(os.Args)-1)
|
args := make([]string, len(cliArgs)-1)
|
||||||
copy(args, os.Args)
|
copy(args, cliArgs)
|
||||||
|
|
||||||
fmt.Println("Reading commands from stdin...", args)
|
fmt.Println("Reading commands from stdin...", args)
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
@ -131,7 +140,7 @@ func Run() error {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
return app.Run(os.Args)
|
return app.Run(cliArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlags(c *preCli) error {
|
func parseFlags(c *preCli) error {
|
||||||
@ -140,7 +149,10 @@ func parseFlags(c *preCli) error {
|
|||||||
if len(os.Args) <= 2 {
|
if len(os.Args) <= 2 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
args := os.Args[2:]
|
return parseFlagsWithArgs(os.Args[2:], c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseFlagsWithArgs(args []string, c *preCli) error {
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
if !strings.HasPrefix(args[i], "--") {
|
if !strings.HasPrefix(args[i], "--") {
|
||||||
args = args[:i]
|
args = args[:i]
|
||||||
|
Loading…
Reference in New Issue
Block a user