From 373859be83210867ec8b8a34469b85b8b785b842 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 7 Aug 2022 18:15:26 +0200 Subject: [PATCH] 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. --- cmd/syncthing/cli/main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/syncthing/cli/main.go b/cmd/syncthing/cli/main.go index b6599c893..8396ac302 100644 --- a/cmd/syncthing/cli/main.go +++ b/cmd/syncthing/cli/main.go @@ -38,7 +38,16 @@ func Run() error { // add flags there... c := preCli{} 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. err := cmdutil.SetConfigDataLocationsFromFlags(c.HomeDir, c.ConfDir, c.DataDir) if err != nil { @@ -107,8 +116,8 @@ func Run() error { } // Drop the `-` not to recurse into self. - args := make([]string, len(os.Args)-1) - copy(args, os.Args) + args := make([]string, len(cliArgs)-1) + copy(args, cliArgs) fmt.Println("Reading commands from stdin...", args) 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 { @@ -140,7 +149,10 @@ func parseFlags(c *preCli) error { if len(os.Args) <= 2 { 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++ { if !strings.HasPrefix(args[i], "--") { args = args[:i]