Skip to content

Commit 4e5942e

Browse files
committed
Prevent many random project names on project file write error
1 parent 75ed1c3 commit 4e5942e

File tree

15 files changed

+219
-87
lines changed

15 files changed

+219
-87
lines changed

.github/workflows/on_push.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
name: Checkout
2222
uses: actions/checkout@v4
2323
-
24-
uses: actions/setup-go@v5
24+
uses: actions/setup-go@v6
2525
with:
2626
go-version-file: ${{ env.GO_VERSION_FILE }}
2727
check-latest: ${{ env.CHECK_LATEST }}
@@ -56,7 +56,7 @@ jobs:
5656
name: Checkout
5757
uses: actions/checkout@v4
5858
-
59-
uses: actions/setup-go@v5
59+
uses: actions/setup-go@v6
6060
with:
6161
go-version-file: ${{ env.GO_VERSION_FILE }}
6262
check-latest: ${{ env.CHECK_LATEST }}
@@ -81,7 +81,7 @@ jobs:
8181
name: Checkout
8282
uses: actions/checkout@v4
8383
-
84-
uses: actions/setup-go@v5
84+
uses: actions/setup-go@v6
8585
with:
8686
go-version-file: ${{ env.GO_VERSION_FILE }}
8787
check-latest: ${{ env.CHECK_LATEST }}
@@ -107,7 +107,7 @@ jobs:
107107
name: Checkout
108108
uses: actions/checkout@v4
109109
-
110-
uses: actions/setup-go@v5
110+
uses: actions/setup-go@v6
111111
with:
112112
go-version-file: ${{ env.GO_VERSION_FILE }}
113113
check-latest: ${{ env.CHECK_LATEST }}
@@ -132,7 +132,7 @@ jobs:
132132
name: Checkout
133133
uses: actions/checkout@v4
134134
-
135-
uses: actions/setup-go@v5
135+
uses: actions/setup-go@v6
136136
with:
137137
go-version-file: ${{ env.GO_VERSION_FILE }}
138138
check-latest: ${{ env.CHECK_LATEST }}
@@ -158,7 +158,7 @@ jobs:
158158
name: Checkout
159159
uses: actions/checkout@v4
160160
-
161-
uses: actions/setup-go@v5
161+
uses: actions/setup-go@v6
162162
with:
163163
go-version-file: ${{ env.GO_VERSION_FILE }}
164164
check-latest: ${{ env.CHECK_LATEST }}
@@ -207,7 +207,7 @@ jobs:
207207
name: Checkout
208208
uses: actions/checkout@v4
209209
-
210-
uses: actions/setup-go@v5
210+
uses: actions/setup-go@v6
211211
with:
212212
go-version-file: ${{ env.GO_VERSION_FILE }}
213213
check-latest: ${{ env.CHECK_LATEST }}
@@ -273,7 +273,7 @@ jobs:
273273
uses: actions/checkout@v4
274274
-
275275
name: Setup go
276-
uses: actions/setup-go@v5
276+
uses: actions/setup-go@v6
277277
with:
278278
go-version-file: ${{ env.GO_VERSION_FILE }}
279279
check-latest: ${{ env.CHECK_LATEST }}
@@ -321,7 +321,7 @@ jobs:
321321
uses: actions/checkout@v4
322322
-
323323
name: Setup go
324-
uses: actions/setup-go@v5
324+
uses: actions/setup-go@v6
325325
with:
326326
go-version-file: ${{ env.GO_VERSION_FILE }}
327327
check-latest: ${{ env.CHECK_LATEST }}
@@ -362,7 +362,7 @@ jobs:
362362
uses: actions/checkout@v4
363363
-
364364
name: Setup go
365-
uses: actions/setup-go@v5
365+
uses: actions/setup-go@v6
366366
with:
367367
go-version-file: ${{ env.GO_VERSION_FILE }}
368368
check-latest: ${{ env.CHECK_LATEST }}
@@ -403,7 +403,7 @@ jobs:
403403
uses: actions/checkout@v4
404404
-
405405
name: Setup go
406-
uses: actions/setup-go@v5
406+
uses: actions/setup-go@v6
407407
with:
408408
go-version-file: ${{ env.GO_VERSION_FILE }}
409409
check-latest: ${{ env.CHECK_LATEST }}
@@ -444,7 +444,7 @@ jobs:
444444
uses: actions/checkout@v4
445445
-
446446
name: Setup go
447-
uses: actions/setup-go@v5
447+
uses: actions/setup-go@v6
448448
with:
449449
go-version-file: ${{ env.GO_VERSION_FILE }}
450450
check-latest: ${{ env.CHECK_LATEST }}
@@ -485,7 +485,7 @@ jobs:
485485
uses: actions/checkout@v4
486486
-
487487
name: Setup go
488-
uses: actions/setup-go@v5
488+
uses: actions/setup-go@v6
489489
with:
490490
go-version-file: ${{ env.GO_VERSION_FILE }}
491491
check-latest: ${{ env.CHECK_LATEST }}
@@ -526,7 +526,7 @@ jobs:
526526
uses: actions/checkout@v4
527527
-
528528
name: Setup go
529-
uses: actions/setup-go@v5
529+
uses: actions/setup-go@v6
530530
with:
531531
go-version-file: ${{ env.GO_VERSION_FILE }}
532532
check-latest: ${{ env.CHECK_LATEST }}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ lint: install-linter
156156
.PHONY: vulncheck
157157
vulncheck:
158158
go install golang.org/x/vuln/cmd/govulncheck@latest
159-
./bin/govulncheck-with-excludes.sh ./...
159+
govulncheck
160160

