all: some improvements for better troubleshooting

- Include version/OS information when logging
 - Make time field human readable in log file
 - Force root privilege when running status command on darwin

Updates #34
This commit is contained in:
Cuong Manh Le
2023-03-29 12:36:19 +07:00
committed by Cuong Manh Le
parent 9927803497
commit b3a342bc44
4 changed files with 39 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/cuonglm/osinfo"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/kardianos/service" "github.com/kardianos/service"
"github.com/miekg/dns" "github.com/miekg/dns"
@@ -36,6 +37,11 @@ import (
const selfCheckFQDN = "verify.controld.com" const selfCheckFQDN = "verify.controld.com"
var (
version = "dev"
commit = "none"
)
var ( var (
v = viper.NewWithOptions(viper.KeyDelimiter("::")) v = viper.NewWithOptions(viper.KeyDelimiter("::"))
defaultConfigWritten = false defaultConfigWritten = false
@@ -62,17 +68,28 @@ _/ ___\ __\_ __ \ | / __ |
\/ dns forwarding proxy \/ \/ dns forwarding proxy \/
` `
var rootCmd = &cobra.Command{
Use: "ctrld",
Short: strings.TrimLeft(rootShortDesc, "\n"),
Version: curVersion(),
}
func curVersion() string {
if version != "dev" {
version = "v" + version
}
if len(commit) > 7 {
commit = commit[:7]
}
return fmt.Sprintf("%s-%s", version, commit)
}
func initCLI() { func initCLI() {
// Enable opening via explorer.exe on Windows. // Enable opening via explorer.exe on Windows.
// See: https://github.com/spf13/cobra/issues/844. // See: https://github.com/spf13/cobra/issues/844.
cobra.MousetrapHelpText = "" cobra.MousetrapHelpText = ""
cobra.EnableCommandSorting = false cobra.EnableCommandSorting = false
rootCmd := &cobra.Command{
Use: "ctrld",
Short: strings.TrimLeft(rootShortDesc, "\n"),
Version: "1.1.3",
}
rootCmd.PersistentFlags().CountVarP( rootCmd.PersistentFlags().CountVarP(
&verbose, &verbose,
"verbose", "verbose",
@@ -142,7 +159,12 @@ func initCLI() {
if err := v.Unmarshal(&cfg); err != nil { if err := v.Unmarshal(&cfg); err != nil {
log.Fatalf("failed to unmarshal config: %v", err) log.Fatalf("failed to unmarshal config: %v", err)
} }
fmt.Println("starting ctrld...")
log.Println("starting ctrld ...")
log.Printf("version: %s\n", curVersion())
oi := osinfo.New()
log.Printf("os: %s\n", oi.String())
// Wait for network up. // Wait for network up.
if !ctrldnet.Up() { if !ctrldnet.Up() {
log.Fatal("network is not up yet") log.Fatal("network is not up yet")
@@ -362,6 +384,10 @@ func initCLI() {
} }
}, },
} }
if runtime.GOOS == "darwin" {
// On darwin, running status command without privileges may return wrong information.
statusCmd.PreRun = checkHasElevatedPrivilege
}
uninstallCmd := &cobra.Command{ uninstallCmd := &cobra.Command{
PreRun: checkHasElevatedPrivilege, PreRun: checkHasElevatedPrivilege,
@@ -522,7 +548,7 @@ func readConfigFile(writeDefaultConfig bool) bool {
// If err == nil, there's a config supplied via `--config`, no default config written. // If err == nil, there's a config supplied via `--config`, no default config written.
err := v.ReadInConfig() err := v.ReadInConfig()
if err == nil { if err == nil {
fmt.Println("loading config file from:", v.ConfigFileUsed()) log.Println("loading config file from:", v.ConfigFileUsed())
defaultConfigFile = v.ConfigFileUsed() defaultConfigFile = v.ConfigFileUsed()
return true return true
} }
@@ -536,7 +562,7 @@ func readConfigFile(writeDefaultConfig bool) bool {
if err := writeConfigFile(); err != nil { if err := writeConfigFile(); err != nil {
log.Fatalf("failed to write default config file: %v", err) log.Fatalf("failed to write default config file: %v", err)
} else { } else {
fmt.Println("writing default config file to: " + defaultConfigFile) log.Println("writing default config file to: " + defaultConfigFile)
} }
defaultConfigWritten = true defaultConfigWritten = true
return false return false

View File

@@ -73,7 +73,6 @@ func initLogging() {
} }
writers = append(writers, logFile) writers = append(writers, logFile)
} }
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
consoleWriter := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) { consoleWriter := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
w.TimeFormat = time.StampMilli w.TimeFormat = time.StampMilli
}) })

1
go.mod
View File

@@ -4,6 +4,7 @@ go 1.19
require ( require (
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534 github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534
github.com/cuonglm/osinfo v0.0.0-20230329055532-c513f836da19
github.com/frankban/quicktest v1.14.3 github.com/frankban/quicktest v1.14.3
github.com/fsnotify/fsnotify v1.6.0 github.com/fsnotify/fsnotify v1.6.0
github.com/go-playground/validator/v10 v10.11.1 github.com/go-playground/validator/v10 v10.11.1

4
go.sum
View File

@@ -54,6 +54,10 @@ github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534 h1:rtAn27
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cuonglm/osinfo v0.0.0-20230329052356-117e0ee9d353 h1:PFKlvMrKAUendoPEiJxSMkYeGG/G/5k7vu2ldGBnq3I=
github.com/cuonglm/osinfo v0.0.0-20230329052356-117e0ee9d353/go.mod h1:G45410zMgmnSjLVKCq4f6GpbYAzoP2plX9rPwgx6C24=
github.com/cuonglm/osinfo v0.0.0-20230329055532-c513f836da19 h1:7P/f19Mr0oa3ug8BYt4JuRe/Zq3dF4Mrr4m8+Kw+Hcs=
github.com/cuonglm/osinfo v0.0.0-20230329055532-c513f836da19/go.mod h1:G45410zMgmnSjLVKCq4f6GpbYAzoP2plX9rPwgx6C24=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=