Skip to content

Commit

Permalink
Merge pull request #1067 from wakatime/feature/remove-offline-only
Browse files Browse the repository at this point in the history
Remove --offline-only flag
  • Loading branch information
alanhamlett authored Jul 23, 2024
2 parents 3bc6eef + b1f073a commit f039f71
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 72 deletions.
2 changes: 0 additions & 2 deletions cmd/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ func SendHeartbeats(v *viper.Viper, queueFilepath string) error {
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))
} else if params.Offline.OfflineOnly {
return errors.New("--offline-only can NOT be used with --disable-offline")
}

handleOpts = append(handleOpts, backoff.WithBackoff(backoff.Config{
Expand Down
4 changes: 2 additions & 2 deletions cmd/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
)

// SaveHeartbeats saves heartbeats to the offline db without trying to send to the API.
// Used when we have more heartbeats than `offline.SendLimit`, when --offline-only enabled,
// when we couldn't send heartbeats to the API, or the API returned an auth error.
// Used when we have more heartbeats than `offline.SendLimit`, when we couldn't send
// heartbeats to the API, or the API returned an auth error.
func SaveHeartbeats(v *viper.Viper, heartbeats []heartbeat.Heartbeat, queueFilepath string) error {
params, err := loadParams(v)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ type (
// Offline contains offline related parameters.
Offline struct {
Disabled bool
OfflineOnly bool
PrintMax int
QueueFile string
QueueFileLegacy string
Expand Down Expand Up @@ -657,7 +656,6 @@ func LoadOfflineParams(v *viper.Viper) Offline {

return Offline{
Disabled: disabled,
OfflineOnly: v.GetBool("offline-only"),
PrintMax: v.GetInt("print-offline-heartbeats"),
QueueFile: vipertools.GetString(v, "offline-queue-file"),
QueueFileLegacy: vipertools.GetString(v, "offline-queue-file-legacy"),
Expand Down Expand Up @@ -1041,9 +1039,8 @@ func (p Heartbeat) String() string {
// String implements fmt.Stringer interface.
func (p Offline) String() string {
return fmt.Sprintf(
"disabled: %t, offline only: %t, print max: %d, queue file: '%s', queue file legacy: '%s', num sync max: %d",
"disabled: %t, print max: %d, queue file: '%s', queue file legacy: '%s', num sync max: %d",
p.Disabled,
p.OfflineOnly,
p.PrintMax,
p.QueueFile,
p.QueueFileLegacy,
Expand Down
2 changes: 1 addition & 1 deletion cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2505,7 +2505,7 @@ func TestOffline_String(t *testing.T) {

assert.Equal(
t,
"disabled: true, offline only: false, print max: 6, queue file: '/path/to/queue.file',"+
"disabled: true, print max: 6, queue file: '/path/to/queue.file',"+
" queue file legacy: '/path/to/legacy.file', num sync max: 12",
offline.String(),
)
Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) {
" new heartbeats.", offline.SyncMaxDefault),
)
flags.Bool("offline-count", false, "Prints the number of heartbeats in the offline db, then exits.")
flags.Bool("offline-only", false, "Saves the heartbeat(s) to the offline db, then exits.")
flags.Int(
"timeout",
api.DefaultTimeoutSecs,
Expand Down
6 changes: 0 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ func RunE(cmd *cobra.Command, v *viper.Viper) error {
if v.IsSet("entity") {
log.Debugln("command: heartbeat")

if v.GetBool("offline-only") {
exitCode := saveHeartbeats(v)

os.Exit(exitCode) // nolint:gocritic
}

return RunCmdWithOfflineSync(v, logFileParams.Verbose, logFileParams.SendDiagsOnErrors, cmdheartbeat.Run)
}

Expand Down
56 changes: 0 additions & 56 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,62 +460,6 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
assert.Eventually(t, func() bool { return numCalls == 4 }, time.Second, 50*time.Millisecond)
}

func TestSendHeartbeats_OfflineOnly(t *testing.T) {
apiURL, router, close := setupTestServer()
defer close()

router.HandleFunc("/users/current/heartbeats.bulk", func(_ http.ResponseWriter, _ *http.Request) {
require.FailNow(t, "Should not make any API request")
})

tmpDir := t.TempDir()

offlineQueueFile, err := os.CreateTemp(tmpDir, "")
require.NoError(t, err)

defer offlineQueueFile.Close()

tmpConfigFile, err := os.CreateTemp(tmpDir, "wakatime.cfg")
require.NoError(t, err)

defer tmpConfigFile.Close()

tmpInternalConfigFile, err := os.CreateTemp(tmpDir, "wakatime-internal.cfg")
require.NoError(t, err)

defer tmpInternalConfigFile.Close()

data, err := os.ReadFile("testdata/extra_heartbeats.json")
require.NoError(t, err)

buffer := bytes.NewBuffer(data)

runWakatimeCli(
t,
buffer,
"--api-url", apiURL,
"--key", "00000000-0000-4000-8000-000000000000",
"--config", tmpConfigFile.Name(),
"--internal-config", tmpInternalConfigFile.Name(),
"--entity", "testdata/main.go",
"--extra-heartbeats", "true",
"--cursorpos", "100",
"--offline-queue-file", offlineQueueFile.Name(),
"--offline-only",
"--lineno", "42",
"--lines-in-file", "100",
"--time", "1585598059",
"--hide-branch-names", ".*",
"--write",
"--verbose",
)

offlineCount, err := offline.CountHeartbeats(offlineQueueFile.Name())
require.NoError(t, err)

assert.Equal(t, 27, offlineCount)
}

func TestSendHeartbeats_Err(t *testing.T) {
apiURL, router, close := setupTestServer()
defer close()
Expand Down

0 comments on commit f039f71

Please sign in to comment.