mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix: Decouple cloud sync from telemetry (#343)
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/safedep/dry/usefulerror"
|
"github.com/safedep/dry/usefulerror"
|
||||||
"github.com/safedep/pmg/config"
|
"github.com/safedep/pmg/config"
|
||||||
"github.com/safedep/pmg/errcodes"
|
"github.com/safedep/pmg/errcodes"
|
||||||
"github.com/safedep/pmg/internal/analytics"
|
|
||||||
"github.com/safedep/pmg/internal/audit"
|
"github.com/safedep/pmg/internal/audit"
|
||||||
"github.com/safedep/pmg/internal/ui"
|
"github.com/safedep/pmg/internal/ui"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -37,11 +36,6 @@ func newSyncCommand() *cobra.Command {
|
|||||||
func runSync(cmd *cobra.Command, args []string) error {
|
func runSync(cmd *cobra.Command, args []string) error {
|
||||||
cfg := config.Get()
|
cfg := config.Get()
|
||||||
|
|
||||||
if analytics.IsDisabled() {
|
|
||||||
ui.Infof("Cloud sync is disabled because telemetry is disabled (disable_telemetry or PMG_DISABLE_TELEMETRY)")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !cfg.Config.Cloud.Enabled {
|
if !cfg.Config.Cloud.Enabled {
|
||||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||||
WithCode(errcodes.Lifecycle).
|
WithCode(errcodes.Lifecycle).
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/safedep/dry/log"
|
"github.com/safedep/dry/log"
|
||||||
"github.com/safedep/pmg/config"
|
"github.com/safedep/pmg/config"
|
||||||
"github.com/safedep/pmg/internal/analytics"
|
|
||||||
"github.com/safedep/pmg/internal/audit"
|
"github.com/safedep/pmg/internal/audit"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -30,11 +29,6 @@ func runSyncBackground(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
cfg := config.Get()
|
cfg := config.Get()
|
||||||
|
|
||||||
if analytics.IsDisabled() {
|
|
||||||
log.Debugf("Auto-sync: telemetry disabled; exiting")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !cfg.Config.Cloud.Enabled || !cfg.Config.Cloud.AutoSync.Enabled {
|
if !cfg.Config.Cloud.Enabled || !cfg.Config.Cloud.AutoSync.Enabled {
|
||||||
log.Debugf("Auto-sync: cloud or auto_sync disabled; exiting")
|
log.Debugf("Auto-sync: cloud or auto_sync disabled; exiting")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
||||||
"github.com/safedep/dry/log"
|
"github.com/safedep/dry/log"
|
||||||
"github.com/safedep/pmg/config"
|
"github.com/safedep/pmg/config"
|
||||||
"github.com/safedep/pmg/internal/analytics"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var global *auditor
|
var global *auditor
|
||||||
@@ -27,7 +26,7 @@ func Initialize(cfg *config.RuntimeConfig) error {
|
|||||||
var sinks []Sink
|
var sinks []Sink
|
||||||
sinks = append(sinks, newEventlogSink())
|
sinks = append(sinks, newEventlogSink())
|
||||||
|
|
||||||
if cfg.Config.Cloud.Enabled && !analytics.IsDisabled() {
|
if cfg.Config.Cloud.Enabled {
|
||||||
cs, err := newCloudSink(cfg, newCloudSinkCIResolver())
|
cs, err := newCloudSink(cfg, newCloudSinkCIResolver())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Cloud sync initialization failed: %v", err)
|
log.Warnf("Cloud sync initialization failed: %v", err)
|
||||||
@@ -36,10 +35,6 @@ func Initialize(cfg *config.RuntimeConfig) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Config.Cloud.Enabled && analytics.IsDisabled() {
|
|
||||||
log.Warnf("Cloud sync is disabled because telemetry is disabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
setGlobal(newAuditor(sinks...))
|
setGlobal(newAuditor(sinks...))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"github.com/safedep/dry/log"
|
"github.com/safedep/dry/log"
|
||||||
"github.com/safedep/pmg/config"
|
"github.com/safedep/pmg/config"
|
||||||
"github.com/safedep/pmg/internal/analytics"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SyncBackgroundSubcommand is the cobra `Use` of the hidden child command
|
// SyncBackgroundSubcommand is the cobra `Use` of the hidden child command
|
||||||
@@ -48,9 +47,6 @@ func MaybeSpawnBackgroundSync(cfg *config.RuntimeConfig) {
|
|||||||
if !cfg.Config.Cloud.Enabled || !cfg.Config.Cloud.AutoSync.Enabled {
|
if !cfg.Config.Cloud.Enabled || !cfg.Config.Cloud.AutoSync.Enabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if analytics.IsDisabled() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !SyncCooldownElapsed(cfg.CloudSyncLastRunPath(), cfg.Config.Cloud.AutoSync.MinInterval) {
|
if !SyncCooldownElapsed(cfg.CloudSyncLastRunPath(), cfg.Config.Cloud.AutoSync.MinInterval) {
|
||||||
log.Debugf("Auto-sync cooldown not elapsed; skipping spawn")
|
log.Debugf("Auto-sync cooldown not elapsed; skipping spawn")
|
||||||
|
|||||||
@@ -83,6 +83,15 @@ func TestMaybeSpawnBackgroundSyncSpawnsByDefault(t *testing.T) {
|
|||||||
assert.NotEmpty(t, rec.calls[0].name, "spawned name should be a resolved binary path")
|
assert.NotEmpty(t, rec.calls[0].name, "spawned name should be a resolved binary path")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMaybeSpawnBackgroundSyncIgnoresTelemetryDisabled(t *testing.T) {
|
||||||
|
rec := withMockSpawner(t)
|
||||||
|
cfg := newAutoSyncConfig(t)
|
||||||
|
cfg.Config.DisableTelemetry = true
|
||||||
|
|
||||||
|
MaybeSpawnBackgroundSync(cfg)
|
||||||
|
assert.Equal(t, 1, rec.callCount())
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaybeSpawnBackgroundSyncShortCircuits(t *testing.T) {
|
func TestMaybeSpawnBackgroundSyncShortCircuits(t *testing.T) {
|
||||||
t.Run("nil config", func(t *testing.T) {
|
t.Run("nil config", func(t *testing.T) {
|
||||||
rec := withMockSpawner(t)
|
rec := withMockSpawner(t)
|
||||||
@@ -108,15 +117,6 @@ func TestMaybeSpawnBackgroundSyncShortCircuits(t *testing.T) {
|
|||||||
assert.Equal(t, 0, rec.callCount())
|
assert.Equal(t, 0, rec.callCount())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("telemetry disabled via config", func(t *testing.T) {
|
|
||||||
rec := withMockSpawner(t)
|
|
||||||
cfg := newAutoSyncConfig(t)
|
|
||||||
cfg.Config.DisableTelemetry = true
|
|
||||||
|
|
||||||
MaybeSpawnBackgroundSync(cfg)
|
|
||||||
assert.Equal(t, 0, rec.callCount())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("we are the sync-background child", func(t *testing.T) {
|
t.Run("we are the sync-background child", func(t *testing.T) {
|
||||||
rec := withMockSpawner(t)
|
rec := withMockSpawner(t)
|
||||||
cfg := newAutoSyncConfig(t)
|
cfg := newAutoSyncConfig(t)
|
||||||
|
|||||||
Reference in New Issue
Block a user