mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-02 22:50:18 +00:00
We had some unholy mix of our own logger and the stdlib logger, probably because for historical reasons we wanted the device ID to stdout and the rest to stderr? But that's not the case any more, and the mix of formats is weird. Ideally I think the generate command should be silent and just print the device ID and nothing else, but that's tricky to accomplish since we have other methods do logging on their own. Hence this just harmonizes it so that we at least use the same logger with the same format and target...
This commit is contained in:
parent
2a8362d7af
commit
24275b4584
@ -13,7 +13,6 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/syncthing/syncthing/cmd/syncthing/cmdutil"
|
"github.com/syncthing/syncthing/cmd/syncthing/cmdutil"
|
||||||
@ -21,6 +20,7 @@ import (
|
|||||||
"github.com/syncthing/syncthing/lib/events"
|
"github.com/syncthing/syncthing/lib/events"
|
||||||
"github.com/syncthing/syncthing/lib/fs"
|
"github.com/syncthing/syncthing/lib/fs"
|
||||||
"github.com/syncthing/syncthing/lib/locations"
|
"github.com/syncthing/syncthing/lib/locations"
|
||||||
|
"github.com/syncthing/syncthing/lib/logger"
|
||||||
"github.com/syncthing/syncthing/lib/osutil"
|
"github.com/syncthing/syncthing/lib/osutil"
|
||||||
"github.com/syncthing/syncthing/lib/protocol"
|
"github.com/syncthing/syncthing/lib/protocol"
|
||||||
"github.com/syncthing/syncthing/lib/syncthing"
|
"github.com/syncthing/syncthing/lib/syncthing"
|
||||||
@ -32,9 +32,7 @@ type CLI struct {
|
|||||||
GUIPassword string `placeholder:"STRING" help:"Specify new GUI authentication password (use - to read from standard input)"`
|
GUIPassword string `placeholder:"STRING" help:"Specify new GUI authentication password (use - to read from standard input)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CLI) Run() error {
|
func (c *CLI) Run(l logger.Logger) error {
|
||||||
log.SetFlags(0)
|
|
||||||
|
|
||||||
if c.HideConsole {
|
if c.HideConsole {
|
||||||
osutil.HideConsole()
|
osutil.HideConsole()
|
||||||
}
|
}
|
||||||
@ -59,13 +57,13 @@ func (c *CLI) Run() error {
|
|||||||
c.GUIPassword = string(password)
|
c.GUIPassword = string(password)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Generate(c.ConfDir, c.GUIUser, c.GUIPassword, c.NoDefaultFolder, c.SkipPortProbing); err != nil {
|
if err := Generate(l, c.ConfDir, c.GUIUser, c.GUIPassword, c.NoDefaultFolder, c.SkipPortProbing); err != nil {
|
||||||
return fmt.Errorf("failed to generate config and keys: %w", err)
|
return fmt.Errorf("failed to generate config and keys: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Generate(confDir, guiUser, guiPassword string, noDefaultFolder, skipPortProbing bool) error {
|
func Generate(l logger.Logger, confDir, guiUser, guiPassword string, noDefaultFolder, skipPortProbing bool) error {
|
||||||
dir, err := fs.ExpandTilde(confDir)
|
dir, err := fs.ExpandTilde(confDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -80,7 +78,7 @@ func Generate(confDir, guiUser, guiPassword string, noDefaultFolder, skipPortPro
|
|||||||
certFile, keyFile := locations.Get(locations.CertFile), locations.Get(locations.KeyFile)
|
certFile, keyFile := locations.Get(locations.CertFile), locations.Get(locations.KeyFile)
|
||||||
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Println("WARNING: Key exists; will not overwrite.")
|
l.Warnln("Key exists; will not overwrite.")
|
||||||
} else {
|
} else {
|
||||||
cert, err = syncthing.GenerateCertificate(certFile, keyFile)
|
cert, err = syncthing.GenerateCertificate(certFile, keyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -88,7 +86,7 @@ func Generate(confDir, guiUser, guiPassword string, noDefaultFolder, skipPortPro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
myID = protocol.NewDeviceID(cert.Certificate[0])
|
myID = protocol.NewDeviceID(cert.Certificate[0])
|
||||||
log.Println("Device ID:", myID)
|
l.Infoln("Device ID:", myID)
|
||||||
|
|
||||||
cfgFile := locations.Get(locations.ConfigFile)
|
cfgFile := locations.Get(locations.ConfigFile)
|
||||||
cfg, _, err := config.Load(cfgFile, myID, events.NoopLogger)
|
cfg, _, err := config.Load(cfgFile, myID, events.NoopLogger)
|
||||||
@ -106,7 +104,7 @@ func Generate(confDir, guiUser, guiPassword string, noDefaultFolder, skipPortPro
|
|||||||
|
|
||||||
var updateErr error
|
var updateErr error
|
||||||
waiter, err := cfg.Modify(func(cfg *config.Configuration) {
|
waiter, err := cfg.Modify(func(cfg *config.Configuration) {
|
||||||
updateErr = updateGUIAuthentication(&cfg.GUI, guiUser, guiPassword)
|
updateErr = updateGUIAuthentication(l, &cfg.GUI, guiUser, guiPassword)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("modify config: %w", err)
|
return fmt.Errorf("modify config: %w", err)
|
||||||
@ -122,17 +120,17 @@ func Generate(confDir, guiUser, guiPassword string, noDefaultFolder, skipPortPro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateGUIAuthentication(guiCfg *config.GUIConfiguration, guiUser, guiPassword string) error {
|
func updateGUIAuthentication(l logger.Logger, guiCfg *config.GUIConfiguration, guiUser, guiPassword string) error {
|
||||||
if guiUser != "" && guiCfg.User != guiUser {
|
if guiUser != "" && guiCfg.User != guiUser {
|
||||||
guiCfg.User = guiUser
|
guiCfg.User = guiUser
|
||||||
log.Println("Updated GUI authentication user name:", guiUser)
|
l.Infoln("Updated GUI authentication user name:", guiUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
if guiPassword != "" && guiCfg.Password != guiPassword {
|
if guiPassword != "" && guiCfg.Password != guiPassword {
|
||||||
if err := guiCfg.HashAndSetPassword(guiPassword); err != nil {
|
if err := guiCfg.HashAndSetPassword(guiPassword); err != nil {
|
||||||
return fmt.Errorf("failed to set GUI authentication password: %w", err)
|
return fmt.Errorf("failed to set GUI authentication password: %w", err)
|
||||||
}
|
}
|
||||||
log.Println("Updated GUI authentication password.")
|
l.Infoln("Updated GUI authentication password.")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -251,6 +251,7 @@ func main() {
|
|||||||
|
|
||||||
ctx, err := parser.Parse(args)
|
ctx, err := parser.Parse(args)
|
||||||
parser.FatalIfErrorf(err)
|
parser.FatalIfErrorf(err)
|
||||||
|
ctx.BindTo(l, (*logger.Logger)(nil)) // main logger available to subcommands
|
||||||
err = ctx.Run()
|
err = ctx.Run()
|
||||||
parser.FatalIfErrorf(err)
|
parser.FatalIfErrorf(err)
|
||||||
}
|
}
|
||||||
@ -345,7 +346,7 @@ func (options serveOptions) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if options.GenerateDir != "" {
|
if options.GenerateDir != "" {
|
||||||
if err := generate.Generate(options.GenerateDir, "", "", options.NoDefaultFolder, options.SkipPortProbing); err != nil {
|
if err := generate.Generate(l, options.GenerateDir, "", "", options.NoDefaultFolder, options.SkipPortProbing); err != nil {
|
||||||
l.Warnln("Failed to generate config and keys:", err)
|
l.Warnln("Failed to generate config and keys:", err)
|
||||||
os.Exit(svcutil.ExitError.AsInt())
|
os.Exit(svcutil.ExitError.AsInt())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user