From 45a9162faf65e60fad1571c047b6d2f314bda736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= Date: Wed, 12 Apr 2023 11:31:23 -0300 Subject: [PATCH] Move log file to .wakatime folder --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- TROUBLESHOOTING.md | 2 +- USAGE.md | 2 +- cmd/logfile/logfile.go | 14 +++++++++++--- cmd/logfile/logfile_test.go | 14 +++++++------- cmd/root.go | 4 ++-- cmd/run.go | 9 +++++++++ pkg/ini/ini.go | 26 +++++++++++++++++++------- pkg/offline/offline.go | 2 +- 9 files changed, 52 insertions(+), 23 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index abdc80a8..e161acc3 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -19,5 +19,5 @@ Please fill out the template below to report a bug. **Logs**: diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index bac5adae..1aa3878a 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -4,7 +4,7 @@ First, read [How to debug WakaTime plugins][faq debug plugins]. Set `debug=true` in your `~/.wakatime.cfg` file to enable verbose logging. -The common wakatime-cli program logs to your user `$HOME` directory `~/.wakatime.log`. +The common wakatime-cli program logs to your user `$HOME` directory `~/.wakatime/wakatime.log`. Each plugin also has its own log file: diff --git a/USAGE.md b/USAGE.md index f4bea2f2..287b62cd 100644 --- a/USAGE.md +++ b/USAGE.md @@ -81,7 +81,7 @@ some/submodule/name = new project name | ssl_certs_file | Path to a CA certs file. By default, uses bundled Letsencrypt CA cert along with system ca certs. | _filepath_ | | | timeout | Connection timeout in seconds when communicating with the api. | _int_ | `120` | | hostname | Optional name of local machine. By default, auto-detects the local machine’s hostname. | _string_ | | -| log_file | Optional log file path. | _filepath_ | `~/.wakatime.log` | +| log_file | Optional log file path. | _filepath_ | `~/.wakatime/wakatime.log` | ### Project Map Section diff --git a/cmd/logfile/logfile.go b/cmd/logfile/logfile.go index bff35e5f..a049a5b2 100644 --- a/cmd/logfile/logfile.go +++ b/cmd/logfile/logfile.go @@ -11,7 +11,10 @@ import ( "github.com/spf13/viper" ) -const defaultFile = ".wakatime.log" +const ( + defaultFile = "wakatime.log" + defaultFolder = ".wakatime" +) // Params contains log file parameters. type Params struct { @@ -49,12 +52,17 @@ func LoadParams(v *viper.Viper) (Params, error) { return params, nil } - home, err := ini.WakaHomeDir() + home, hometype, err := ini.WakaHomeDir() if err != nil { return Params{}, fmt.Errorf("failed getting user's home directory: %s", err) } - params.File = filepath.Join(home, defaultFile) + switch hometype { + case ini.WakaHomeTypeEnvVar: + params.File = filepath.Join(home, defaultFile) + default: + params.File = filepath.Join(home, defaultFolder, defaultFile) + } return params, nil } diff --git a/cmd/logfile/logfile_test.go b/cmd/logfile/logfile_test.go index 50812532..12e2c8bf 100644 --- a/cmd/logfile/logfile_test.go +++ b/cmd/logfile/logfile_test.go @@ -13,14 +13,14 @@ import ( ) func TestLoadParams(t *testing.T) { - tmpFile, err := os.CreateTemp(t.TempDir(), "wakatime.log") + tmpFile, err := os.CreateTemp(t.TempDir(), "") require.NoError(t, err) defer tmpFile.Close() dir, _ := filepath.Split(tmpFile.Name()) - logFile, err := os.Create(filepath.Join(dir, ".wakatime.log")) + logFile, err := os.Create(filepath.Join(dir, "wakatime.log")) require.NoError(t, err) defer logFile.Close() @@ -41,14 +41,14 @@ func TestLoadParams(t *testing.T) { "log file and verbose set": { ViperDebug: true, Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), Verbose: true, }, }, "log file and verbose from config": { ViperDebugConfig: true, Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), Verbose: true, }, }, @@ -76,18 +76,18 @@ func TestLoadParams(t *testing.T) { "log file from WAKATIME_HOME": { EnvVar: dir, Expected: logfile.Params{ - File: filepath.Join(dir, ".wakatime.log"), + File: filepath.Join(dir, "wakatime.log"), }, }, "log file from home dir": { Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), }, }, "log to stdout": { ViperToStdout: true, Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), ToStdout: true, }, }, diff --git a/cmd/root.go b/cmd/root.go index 57d8eab8..c540a29a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -154,8 +154,8 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) { " remote file, this local file will be used for stats and just"+ " the value of --entity is sent with the heartbeat.", ) - flags.String("log-file", "", "Optional log file. Defaults to '~/.wakatime.log'.") - flags.String("logfile", "", "(deprecated) Optional log file. Defaults to '~/.wakatime.log'.") + flags.String("log-file", "", "Optional log file. Defaults to '~/.wakatime/wakatime.log'.") + flags.String("logfile", "", "(deprecated) Optional log file. Defaults to '~/.wakatime/wakatime.log'.") flags.Bool("log-to-stdout", false, "If enabled, logs will go to stdout. Will overwrite logfile configs.") flags.Bool( "no-ssl-verify", diff --git a/cmd/run.go b/cmd/run.go index 1d9cf774..0862a193 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "path/filepath" "runtime/debug" "strings" @@ -216,6 +217,14 @@ func SetupLogging(v *viper.Viper) (*logfile.Params, error) { logFile := os.Stdout if !logfileParams.ToStdout { + dir := filepath.Dir(logfileParams.File) + if _, err := os.Stat(dir); os.IsNotExist(err) { + err := os.MkdirAll(dir, 0750) + if err != nil { + return nil, fmt.Errorf("error creating log file directory: %s", err) + } + } + logFile, err = os.OpenFile(logfileParams.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { return nil, fmt.Errorf("error opening log file: %s", err) diff --git a/pkg/ini/ini.go b/pkg/ini/ini.go index e53d9aa4..851abb04 100644 --- a/pkg/ini/ini.go +++ b/pkg/ini/ini.go @@ -19,6 +19,18 @@ import ( "gopkg.in/ini.v1" ) +// WakaHomeType is WakaTime home type. +type WakaHomeType int + +const ( + // WakaHomeTypeUnknown is unknown WakaTime home type. + WakaHomeTypeUnknown WakaHomeType = iota + // WakaHomeTypeEnvVar is WakaTime home type from environment variable. + WakaHomeTypeEnvVar + // WakaHomeTypeOSDir is WakaTime home type from OS directory. + WakaHomeTypeOSDir +) + const ( // defaultFile is the name of the default wakatime config file. defaultFile = ".wakatime.cfg" @@ -137,7 +149,7 @@ func FilePath(v *viper.Viper) (string, error) { return p, nil } - home, err := WakaHomeDir() + home, _, err := WakaHomeDir() if err != nil { return "", fmt.Errorf("failed getting user's home directory: %s", err) } @@ -172,7 +184,7 @@ func InternalFilePath(v *viper.Viper) (string, error) { return p, nil } - home, err := WakaHomeDir() + home, _, err := WakaHomeDir() if err != nil { return "", fmt.Errorf("failed getting user's home directory: %s", err) } @@ -181,12 +193,12 @@ func InternalFilePath(v *viper.Viper) (string, error) { } // WakaHomeDir returns the current user's home directory. -func WakaHomeDir() (string, error) { +func WakaHomeDir() (string, WakaHomeType, error) { home, exists := os.LookupEnv("WAKATIME_HOME") if exists && home != "" { home, err := homedir.Expand(home) if err == nil { - return home, nil + return home, WakaHomeTypeEnvVar, nil } log.Warnf("failed to expand WAKATIME_HOME filepath: %s", err) @@ -198,7 +210,7 @@ func WakaHomeDir() (string, error) { } if home != "" { - return home, nil + return home, WakaHomeTypeOSDir, nil } u, err := user.LookupId(strconv.Itoa(os.Getuid())) @@ -207,10 +219,10 @@ func WakaHomeDir() (string, error) { } if u.HomeDir != "" { - return u.HomeDir, nil + return u.HomeDir, WakaHomeTypeOSDir, nil } - return "", fmt.Errorf("could not determine wakatime home dir") + return "", WakaHomeTypeUnknown, fmt.Errorf("could not determine wakatime home dir") } // mutexClock is used to implement mutex.Clock interface. diff --git a/pkg/offline/offline.go b/pkg/offline/offline.go index 855b7c60..cef87f0c 100644 --- a/pkg/offline/offline.go +++ b/pkg/offline/offline.go @@ -89,7 +89,7 @@ func WithQueue(filepath string) heartbeat.HandleOption { // the user's $HOME folder cannot be detected, it defaults to the // current directory. func QueueFilepath() (string, error) { - home, err := ini.WakaHomeDir() + home, _, err := ini.WakaHomeDir() if err != nil { return dbFilename, fmt.Errorf("failed getting user's home directory, defaulting to current directory: %s", err) }