Skip to content

Commit

Permalink
chore: fixed behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding committed Jul 13, 2024
1 parent cf1b03e commit 1bc3532
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 25 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Execute(ctx context.Context, args []string, stdout, stderr *os.File, static
func init() {

// Global Flag - Config File
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "myWhoop config file - default is $HOME/.mywhoop.yaml")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "MyWhoop config file - default is $HOME/.mywhoop.yaml")
rootCmd.PersistentFlags().StringVarP(&VerbosityLevel, "debug", "d", "", "Enable debug output. Use the values DEBUG, INFO, WARN, ERROR, Default is INFO.")
rootCmd.PersistentFlags().StringVar(&CredentialsFile, "credentials", "", "File path to the Whoop credentials file that contains a valid authentication token.")

Expand All @@ -83,7 +83,7 @@ func InitLogger(cfg *internal.ConfigurationData) error {

ok, configFilePath := internal.CheckConfigFile(cfgFile)
if ok {
slog.Info("config file found", "config", configFilePath)
slog.Info("config file specified", "config", configFilePath)
config, err := internal.GenerateConfigStruct(configFilePath)
if err != nil {
slog.Error("unable to generate configuration struct", "error", err)
Expand Down Expand Up @@ -126,7 +126,7 @@ func InitLogger(cfg *internal.ConfigurationData) error {
func changeTimeFormat(groups []string, a slog.Attr) slog.Attr {

if a.Key == slog.TimeKey {
a.Value = slog.StringValue(time.Now().Format("2006/01/02 15:04:05"))
a.Value = slog.StringValue(time.Now().Local().Format("2006/01/02 15:04:05"))
}
return a

Expand Down
17 changes: 12 additions & 5 deletions docs/examples/systemd/mywhoop_docker.service
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ ExecStartPre=-/usr/bin/docker stop mywhoop
ExecStartPre=-/usr/bin/docker rm mywhoop
ExecStartPre=/usr/bin/docker pull ghcr.io/karl-cardenas-coding/mywhoop:v1.0.0
ExecStart=/usr/bin/docker run --name mywhoop \
-e WHOOP_CLIENT_ID=************* \
-e WHOOP_CLIENT_SECRET=************* \
-e NOTIFICATION_NTFY_AUTH_TOKEN=************* \
-e WHOOP_CREDENTIALS_FILE=/home/ubuntu/token.json \
-e WHOOP_CLIENT_ID=$WHOOP_CLIENT_ID \
-e WHOOP_CLIENT_SECRET=$WHOOP_CLIENT_SECRET \
-e NOTIFICATION_NTFY_AUTH_TOKEN=$NOTIFICATION_NTFY_AUTH_TOKEN \
-v /home/ubuntu/mywhoop/:app \
ghcr.io/karl-cardenas-coding/mywhoop:v1.0.0
ghcr.io/karl-cardenas-coding/mywhoop:v1.0.0 server \
--config /app/.mywhoop.yaml \
--credentials /app/token.json
Restart=on-failure
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/mywhoop/
Environment="WHOOP_CLIENT_ID=*************"
Environment="WHOOP_CLIENT_SECRET==*************"
Environment="NOTIFICATION_NTFY_AUTH_TOKEN=*************"
Environment="AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
Environment="AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY"
Environment="AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION"

[Install]
WantedBy=multi-user.target
2 changes: 2 additions & 0 deletions export/aws_s3_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -70,6 +71,7 @@ func NewAwsS3Export(region, bucket, profile string, client *http.Client, f *File

creds, err := cfg.Credentials.Retrieve(context.TODO())
if err != nil {
slog.Debug("error retrieving aws credentials", "error", err)
return nil, errors.New("ERROR RETRIEVING AWS CREDENTIALS")
}

Expand Down
28 changes: 11 additions & 17 deletions internal/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ func GenerateConfigStruct(filePath string) (ConfigurationData, error) {
func readConfigFileYaml(file string) (ConfigurationData, error) {

if _, err := os.Stat(file); os.IsNotExist(err) {
slog.Error("unable to read the input file", "error", err)
slog.Error("file not found", "file", file)
return ConfigurationData{}, errors.New("unable to read the input file")
}

fileContent, err := os.ReadFile(file)
if err != nil {
return ConfigurationData{}, errors.New("unable to read the input file")
slog.Error("unable to read the content of the file", "file", file)
return ConfigurationData{}, err
}

config := ConfigurationData{}
Expand All @@ -103,29 +104,22 @@ func readConfigFileYaml(file string) (ConfigurationData, error) {
}

// determineFileType validates the existence of an input file and ensures its prefix is json | yaml | yml
// If the file prefix is yml then it is converted to yaml.
func determineFileType(file string) (string, error) {
f, err := os.Stat(file)
if err != nil {
return "none", errors.New("unable to read the input file")
}
var fileType string

switch {
case strings.HasSuffix(f.Name(), "yaml"):
fileType = "yaml"
case strings.HasSuffix(file, "yaml"):
return "yaml", nil

case strings.HasSuffix(f.Name(), "json"):
fileType = "json"
case strings.HasSuffix(file, "json"):
return "json", nil

case strings.HasSuffix(f.Name(), "yml"):
fileType = "yaml"
case strings.HasSuffix(file, "yml"):
return "yaml", nil

default:
fileType = "none"
err = errors.New("invalid file type provided. Must be of type json, yaml or yml")
return "", errors.New("invalid file type provided. Must be of type json, yaml or yml")
}

return fileType, err
}

// validateConfiguration is a function that validates the configuration data
Expand Down
53 changes: 53 additions & 0 deletions internal/config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,56 @@ func TestReadConfigFileYaml(t *testing.T) {
}

}

func TestDetermineFileType(t *testing.T) {

tests := []struct {
description string
fileName string
expected string
errorExpected bool
}{
{
description: "Test file type is json",
fileName: "valid_config.json",
expected: "json",
errorExpected: false,
},

{
description: "Test file type is yaml",
fileName: "valid_config.yaml",
expected: "yaml",
errorExpected: false,
},

{
description: "Test file type is yml",
fileName: "valid_config.yml",
expected: "yaml",
errorExpected: false,
},

{
description: "Test file type is invalid",
fileName: "invalid_config.txt",
expected: "",
errorExpected: true,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
got, err := determineFileType(test.fileName)
if got != test.expected {
t.Fatalf("Failed to determine the file type. Expected %s but received %s", test.expected, got)
}

if test.errorExpected && err == nil {
t.Fatalf("Error expected but got nil %v", err)
}

})
}

}

0 comments on commit 1bc3532

Please sign in to comment.