Clean up flags vs envvars

This commit is contained in:
Jakob Borg 2014-10-06 17:55:54 +02:00
parent 9b11609b63
commit 214f18cbfd
3 changed files with 48 additions and 50 deletions

View File

@ -73,7 +73,6 @@ const (
) )
var l = logger.DefaultLogger var l = logger.DefaultLogger
var innerProcess = os.Getenv("STNORESTART") != ""
func init() { func init() {
if Version != "unknown-dev" { if Version != "unknown-dev" {
@ -110,7 +109,13 @@ var (
const ( const (
usage = "syncthing [options]" usage = "syncthing [options]"
extraUsage = `The value for the -logflags option is a sum of the following: extraUsage = `
The default configuration directory is:
%s
The -logflags value is a sum of the following:
1 Date 1 Date
2 Time 2 Time
@ -122,23 +127,18 @@ I.e. to prefix each log line with date and time, set -logflags=3 (1 + 2 from
above). The value 0 is used to disable all of the above. The default is to above). The value 0 is used to disable all of the above. The default is to
show time only (2). show time only (2).
The following enviroment variables are interpreted by syncthing:
STGUIADDRESS Override GUI listen address set in config. Expects protocol type Development Settings
followed by hostname or an IP address, followed by a port, such --------------------
as "https://127.0.0.1:8888".
STGUIAUTH Override GUI authentication credentials set in config. Expects The following environment variables modify syncthing's behavior in ways that
a colon separated username and password, such as "admin:secret". are mostly useful for developers. Use with care.
STGUIAPIKEY Override GUI API key set in config. STGUIASSETS Directory to load GUI assets from. Overrides compiled in assets.
STNORESTART Do not attempt to restart when requested to, instead just exit.
Set this variable when running under a service manager such as
runit, launchd, etc.
STTRACE A comma separated string of facilities to trace. The valid STTRACE A comma separated string of facilities to trace. The valid
facility strings: facility strings are:
- "beacon" (the beacon package) - "beacon" (the beacon package)
- "discover" (the discover package) - "discover" (the discover package)
- "events" (the events package) - "events" (the events package)
@ -151,8 +151,6 @@ The following enviroment variables are interpreted by syncthing:
- "xdr" (the xdr package) - "xdr" (the xdr package)
- "all" (all of the above) - "all" (all of the above)
STGUIASSETS Directory to load GUI assets from. Overrides compiled in assets.
STPROFILER Set to a listen address such as "127.0.0.1:9090" to start the STPROFILER Set to a listen address such as "127.0.0.1:9090" to start the
profiler with HTTP access. profiler with HTTP access.
@ -172,7 +170,7 @@ func init() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }
// Command line options // Command line and environment options
var ( var (
reset bool reset bool
showVersion bool showVersion bool
@ -180,9 +178,15 @@ var (
doUpgradeCheck bool doUpgradeCheck bool
noBrowser bool noBrowser bool
generateDir string generateDir string
guiAddress string noRestart = os.Getenv("STNORESTART") != ""
guiAuthentication string guiAddress = os.Getenv("STGUIADDRESS") // legacy
guiAPIKey string guiAuthentication = os.Getenv("STGUIAUTH") // legacy
guiAPIKey = os.Getenv("STGUIAPIKEY") // legacy
profiler = os.Getenv("STPROFILER")
guiAssets = os.Getenv("STGUIASSETS")
cpuProfile = os.Getenv("STCPUPROFILE") != ""
stRestarting = os.Getenv("STRESTART") != ""
innerProcess = os.Getenv("STNORESTART") != "" || os.Getenv("STMONITORED") != ""
) )
func main() { func main() {
@ -190,20 +194,27 @@ func main() {
if err != nil { if err != nil {
l.Fatalln("home:", err) l.Fatalln("home:", err)
} }
flag.StringVar(&confDir, "home", defConfDir, "Set configuration directory") flag.StringVar(&generateDir, "generate", "", "Generate key in specified dir, then exit")
flag.StringVar(&guiAddress, "gui-address", guiAddress, "Override GUI address")
flag.StringVar(&guiAuthentication, "gui-authentication", guiAuthentication, "Override GUI authentication; username:password")
flag.StringVar(&guiAPIKey, "gui-apikey", guiAPIKey, "Override GUI API key")
flag.StringVar(&confDir, "home", "", "Set configuration directory")
flag.IntVar(&logFlags, "logflags", logFlags, "Select information in log line prefix")
flag.BoolVar(&noBrowser, "no-browser", false, "Do not start browser")
flag.BoolVar(&noRestart, "no-restart", noRestart, "Do not restart; just exit")
flag.BoolVar(&reset, "reset", false, "Prepare to resync from cluster") flag.BoolVar(&reset, "reset", false, "Prepare to resync from cluster")
flag.BoolVar(&showVersion, "version", false, "Show version")
flag.BoolVar(&doUpgrade, "upgrade", false, "Perform upgrade") flag.BoolVar(&doUpgrade, "upgrade", false, "Perform upgrade")
flag.BoolVar(&doUpgradeCheck, "upgrade-check", false, "Check for available upgrade") flag.BoolVar(&doUpgradeCheck, "upgrade-check", false, "Check for available upgrade")
flag.BoolVar(&noBrowser, "no-browser", false, "Do not start browser") flag.BoolVar(&showVersion, "version", false, "Show version")
flag.StringVar(&generateDir, "generate", "", "Generate key in specified dir")
flag.StringVar(&guiAddress, "gui-address", "", "Override GUI address") flag.Usage = usageFor(flag.CommandLine, usage, fmt.Sprintf(extraUsage, defConfDir))
flag.StringVar(&guiAuthentication, "gui-authentication", "", "Override GUI authentication. Expects 'username:password'")
flag.StringVar(&guiAPIKey, "gui-apikey", "", "Override GUI API key")
flag.IntVar(&logFlags, "logflags", logFlags, "Set log flags")
flag.Usage = usageFor(flag.CommandLine, usage, extraUsage)
flag.Parse() flag.Parse()
if confDir == "" {
// Not set as default above because the string can be really long.
confDir = defConfDir
}
if showVersion { if showVersion {
fmt.Println(LongVersion) fmt.Println(LongVersion)
return return
@ -291,7 +302,7 @@ func main() {
return return
} }
if os.Getenv("STNORESTART") != "" { if noRestart {
syncthingMain() syncthingMain()
} else { } else {
monitorMain() monitorMain()
@ -386,7 +397,7 @@ func syncthingMain() {
l.Infof("Edit %s to taste or use the GUI\n", cfgFile) l.Infof("Edit %s to taste or use the GUI\n", cfgFile)
} }
if profiler := os.Getenv("STPROFILER"); len(profiler) > 0 { if len(profiler) > 0 {
go func() { go func() {
l.Debugln("Starting profiler on", profiler) l.Debugln("Starting profiler on", profiler)
runtime.SetBlockProfileRate(1) runtime.SetBlockProfileRate(1)
@ -507,11 +518,11 @@ nextFolder:
urlShow := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostShow, strconv.Itoa(addr.Port))) urlShow := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostShow, strconv.Itoa(addr.Port)))
l.Infoln("Starting web GUI on", urlShow) l.Infoln("Starting web GUI on", urlShow)
err := startGUI(guiCfg, os.Getenv("STGUIASSETS"), m) err := startGUI(guiCfg, guiAssets, m)
if err != nil { if err != nil {
l.Fatalln("Cannot start GUI:", err) l.Fatalln("Cannot start GUI:", err)
} }
if !noBrowser && opts.StartBrowser && len(os.Getenv("STRESTART")) == 0 { if opts.StartBrowser && !noBrowser && !stRestarting {
urlOpen := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostOpen, strconv.Itoa(addr.Port))) urlOpen := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostOpen, strconv.Itoa(addr.Port)))
openURL(urlOpen) openURL(urlOpen)
} }
@ -593,7 +604,7 @@ nextFolder:
} }
} }
if cpuprof := os.Getenv("STCPUPROFILE"); len(cpuprof) > 0 { if cpuProfile {
f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid())) f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid()))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -1086,10 +1097,6 @@ func getFreePort(host string, ports ...int) (int, error) {
} }
func overrideGUIConfig(cfg config.GUIConfiguration, address, authentication, apikey string) config.GUIConfiguration { func overrideGUIConfig(cfg config.GUIConfiguration, address, authentication, apikey string) config.GUIConfiguration {
if address == "" {
address = os.Getenv("STGUIADDRESS")
}
if address != "" { if address != "" {
cfg.Enabled = true cfg.Enabled = true
@ -1101,7 +1108,7 @@ func overrideGUIConfig(cfg config.GUIConfiguration, address, authentication, api
if err != nil { if err != nil {
l.Fatalln(err) l.Fatalln(err)
} }
l.Debugf("%#v", parsed) cfg.Address = parsed.Host
switch parsed.Scheme { switch parsed.Scheme {
case "http": case "http":
cfg.UseTLS = false cfg.UseTLS = false
@ -1110,14 +1117,9 @@ func overrideGUIConfig(cfg config.GUIConfiguration, address, authentication, api
default: default:
l.Fatalln("Unknown scheme:", parsed.Scheme) l.Fatalln("Unknown scheme:", parsed.Scheme)
} }
cfg.Address = parsed.Host
} }
} }
if authentication == "" {
authentication = os.Getenv("STGUIAUTH")
}
if authentication != "" { if authentication != "" {
authenticationParts := strings.SplitN(authentication, ":", 2) authenticationParts := strings.SplitN(authentication, ":", 2)
@ -1130,10 +1132,6 @@ func overrideGUIConfig(cfg config.GUIConfiguration, address, authentication, api
cfg.Password = string(hash) cfg.Password = string(hash)
} }
if apikey == "" {
apikey = os.Getenv("STGUIAPIKEY")
}
if apikey != "" { if apikey != "" {
cfg.APIKey = apikey cfg.APIKey = apikey
} }

View File

@ -41,6 +41,7 @@ const (
func monitorMain() { func monitorMain() {
os.Setenv("STNORESTART", "yes") os.Setenv("STNORESTART", "yes")
os.Setenv("STMONITORED", "yes")
l.SetPrefix("[monitor] ") l.SetPrefix("[monitor] ")
args := os.Args args := os.Args

View File

@ -47,9 +47,8 @@ func usageFor(fs *flag.FlagSet, usage string, extra string) func() {
var opt = " -" + f.Name var opt = " -" + f.Name
if f.DefValue != "false" { if f.DefValue != "false" {
opt += "=" + fmt.Sprintf("%q", f.DefValue) opt += "=" + fmt.Sprintf(`"%s"`, f.DefValue)
} }
options = append(options, []string{opt, f.Usage}) options = append(options, []string{opt, f.Usage})
}) })