Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Aug 19, 2023
1 parent 881d2a3 commit 62f0fe2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
8 changes: 5 additions & 3 deletions lib/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (a *Application) loadWorkflowFile(filename string) *workflow.StackupWorkflo
}

func (a *Application) init() {
utils.EnsureConfigDirExists("stackup")
a.Gateway = gateway.New([]string{}, []string{}, []string{}, []string{})
a.ConfigFilename = support.FindExistingFile([]string{"stackup.dist.yaml", "stackup.yaml"}, "stackup.yaml")

Expand Down Expand Up @@ -405,11 +406,12 @@ func (a *Application) Run() {
a.init()
a.handleFlagOptions()

a.Analytics = telemetry.New(true, a.Gateway)
a.Workflow.Initialize()
a.Analytics = telemetry.New(false, a.Gateway)
a.Workflow.Initialize(a.GetConfigurationPath())
a.Gateway.Initialize(a.Workflow.Settings)
a.Analytics.IsEnabled = *a.Workflow.Settings.AnonymousStatistics

if *a.Workflow.Settings.AnonymousStatistics {
if a.Analytics.IsEnabled {
a.Analytics.EventOnly("app.start")
}

Expand Down
26 changes: 2 additions & 24 deletions lib/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -57,34 +56,14 @@ func CreateCacheEntry(keyName string, value string, expiresAt *carbon.Carbon, ha
}

// The function creates and initializes a cache object.
func CreateCache(name string) *Cache {
result := Cache{Name: name, Enabled: false}
func CreateCache(name string, storagePath string) *Cache {
result := Cache{Name: name, Enabled: false, Path: storagePath}
result.Init()
result.Enabled = true

return &result
}

// The function ensures that a directory with a given name exists in the user's home directory and
// returns the path to the directory.
func ensureConfigDirExists(dirName string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

// Append the directory name to the home directory
configDir := filepath.Join(homeDir, dirName)

// Ensure the directory exists
err = os.MkdirAll(configDir, 0744)
if err != nil {
return "", err
}

return configDir, nil
}

func (ce *CacheEntry) IsExpired() bool {
return carbon.Parse(ce.ExpiresAt, "America/New_York").IsPast()
}
Expand All @@ -109,7 +88,6 @@ func (c *Cache) Init() {
return
}

c.Path, _ = ensureConfigDirExists(".stackup")
c.Filename = filepath.Join(c.Path, "stackup.db")
if c.Name == "" {
c.Name = projectinfo.New().FsSafeName()
Expand Down
12 changes: 6 additions & 6 deletions lib/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var POSTHOG_API_KEY_FOR_CLI = "V6yB20dWAVDZfG1yDOFkSQPeGachBJst3UPkVJJdkIS"

type Telemetry struct {
isEnabled bool
IsEnabled bool
posthogClient posthog.Client
}

Expand All @@ -42,9 +42,9 @@ func New(telemetryIsEnabled bool, gw *gateway.Gateway) *Telemetry {
},
)

return &Telemetry{isEnabled: telemetryIsEnabled, posthogClient: client}
return &Telemetry{IsEnabled: telemetryIsEnabled, posthogClient: client}
} else {
return &Telemetry{isEnabled: false}
return &Telemetry{IsEnabled: false}
}
}

Expand All @@ -53,7 +53,7 @@ func (t *Telemetry) EventOnly(name string) {
}

func (t *Telemetry) Event(name string, properties map[string]interface{}) {
if !t.isEnabled {
if !t.IsEnabled {
return
}

Expand All @@ -79,7 +79,7 @@ func (t *Telemetry) CaptureEvent(eventName string, properties posthog.Properties
return
}

if t.isEnabled {
if t.IsEnabled {
t.posthogClient.Enqueue(posthog.Capture{
DistinctId: userIdentity,
Event: eventName,
Expand All @@ -94,7 +94,7 @@ func (t *Telemetry) GetDistinctId() (string, error) {
var distinctId string
var outputErr error

machineId, err := machineid.ID()
machineId, err := machineid.ProtectedID("stackup")
if err != nil {
outputErr = err
}
Expand Down
4 changes: 2 additions & 2 deletions lib/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (workflow *StackupWorkflow) TryLoadDotEnvVaultFile(value string) bool {
return true
}

func (workflow *StackupWorkflow) Initialize() {
workflow.Cache = cache.CreateCache("")
func (workflow *StackupWorkflow) Initialize(configPath string) {
workflow.Cache = cache.CreateCache("", configPath)

// generate uuids for each task as the initial step, as other code below relies on a uuid existing
for _, task := range workflow.Tasks {
Expand Down

0 comments on commit 62f0fe2

Please sign in to comment.