diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index e4b1e3174..e553f1ace 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -188,6 +188,7 @@ var ( doUpgrade bool doUpgradeCheck bool noBrowser bool + noConsole bool generateDir string logFile string noRestart = os.Getenv("STNORESTART") != "" @@ -214,6 +215,9 @@ func main() { logFile = filepath.Join(defConfDir, "syncthing.log") flag.StringVar(&logFile, "logfile", logFile, "Log file name (blank for stdout)") + + // We also add an option to hide the console window + flag.BoolVar(&noConsole, "no-console", false, "Hide console window") } flag.StringVar(&generateDir, "generate", "", "Generate key and config in specified dir, then exit") @@ -232,6 +236,10 @@ func main() { flag.Usage = usageFor(flag.CommandLine, usage, fmt.Sprintf(extraUsage, defConfDir)) flag.Parse() + if noConsole { + osutil.HideConsole() + } + if confDir == "" { // Not set as default above because the string can be really long. confDir = defConfDir diff --git a/internal/osutil/hidden_unix.go b/internal/osutil/hidden_unix.go index b158f73d0..6c2a05ed9 100644 --- a/internal/osutil/hidden_unix.go +++ b/internal/osutil/hidden_unix.go @@ -24,3 +24,5 @@ func HideFile(path string) error { func ShowFile(path string) error { return nil } + +func HideConsole() {} diff --git a/internal/osutil/hidden_windows.go b/internal/osutil/hidden_windows.go index 02a58af0a..8df111460 100644 --- a/internal/osutil/hidden_windows.go +++ b/internal/osutil/hidden_windows.go @@ -48,3 +48,14 @@ func ShowFile(path string) error { attrs &^= syscall.FILE_ATTRIBUTE_HIDDEN return syscall.SetFileAttributes(p, attrs) } + +func HideConsole() { + getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow") + showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow") + if getConsoleWindow.Find() == nil && showWindow.Find() == nil { + hwnd, _, _ := getConsoleWindow.Call() + if hwnd != 0 { + showWindow.Call(hwnd, 0) + } + } +}