161161
.PHONY: test
162162
test:

cmd/fileexperts/fileexperts_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"testing"
1313
"time"
1414

15-
"github.com/wakatime/wakatime-cli/cmd"
1615
"github.com/wakatime/wakatime-cli/cmd/fileexperts"
1716
"github.com/wakatime/wakatime-cli/pkg/api"
1817
"github.com/wakatime/wakatime-cli/pkg/log"
18+
"github.com/wakatime/wakatime-cli/pkg/log/setup"
1919
"github.com/wakatime/wakatime-cli/pkg/project"
2020

2121
"github.com/spf13/viper"
@@ -111,7 +111,7 @@ func TestFileExperts_NonExistingEntity(t *testing.T) {
111111
v.Set("log-file", logFile.Name())
112112
v.Set("verbose", true)
113113

114-
logger, err := cmd.SetupLogging(ctx, v)
114+
logger, err := setup.Logging(ctx, v)
115115
require.NoError(t, err)
116116

117117
defer logger.Flush()

cmd/heartbeat/heartbeat_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
"testing"
1515
"time"
1616

17-
"github.com/wakatime/wakatime-cli/cmd"
1817
cmdheartbeat "github.com/wakatime/wakatime-cli/cmd/heartbeat"
1918
"github.com/wakatime/wakatime-cli/pkg/api"
2019
"github.com/wakatime/wakatime-cli/pkg/file"
2120
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
2221
"github.com/wakatime/wakatime-cli/pkg/ini"
2322
"github.com/wakatime/wakatime-cli/pkg/log"
23+
"github.com/wakatime/wakatime-cli/pkg/log/setup"
2424
"github.com/wakatime/wakatime-cli/pkg/offline"
2525
"github.com/wakatime/wakatime-cli/pkg/params"
2626
"github.com/wakatime/wakatime-cli/pkg/project"
@@ -560,7 +560,7 @@ func TestSendHeartbeats_ExtraHeartbeatsNestedError(t *testing.T) {
560560
v.Set("log-file", logFile.Name())
561561
v.Set("verbose", true)
562562

563-
logger, err := cmd.SetupLogging(ctx, v)
563+
logger, err := setup.Logging(ctx, v)
564564
require.NoError(t, err)
565565

566566
defer logger.Flush()
@@ -748,7 +748,7 @@ func TestSendHeartbeats_NonExistingEntity(t *testing.T) {
748748
v.Set("log-file", logFile.Name())
749749
v.Set("verbose", true)
750750

751-
logger, err := cmd.SetupLogging(ctx, v)
751+
logger, err := setup.Logging(ctx, v)
752752
require.NoError(t, err)
753753

754754
defer logger.Flush()
@@ -883,7 +883,7 @@ func TestSendHeartbeats_ExtraHeartbeatsIsUnsavedEntity(t *testing.T) {
883883
v.Set("log-file", logFile.Name())
884884
v.Set("verbose", true)
885885

886-
logger, err := cmd.SetupLogging(ctx, v)
886+
logger, err := setup.Logging(ctx, v)
887887
require.NoError(t, err)
888888

889889
defer logger.Flush()
@@ -1009,7 +1009,7 @@ func TestSendHeartbeats_NonExistingExtraHeartbeatsEntity(t *testing.T) {
10091009
v.Set("log-file", logFile.Name())
10101010
v.Set("verbose", true)
10111011

1012-
logger, err := cmd.SetupLogging(ctx, v)
1012+
logger, err := setup.Logging(ctx, v)
10131013
require.NoError(t, err)
10141014

10151015
defer logger.Flush()
@@ -1100,7 +1100,7 @@ func TestSendHeartbeats_ErrBackoff(t *testing.T) {
11001100
v.Set("key", "00000000-0000-4000-8000-000000000000")
11011101
v.Set("log-file", logFile.Name())
11021102

1103-
logger, err := cmd.SetupLogging(ctx, v)
1103+
logger, err := setup.Logging(ctx, v)
11041104
require.NoError(t, err)
11051105

11061106
defer logger.Flush()
@@ -1162,7 +1162,7 @@ func TestSendHeartbeats_ErrBackoff_Verbose(t *testing.T) {
11621162
v.Set("log-file", logFile.Name())
11631163
v.Set("verbose", true)
11641164

1165-
logger, err := cmd.SetupLogging(ctx, v)
1165+
logger, err := setup.Logging(ctx, v)
11661166
require.NoError(t, err)
11671167

11681168
defer logger.Flush()

cmd/run.go

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
stdlog "log"
99
"os"
10-
"path/filepath"
1110
"runtime/debug"
1211
"strings"
1312

@@ -16,7 +15,6 @@ import (
1615
"github.com/wakatime/wakatime-cli/cmd/configwrite"
1716
"github.com/wakatime/wakatime-cli/cmd/fileexperts"
1817
cmdheartbeat "github.com/wakatime/wakatime-cli/cmd/heartbeat"
19-
"github.com/wakatime/wakatime-cli/cmd/logfile"
2018
cmdoffline "github.com/wakatime/wakatime-cli/cmd/offline"
2119
"github.com/wakatime/wakatime-cli/cmd/offlinecount"
2220
"github.com/wakatime/wakatime-cli/cmd/offlineprint"
@@ -29,6 +27,7 @@ import (
2927
"github.com/wakatime/wakatime-cli/pkg/ini"
3028
"github.com/wakatime/wakatime-cli/pkg/lexer"
3129
"github.com/wakatime/wakatime-cli/pkg/log"
30+
"github.com/wakatime/wakatime-cli/pkg/log/setup"
3231
"github.com/wakatime/wakatime-cli/pkg/metrics"
3332
"github.com/wakatime/wakatime-cli/pkg/offline"
3433
"github.com/wakatime/wakatime-cli/pkg/params"
@@ -38,7 +37,6 @@ import (
3837
"github.com/spf13/cobra"
3938
"github.com/spf13/viper"
4039
"go.uber.org/zap/zapcore"
41-
"gopkg.in/natefinch/lumberjack.v2"
4240
)
4341

4442
type diagnostics struct {
@@ -66,7 +64,7 @@ func RunE(cmd *cobra.Command, v *viper.Viper) error {
6664
}
6765
}
6866

69-
logger, err = SetupLogging(ctx, v)
67+
logger, err = setup.Logging(ctx, v)
7068
if err != nil {
7169
// log to std out and exit, as logger instance failed to setup
7270
stdlog.Fatalf("failed to setup logging: %s", err)
@@ -218,43 +216,6 @@ func parseConfigFiles(ctx context.Context, v *viper.Viper) error {
218216
return nil
219217
}
220218

221-
// SetupLogging uses the --log-file param to configure logging to file or stdout.
222-
// It returns a logger with the configured settings or the default settings if it's not set.
223-
func SetupLogging(ctx context.Context, v *viper.Viper) (*log.Logger, error) {
224-
params, err := logfile.LoadParams(ctx, v)
225-
if err != nil {
226-
return nil, fmt.Errorf("failed to load log params: %s", err)
227-
}
228-
229-
var destOutput io.Writer = os.Stdout
230-
231-
if !params.ToStdout {
232-
dir := filepath.Dir(params.File)
233-
if _, err := os.Stat(dir); os.IsNotExist(err) {
234-
err := os.MkdirAll(dir, 0750)
235-
if err != nil {
236-
return nil, fmt.Errorf("failed to create log file directory %q: %s", dir, err)
237-
}
238-
}
239-
240-
// rotate log files
241-
destOutput = &lumberjack.Logger{
242-
Filename: params.File,
243-
MaxSize: log.MaxLogFileSize,
244-
MaxBackups: log.MaxNumberOfBackups,
245-
}
246-
}
247-
248-
logger := log.New(
249-
destOutput,
250-
log.WithVerbose(params.Verbose),
251-
log.WithSendDiagsOnErrors(params.SendDiagsOnErrors),
252-
log.WithMetrics(params.Metrics),
253-
)
254-
255-
return logger, nil
256-
}
257-
258219
// cmdFn represents a command function.
259220
type cmdFn func(ctx context.Context, v *viper.Viper) (int, error)
260221

cmd/run_internal_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/wakatime/wakatime-cli/pkg/exitcode"
1717
"github.com/wakatime/wakatime-cli/pkg/ini"
1818
"github.com/wakatime/wakatime-cli/pkg/log"
19+
"github.com/wakatime/wakatime-cli/pkg/log/setup"
1920
"github.com/wakatime/wakatime-cli/pkg/version"
2021
"github.com/wakatime/wakatime-cli/pkg/vipertools"
2122

@@ -105,7 +106,7 @@ func TestRunCmd_Panic(t *testing.T) {
105106
v.Set("api-url", testServerURL)
106107
v.Set("log-file", logFile.Name())
107108

108-
logger, err := SetupLogging(ctx, v)
109+
logger, err := setup.Logging(ctx, v)
109110
require.NoError(t, err)
110111

111112
defer logger.Flush()
@@ -185,7 +186,7 @@ func TestRunCmd_Panic_Verbose(t *testing.T) {
185186
v.Set("api-url", testServerURL)
186187
v.Set("log-file", logFile.Name())
187188

188-
logger, err := SetupLogging(ctx, v)
189+
logger, err := setup.Logging(ctx, v)
189190
require.NoError(t, err)
190191

191192
defer logger.Flush()
@@ -314,7 +315,7 @@ func TestRunCmd_BackoffLoggedWithVerbose(t *testing.T) {
314315
v.Set("internal.backoff_retries", "1")
315316
v.Set("verbose", verbose)
316317

317-
logger, err := SetupLogging(ctx, v)
318+
logger, err := setup.Logging(ctx, v)
318319
require.NoError(t, err)
319320

320321
defer logger.Flush()
@@ -378,7 +379,7 @@ func TestRunCmd_BackoffNotLogged(t *testing.T) {
378379
v.Set("internal.backoff_retries", "1")
379380
v.Set("verbose", verbose)
380381

381-
logger, err := SetupLogging(ctx, v)
382+
logger, err := setup.Logging(ctx, v)
382383
require.NoError(t, err)
383384

384385
defer logger.Flush()

0 commit comments

Comments
 (0)