Skip to content

Commit

Permalink
chore: consolidated extension logic (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding authored Jul 28, 2024
1 parent 4abbc62 commit 63ed5ab
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 154 deletions.
115 changes: 21 additions & 94 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ package cmd
import (
"context"
"encoding/json"
"errors"
"log/slog"
"os"

"github.com/karl-cardenas-coding/mywhoop/export"
"github.com/karl-cardenas-coding/mywhoop/internal"
"github.com/karl-cardenas-coding/mywhoop/notifications"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,6 +49,7 @@ func dump(ctx context.Context) error {
}

cfg := Configuration
cfg.Server.Enabled = false

ok, token, err := internal.VerfyToken(cfg.Credentials.CredentialsFile)
if err != nil {
Expand All @@ -63,25 +61,9 @@ func dump(ctx context.Context) error {
os.Exit(1)
}

var notificationMethod internal.Notification

switch cfg.Notification.Method {
case "ntfy":
ntfy := notifications.NewNtfy()
ntfy.ServerEndpoint = cfg.Notification.Ntfy.ServerEndpoint
ntfy.SubscriptionID = cfg.Notification.Ntfy.SubscriptionID
ntfy.UserName = cfg.Notification.Ntfy.UserName
ntfy.Events = cfg.Notification.Ntfy.Events
err = ntfy.SetUp()
if err != nil {
return err
}
notificationMethod = ntfy
slog.Info("Ntfy notification method configured")
default:
slog.Info("no notification method specified. Defaulting to stdout.")
std := notifications.NewStdout()
notificationMethod = std
notificationMethod, err := determineNotificationExtension(cfg)
if err != nil {
return err
}

if filter != "" {
Expand Down Expand Up @@ -172,83 +154,28 @@ func dump(ctx context.Context) error {
}
return err
}
var filePath string

switch cfg.Export.Method {
case "file":

if dataLocation == "" {
filePath = Configuration.Export.FileExport.FilePath
} else {
filePath = dataLocation
}

fileExp := export.NewFileExport(filePath,
Configuration.Export.FileExport.FileType,
Configuration.Export.FileExport.FileName,
Configuration.Export.FileExport.FileNamePrefix,
false,
)
err = fileExp.Export(finalDataRaw)
if err != nil {
notifyErr := notificationMethod.Publish(client, []byte(err.Error()), internal.EventErrors.String())
if notifyErr != nil {
slog.Error("unable to send notification", "error", notifyErr)
}
return err
}
slog.Info("Data exported successfully", "file", fileExp.FileName)
case "s3":

if dataLocation != "" {
cfg.Export.AWSS3.FileConfig.FilePath = dataLocation
}

awsS3, err := export.NewAwsS3Export(cfg.Export.AWSS3.Region,
cfg.Export.AWSS3.Bucket,
cfg.Export.AWSS3.Profile,
client,
&cfg.Export.AWSS3.FileConfig,
false,
)
if err != nil {
return errors.New("unable initialize AWS S3 export. Additional error context: " + err.Error())
}
err = awsS3.Export(finalDataRaw)
if err != nil {
notifyErr := notificationMethod.Publish(client, []byte(err.Error()), internal.EventErrors.String())
if notifyErr != nil {
slog.Error("unable to send notification", "error", notifyErr)
}
return errors.New("unable to export data to AWS S3. Additional error context: " + err.Error())
}

default:

if dataLocation == "" {
filePath = Configuration.Export.FileExport.FilePath
} else {
filePath = dataLocation
exporterMethod, err := determineExporterExtension(cfg, client, dataLocation)
if err != nil {
slog.Error("unable to determine export method", "error", err)
notifyErr := notificationMethod.Publish(client, []byte(err.Error()), internal.EventErrors.String())
if notifyErr != nil {
slog.Error("unable to send notification", "error", notifyErr)
}
return err
}

slog.Info("no export method specified. Defaulting to file.")
fileExp := export.NewFileExport(filePath,
Configuration.Export.FileExport.FileType,
Configuration.Export.FileExport.FileName,
Configuration.Export.FileExport.FileNamePrefix,
false,
)
err = fileExp.Export(finalDataRaw)
if err != nil {
notifyErr := notificationMethod.Publish(client, []byte(err.Error()), internal.EventErrors.String())
if notifyErr != nil {
slog.Error("unable to send notification", "error", notifyErr)
}
return err
err = exporterMethod.Export(finalDataRaw)
if err != nil {
slog.Error("unable to export data", "error", err)
notifyErr := notificationMethod.Publish(client, []byte(err.Error()), internal.EventErrors.String())
if notifyErr != nil {
slog.Error("unable to send notification", "error", notifyErr)
}

return err
}
slog.Info("All Whoop data downloaded successfully")

slog.Info("All Whoop data downloaded and exported successfully")
if notificationMethod != nil {
err = notificationMethod.Publish(client, []byte("Successfully downloaded all Whoop data."), internal.EventSuccess.String())
if err != nil {
Expand Down
108 changes: 108 additions & 0 deletions cmd/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cmd

import (
"errors"
"log/slog"
"net/http"

"github.com/karl-cardenas-coding/mywhoop/export"
"github.com/karl-cardenas-coding/mywhoop/internal"
"github.com/karl-cardenas-coding/mywhoop/notifications"
)

// determineExtension determines the notification extension to use and returns the appropriate notification.
func determineNotificationExtension(cfg internal.ConfigurationData) (internal.Notification, error) {

var notificationMethod internal.Notification

switch cfg.Notification.Method {
case "ntfy":
ntfy := notifications.NewNtfy()
ntfy.ServerEndpoint = cfg.Notification.Ntfy.ServerEndpoint
ntfy.SubscriptionID = cfg.Notification.Ntfy.SubscriptionID
ntfy.UserName = cfg.Notification.Ntfy.UserName
ntfy.Events = cfg.Notification.Ntfy.Events
err := ntfy.SetUp()
if err != nil {
return notificationMethod, err
}
slog.Info("Ntfy notification method configured")
notificationMethod = ntfy

default:
slog.Info("no notification method specified. Defaulting to stdout.")
std := notifications.NewStdout()
notificationMethod = std
}

return notificationMethod, nil

}

// determineExporterExtension determines the export extension to use and returns the appropriate export.
// The paramter isServerMode is used to determine if the exporter is being used in server mode. Use this flag to set server mode defaults.
func determineExporterExtension(cfg internal.ConfigurationData, client *http.Client, dataLocation string) (internal.Export, error) {

var (
filePath string
exporter internal.Export
)

switch cfg.Export.Method {
case "file":
if dataLocation == "" {
filePath = cfg.Export.FileExport.FilePath
}

if dataLocation != "" {
filePath = dataLocation
}

fileExp := export.NewFileExport(filePath,
cfg.Export.FileExport.FileType,
cfg.Export.FileExport.FileName,
cfg.Export.FileExport.FileNamePrefix,
cfg.Server.Enabled,
)
slog.Info("File export method specified")
exporter = fileExp

case "s3":
slog.Info("AWS S3 export method specified")
if dataLocation != "" {
cfg.Export.AWSS3.FileConfig.FilePath = dataLocation
}

awsS3, err := export.NewAwsS3Export(cfg.Export.AWSS3.Region,
cfg.Export.AWSS3.Bucket,
cfg.Export.AWSS3.Profile,
client,
&cfg.Export.AWSS3.FileConfig,
cfg.Server.Enabled,
)
if err != nil {
return exporter, errors.New("unable initialize AWS S3 export. Additional error context: " + err.Error())
}
exporter = awsS3

default:
if dataLocation == "" {
filePath = cfg.Export.FileExport.FilePath
} else {
filePath = dataLocation
}
slog.Info("no valid export method specified. Defaulting to file.")

fileExp := export.NewFileExport(filePath,
cfg.Export.FileExport.FileType,
cfg.Export.FileExport.FileName,
cfg.Export.FileExport.FileNamePrefix,
cfg.Server.Enabled,
)
exporter = fileExp

}

return exporter, nil

}
Loading

0 comments on commit 63ed5ab

Please sign in to comment.