cmd/syncthing: Add cli as a subcommand (fixes #6566, fixes #4719) (#7364)

* cmd/syncthing: Add cli as a subcommand (fixes #6566, fixes #4719)

* Hijack help

* Add comment

* Revert go.mod/go.sum
This commit is contained in:
Audrius Butkevicius
2021-02-15 18:50:53 +01:00
committed by GitHub
parent b2c9e7b07b
commit fb078068b4
8 changed files with 107 additions and 88 deletions
+17 -21
View File
@@ -31,6 +31,9 @@ import (
"time"
"github.com/alecthomas/kong"
"github.com/thejerf/suture/v4"
"github.com/syncthing/syncthing/cmd/syncthing/cli"
"github.com/syncthing/syncthing/cmd/syncthing/decrypt"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/config"
@@ -48,7 +51,6 @@ import (
"github.com/syncthing/syncthing/lib/upgrade"
"github.com/pkg/errors"
"github.com/thejerf/suture/v4"
)
const (
@@ -127,11 +129,12 @@ var (
errTooEarlyUpgrade = fmt.Errorf("last upgrade happened less than %v ago, skipping", upgradeRetryInterval)
)
// The cli struct is the main entry point for the command line parser. The
// The entrypoint struct is the main entry point for the command line parser. The
// commands and options here are top level commands to syncthing.
var cli struct {
var entrypoint struct {
Serve serveOptions `cmd:"" help:"Run Syncthing"`
Decrypt decrypt.CLI `cmd:"" help:"Decrypt or verify an encrypted folder"`
Cli cli.CLI `cmd:"" help:"Command line interface for Syncthing"`
}
// serveOptions are the options for the `syncthing serve` command.
@@ -227,11 +230,11 @@ func main() {
args = append([]string{"serve"}, convertLegacyArgs(args)...)
}
cli.Serve.setDefaults()
entrypoint.Serve.setDefaults()
// Create a parser with an overridden help function to print our extra
// help info.
parser, err := kong.New(&cli, kong.Help(extraHelpPrinter))
parser, err := kong.New(&entrypoint, kong.Help(helpHandler))
if err != nil {
log.Fatal(err)
}
@@ -242,7 +245,11 @@ func main() {
parser.FatalIfErrorf(err)
}
func extraHelpPrinter(options kong.HelpOptions, ctx *kong.Context) error {
func helpHandler(options kong.HelpOptions, ctx *kong.Context) error {
// If we're looking for CLI help, pass the arguments down to the CLI library to print it's own help.
if ctx.Command() == "cli" {
return ctx.Run()
}
if err := kong.DefaultHelpPrinter(options, ctx); err != nil {
return err
}
@@ -282,12 +289,12 @@ func (options serveOptions) Run() error {
case homeSet && dataSet:
err = errors.New("-home must not be used together with -conf and -data")
case homeSet:
if err = setLocation(locations.ConfigBaseDir, options.HomeDir); err == nil {
err = setLocation(locations.DataBaseDir, options.HomeDir)
if err = locations.SetBaseDir(locations.ConfigBaseDir, options.HomeDir); err == nil {
err = locations.SetBaseDir(locations.DataBaseDir, options.HomeDir)
}
case dataSet:
if err = setLocation(locations.ConfigBaseDir, options.ConfDir); err == nil {
err = setLocation(locations.DataBaseDir, options.DataDir)
if err = locations.SetBaseDir(locations.ConfigBaseDir, options.ConfDir); err == nil {
err = locations.SetBaseDir(locations.DataBaseDir, options.DataDir)
}
}
if err != nil {
@@ -1004,17 +1011,6 @@ func exitCodeForUpgrade(err error) int {
return svcutil.ExitError.AsInt()
}
func setLocation(enum locations.BaseDirEnum, loc string) error {
if !filepath.IsAbs(loc) {
var err error
loc, err = filepath.Abs(loc)
if err != nil {
return err
}
}
return locations.SetBaseDir(enum, loc)
}
// convertLegacyArgs returns the slice of arguments with single dash long
// flags converted to double dash long flags.
func convertLegacyArgs(args []string) []string {