2019-02-12 07:58:24 +01:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-12-17 12:28:59 +01:00
|
|
|
|
2021-02-15 18:50:53 +01:00
|
|
|
package cli
|
2016-12-17 01:25:45 +01:00
|
|
|
|
|
|
|
import (
|
2019-02-12 07:58:24 +01:00
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2016-12-17 01:25:45 +01:00
|
|
|
|
2019-02-12 07:58:24 +01:00
|
|
|
"github.com/AudriusButkevicius/recli"
|
|
|
|
"github.com/flynn-archive/go-shlex"
|
|
|
|
"github.com/mattn/go-isatty"
|
|
|
|
"github.com/pkg/errors"
|
2021-02-15 18:50:53 +01:00
|
|
|
|
2019-02-12 07:58:24 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2019-08-15 16:29:37 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2019-02-12 07:58:24 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/locations"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2021-02-15 18:50:53 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/svcutil"
|
|
|
|
|
2019-02-12 07:58:24 +01:00
|
|
|
"github.com/urfave/cli"
|
2016-12-17 01:25:45 +01:00
|
|
|
)
|
|
|
|
|
2021-02-15 18:50:53 +01:00
|
|
|
type CLI struct {
|
|
|
|
GUIAddress string `name:"gui-address" placeholder:"URL" help:"Override GUI address (e.g. \"http://192.0.2.42:8443\")"`
|
|
|
|
GUIAPIKey string `name:"gui-apikey" placeholder:"API-KEY" help:"Override GUI API key"`
|
|
|
|
HomeDir string `name:"home" placeholder:"PATH" help:"Set configuration and data directory"`
|
|
|
|
ConfDir string `name:"conf" placeholder:"PATH" help:"Set configuration directory (config and keys)"`
|
|
|
|
Args []string `arg:"" optional:""`
|
|
|
|
}
|
2019-02-12 07:58:24 +01:00
|
|
|
|
2021-02-15 18:50:53 +01:00
|
|
|
func (c *CLI) Run() error {
|
|
|
|
// Not set as default above because the strings can be really long.
|
|
|
|
var err error
|
|
|
|
homeSet := c.HomeDir != ""
|
|
|
|
confSet := c.ConfDir != ""
|
|
|
|
switch {
|
|
|
|
case homeSet && confSet:
|
|
|
|
err = errors.New("-home must not be used together with -conf")
|
|
|
|
case homeSet:
|
|
|
|
err = locations.SetBaseDir(locations.ConfigBaseDir, c.HomeDir)
|
|
|
|
case confSet:
|
|
|
|
err = locations.SetBaseDir(locations.ConfigBaseDir, c.ConfDir)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Command line options:", err)
|
|
|
|
os.Exit(svcutil.ExitError.AsInt())
|
|
|
|
}
|
|
|
|
guiCfg := config.GUIConfiguration{
|
|
|
|
RawAddress: c.GUIAddress,
|
|
|
|
APIKey: c.GUIAPIKey,
|
|
|
|
}
|
2019-02-12 07:58:24 +01:00
|
|
|
|
|
|
|
// Now if the API key and address is not provided (we are not connecting to a remote instance),
|
|
|
|
// try to rip it out of the config.
|
|
|
|
if guiCfg.RawAddress == "" && guiCfg.APIKey == "" {
|
|
|
|
// Load the certs and get the ID
|
|
|
|
cert, err := tls.LoadX509KeyPair(
|
|
|
|
locations.Get(locations.CertFile),
|
|
|
|
locations.Get(locations.KeyFile),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.Wrap(err, "reading device ID")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
myID := protocol.NewDeviceID(cert.Certificate[0])
|
|
|
|
|
|
|
|
// Load the config
|
2020-08-25 08:11:14 +02:00
|
|
|
cfg, _, err := config.Load(locations.Get(locations.ConfigFile), myID, events.NoopLogger)
|
2019-02-12 07:58:24 +01:00
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.Wrap(err, "loading config")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
guiCfg = cfg.GUI()
|
|
|
|
} else if guiCfg.Address() == "" || guiCfg.APIKey == "" {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.New("Both --gui-address and --gui-apikey should be specified")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if guiCfg.Address() == "" {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.New("Could not find GUI Address")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if guiCfg.APIKey == "" {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.New("Could not find GUI API key")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client := getClient(guiCfg)
|
|
|
|
|
|
|
|
cfg, err := getConfig(client)
|
|
|
|
original := cfg.Copy()
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.Wrap(err, "getting config")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the config and set the default flags
|
|
|
|
recliCfg := recli.DefaultConfig
|
|
|
|
recliCfg.IDTag.Name = "xml"
|
|
|
|
recliCfg.SkipTag.Name = "json"
|
|
|
|
|
|
|
|
commands, err := recli.New(recliCfg).Construct(&cfg)
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.Wrap(err, "config reflect")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the same flags at the upper CLI, but do nothing with them.
|
|
|
|
// This is so that the usage text is the same
|
|
|
|
fakeFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "gui-address",
|
|
|
|
Value: "URL",
|
|
|
|
Usage: "Override GUI address (e.g. \"http://192.0.2.42:8443\")",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "gui-apikey",
|
|
|
|
Value: "API-KEY",
|
|
|
|
Usage: "Override GUI API key",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "home",
|
|
|
|
Value: "PATH",
|
|
|
|
Usage: "Set configuration and data directory",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "conf",
|
|
|
|
Value: "PATH",
|
|
|
|
Usage: "Set configuration directory (config and keys)",
|
|
|
|
},
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the actual CLI
|
|
|
|
app := cli.NewApp()
|
2021-02-15 18:50:53 +01:00
|
|
|
app.Name = "syncthing cli"
|
2019-02-12 07:58:24 +01:00
|
|
|
app.HelpName = app.Name
|
|
|
|
app.Author = "The Syncthing Authors"
|
|
|
|
app.Usage = "Syncthing command line interface"
|
|
|
|
app.Flags = fakeFlags
|
|
|
|
app.Metadata = map[string]interface{}{
|
|
|
|
"client": client,
|
|
|
|
}
|
|
|
|
app.Commands = []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
HideHelp: true,
|
|
|
|
Usage: "Configuration modification command group",
|
|
|
|
Subcommands: commands,
|
2016-12-17 01:25:45 +01:00
|
|
|
},
|
2019-02-12 07:58:24 +01:00
|
|
|
showCommand,
|
|
|
|
operationCommand,
|
|
|
|
errorsCommand,
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:50:53 +01:00
|
|
|
// It expects to be give os.Args which has argv[0] set to executable name, so fake it.
|
|
|
|
c.Args = append([]string{"cli"}, c.Args...)
|
|
|
|
|
2019-02-12 07:58:24 +01:00
|
|
|
tty := isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
|
|
|
|
if !tty {
|
|
|
|
// Not a TTY, consume from stdin
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
input, err := shlex.Split(scanner.Text())
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.Wrap(err, "parsing input")
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
if len(input) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-15 18:50:53 +01:00
|
|
|
err = app.Run(append(c.Args, input...))
|
2019-02-12 07:58:24 +01:00
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return err
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = scanner.Err()
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return err
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-15 18:50:53 +01:00
|
|
|
err = app.Run(c.Args)
|
2019-02-12 07:58:24 +01:00
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return err
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
2016-12-17 01:25:45 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:58:24 +01:00
|
|
|
if !reflect.DeepEqual(cfg, original) {
|
|
|
|
body, err := json.MarshalIndent(cfg, "", " ")
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return err
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
resp, err := client.Post("system/config", string(body))
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return err
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
body, err := responseToBArray(resp)
|
|
|
|
if err != nil {
|
2021-02-15 18:50:53 +01:00
|
|
|
return err
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
2021-02-15 18:50:53 +01:00
|
|
|
return errors.New(string(body))
|
2019-02-12 07:58:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 18:50:53 +01:00
|
|
|
return nil
|
2016-12-17 01:25:45 +01:00
|
|
|
}
|