Skip to content

Commit

Permalink
fixup! Check if legacy offline queue file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jul 29, 2024
1 parent 317e7bf commit e6f7195
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 86 deletions.
6 changes: 1 addition & 5 deletions cmd/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

// Run executes the heartbeat command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
log.Warnf("failed to load offline queue filepath: %s", err)
}
Expand Down Expand Up @@ -115,10 +115,6 @@ func SendHeartbeats(v *viper.Viper, queueFilepath string) error {
handleOpts := initHandleOptions(params)

if !params.Offline.Disabled {
if params.Offline.QueueFile != "" {
queueFilepath = params.Offline.QueueFile
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))
}

Expand Down
4 changes: 0 additions & 4 deletions cmd/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func SaveHeartbeats(v *viper.Viper, heartbeats []heartbeat.Heartbeat, queueFilep

handleOpts := initHandleOptions(params)

if params.Offline.QueueFile != "" {
queueFilepath = params.Offline.QueueFile
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))

sender := offline.Noop{}
Expand Down
9 changes: 1 addition & 8 deletions cmd/offlinecount/offlinecount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package offlinecount
import (
"fmt"

"github.com/wakatime/wakatime-cli/cmd/params"
"github.com/wakatime/wakatime-cli/pkg/exitcode"
"github.com/wakatime/wakatime-cli/pkg/offline"

Expand All @@ -12,20 +11,14 @@ import (

// Run executes the offline-count command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"failed to load offline queue filepath: %s",
err,
)
}

p := params.LoadOfflineParams(v)

if p.QueueFile != "" {
queueFilepath = p.QueueFile
}

count, err := offline.CountHeartbeats(queueFilepath)
if err != nil {
fmt.Println(err)
Expand Down
6 changes: 1 addition & 5 deletions cmd/offlineprint/offlineprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Run executes the print-offline-heartbeats command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"failed to load offline queue filepath: %s",
Expand All @@ -25,10 +25,6 @@ func Run(v *viper.Viper) (int, error) {

p := params.LoadOfflineParams(v)

if p.QueueFile != "" {
queueFilepath = p.QueueFile
}

hh, err := offline.ReadHeartbeats(queueFilepath, p.PrintMax)
if err != nil {
fmt.Println(err)
Expand Down
12 changes: 2 additions & 10 deletions cmd/offlinesync/offlinesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func run(v *viper.Viper) (int, error) {
return exitcode.Success, nil
}

queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"offline sync failed: failed to load offline queue filepath: %s",
err,
)
}

queueFilepathLegacy, err := offline.QueueFilepathLegacy()
queueFilepathLegacy, err := offline.QueueFilepathLegacy(v)
if err != nil {
log.Warnf("legacy offline sync failed: failed to load offline queue filepath: %s", err)
}
Expand Down Expand Up @@ -100,10 +100,6 @@ func syncOfflineActivityLegacy(v *viper.Viper, queueFilepath string) error {
return fmt.Errorf("failed to initialize api client: %w", err)
}

if paramOffline.QueueFileLegacy != "" {
queueFilepath = paramOffline.QueueFileLegacy
}

handle := heartbeat.NewHandle(apiClient,
offline.WithSync(queueFilepath, paramOffline.SyncMax),
apikey.WithReplacing(apikey.Config{
Expand Down Expand Up @@ -139,10 +135,6 @@ func SyncOfflineActivity(v *viper.Viper, queueFilepath string) error {

paramOffline := params.LoadOfflineParams(v)

if paramOffline.QueueFile != "" {
queueFilepath = paramOffline.QueueFile
}

handle := heartbeat.NewHandle(apiClient,
offline.WithSync(queueFilepath, paramOffline.SyncMax),
apikey.WithReplacing(apikey.Config{
Expand Down
29 changes: 11 additions & 18 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@ type (

// Offline contains offline related parameters.
Offline struct {
Disabled bool
LastSentAt time.Time
PrintMax int
QueueFile string
QueueFileLegacy string
RateLimit time.Duration
SyncMax int
Disabled bool
LastSentAt time.Time
PrintMax int
RateLimit time.Duration
SyncMax int
}

// ProjectParams params for project name sanitization.
Expand Down Expand Up @@ -674,13 +672,11 @@ func LoadOfflineParams(v *viper.Viper) Offline {
}

return Offline{
Disabled: disabled,
LastSentAt: lastSentAt,
PrintMax: v.GetInt("print-offline-heartbeats"),
QueueFile: vipertools.GetString(v, "offline-queue-file"),
QueueFileLegacy: vipertools.GetString(v, "offline-queue-file-legacy"),
RateLimit: time.Duration(rateLimit) * time.Second,
SyncMax: syncMax,
Disabled: disabled,
LastSentAt: lastSentAt,
PrintMax: v.GetInt("print-offline-heartbeats"),
RateLimit: time.Duration(rateLimit) * time.Second,
SyncMax: syncMax,
}
}

Expand Down Expand Up @@ -1074,13 +1070,10 @@ func (p Offline) String() string {
}

return fmt.Sprintf(
"disabled: %t, last sent at: '%s', print max: %d, queue file: '%s', queue file legacy: '%s',"+
" num rate limit: %d, num sync max: %d",
"disabled: %t, last sent at: '%s', print max: %d, num rate limit: %d, num sync max: %d",
p.Disabled,
lastSentAt,
p.PrintMax,
p.QueueFile,
p.QueueFileLegacy,
p.RateLimit,
p.SyncMax,
)
Expand Down
31 changes: 5 additions & 26 deletions cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,24 +1771,6 @@ func TestLoadOfflineParams_LastSentAt_Err(t *testing.T) {
assert.Zero(t, params.LastSentAt)
}

func TestLoadOfflineParams_QueueFile(t *testing.T) {
v := viper.New()
v.Set("offline-queue-file", "/path/to/file")

params := cmdparams.LoadOfflineParams(v)

assert.Equal(t, "/path/to/file", params.QueueFile)
}

func TestLoadOfflineParams_QueueFileLegacy(t *testing.T) {
v := viper.New()
v.Set("offline-queue-file-legacy", "/path/to/file")

params := cmdparams.LoadOfflineParams(v)

assert.Equal(t, "/path/to/file", params.QueueFileLegacy)
}

func TestLoadOfflineParams_SyncMax(t *testing.T) {
v := viper.New()
v.Set("sync-offline-activity", 42)
Expand Down Expand Up @@ -2552,19 +2534,16 @@ func TestOffline_String(t *testing.T) {
require.NoError(t, err)

offline := cmdparams.Offline{
Disabled: true,
LastSentAt: lastSentAt,
PrintMax: 6,
QueueFile: "/path/to/queue.file",
QueueFileLegacy: "/path/to/legacy.file",
RateLimit: 15,
SyncMax: 12,
Disabled: true,
LastSentAt: lastSentAt,
PrintMax: 6,
RateLimit: 15,
SyncMax: 12,
}

assert.Equal(
t,
"disabled: true, last sent at: '2021-08-30T18:50:42-03:00', print max: 6,"+
" queue file: '/path/to/queue.file', queue file legacy: '/path/to/legacy.file',"+
" num rate limit: 15, num sync max: 12",
offline.String(),
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func runCmd(v *viper.Viper, verbose bool, sendDiagsOnErrors bool, cmd cmdFn) (er
}

func saveHeartbeats(v *viper.Viper) int {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
log.Warnf("failed to load offline queue filepath: %s", err)
}
Expand Down
10 changes: 5 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build integration

package main_test

import (
Expand Down Expand Up @@ -373,7 +371,7 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
tmpDir := t.TempDir()

// create legacy offline queue file and add some heartbeats
offlineQueueFileLegacy, err := os.CreateTemp(tmpDir, "")
offlineQueueFileLegacy, err := os.CreateTemp(tmpDir, "legacy-offline-file")
require.NoError(t, err)

// early close to avoid file locking in Windows
Expand Down Expand Up @@ -409,7 +407,7 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
err = db.Close()
require.NoError(t, err)

offlineQueueFile, err := os.CreateTemp(tmpDir, "")
offlineQueueFile, err := os.CreateTemp(tmpDir, "new-offline-file")
require.NoError(t, err)

defer offlineQueueFile.Close()
Expand All @@ -429,6 +427,8 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)

buffer := bytes.NewBuffer(data)

assert.FileExists(t, offlineQueueFileLegacy.Name())

runWakatimeCli(
t,
buffer,
Expand All @@ -439,11 +439,11 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
"--entity", "testdata/main.go",
"--extra-heartbeats", "true",
"--cursorpos", "12",
"--sync-offline-activity", "0",
"--offline-queue-file", offlineQueueFile.Name(),
"--offline-queue-file-legacy", offlineQueueFileLegacy.Name(),
"--lineno", "42",
"--lines-in-file", "100",
"--heartbeat-rate-limit-seconds", "0",
"--time", "1585598059",
"--hide-branch-names", ".*",
"--write",
Expand Down
15 changes: 14 additions & 1 deletion pkg/offline/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/vipertools"
)

// dbLegacyFilename is the legacy bolt db filename.
Expand All @@ -14,7 +17,17 @@ const dbLegacyFilename = ".wakatime.bdb"
// the user's $HOME folder cannot be detected, it defaults to the
// current directory.
// This is used to support the old db file name and will be removed in the future.
func QueueFilepathLegacy() (string, error) {
func QueueFilepathLegacy(v *viper.Viper) (string, error) {
paramFile := vipertools.GetString(v, "offline-queue-file-legacy")
if paramFile != "" {
p, err := homedir.Expand(paramFile)
if err != nil {
return "", fmt.Errorf("failed expanding offline-queue-file-legacy param: %s", err)
}

return p, nil
}

home, _, err := ini.WakaHomeDir()
if err != nil {
return dbFilename, fmt.Errorf("failed getting user's home directory, defaulting to current directory: %s", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/offline/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/wakatime/wakatime-cli/pkg/offline"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -40,7 +41,8 @@ func TestQueueFilepathLegacy(t *testing.T) {

defer os.Unsetenv("WAKATIME_HOME")

queueFilepath, err := offline.QueueFilepathLegacy()
v := viper.New()
queueFilepath, err := offline.QueueFilepathLegacy(v)
require.NoError(t, err)

assert.Equal(t, test.Expected, queueFilepath)
Expand Down
15 changes: 14 additions & 1 deletion pkg/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"path/filepath"
"time"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/wakatime/wakatime-cli/pkg/api"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/log"
"github.com/wakatime/wakatime-cli/pkg/vipertools"

bolt "go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -91,7 +94,17 @@ func WithQueue(filepath string) heartbeat.HandleOption {
// QueueFilepath returns the path for offline queue db file. If
// the resource directory cannot be detected, it defaults to the
// current directory.
func QueueFilepath() (string, error) {
func QueueFilepath(v *viper.Viper) (string, error) {
paramFile := vipertools.GetString(v, "offline-queue-file")
if paramFile != "" {
p, err := homedir.Expand(paramFile)
if err != nil {
return "", fmt.Errorf("failed expanding offline-queue-file param: %s", err)
}

return p, nil
}

folder, err := ini.WakaResourcesDir()
if err != nil {
return dbFilename, fmt.Errorf("failed getting resource directory, defaulting to current directory: %s", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/offline/offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/spf13/viper"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/offline"
Expand Down Expand Up @@ -43,7 +44,8 @@ func TestQueueFilepath(t *testing.T) {
folder, err := ini.WakaResourcesDir()
require.NoError(t, err)

queueFilepath, err := offline.QueueFilepath()
v := viper.New()
queueFilepath, err := offline.QueueFilepath(v)
require.NoError(t, err)

expected := filepath.Join(folder, "offline_heartbeats.bdb")
Expand Down

0 comments on commit e6f7195

Please sign in to comment.