Skip to content

Commit

Permalink
New slice and math utils and replaced environment utils with environm…
Browse files Browse the repository at this point in the history
…ent package.
  • Loading branch information
kristofferahl committed Jan 21, 2024
1 parent 7814629 commit 242c422
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
43 changes: 43 additions & 0 deletions internal/environment/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package environment

import (
"fmt"
"os"
"strconv"
"time"
)

func StringVar(key, defaultValue string) string {
v := os.Getenv(key)
if v != "" {
return v
}
return defaultValue
}

func IntVar(key string, defaultValue int) (int, error) {
v := StringVar(key, fmt.Sprintf("%d", defaultValue))
pv, err := strconv.Atoi(v)
if err != nil {
return 0, fmt.Errorf("failed parsing int from environment variable %s", key)
}
return pv, nil
}

func BoolVar(key string, defaultValue bool) (bool, error) {
v := StringVar(key, strconv.FormatBool(defaultValue))
pv, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("failed parsing boolean from environment variable %s", key)
}
return pv, nil
}

func DurationVar(key string, defaultValue time.Duration) (time.Duration, error) {
v := StringVar(key, defaultValue.String())
pv, err := time.ParseDuration(v)
if err != nil {
return time.Second * 0, fmt.Errorf("failed parsing duration from environment variable %s", key)
}
return pv, nil
}
3 changes: 2 additions & 1 deletion internal/store/awsParameterStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/dotnetmentor/racoon/internal/api"
"github.com/dotnetmentor/racoon/internal/config"
"github.com/dotnetmentor/racoon/internal/environment"
"github.com/dotnetmentor/racoon/internal/utils"
)

Expand Down Expand Up @@ -120,7 +121,7 @@ func (s *AwsParameterStore) Write(ctx config.AppContext, key, value, description
}

func newParameterStoreClient(ctx context.Context) (*ssm.Client, error) {
if awsRegion := utils.StringEnvVar("AWS_REGION", ""); awsRegion == "" {
if awsRegion := environment.StringVar("AWS_REGION", ""); awsRegion == "" {
return nil, fmt.Errorf("required environment variable AWS_REGION has no value set")
}

Expand Down
11 changes: 0 additions & 11 deletions internal/utils/environment.go

This file was deleted.

8 changes: 8 additions & 0 deletions internal/utils/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utils

func Min(a, b int) int {
if a < b {
return a
}
return b
}
17 changes: 17 additions & 0 deletions internal/utils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ func SliceDelete[T any](slice []T, match func(i T) bool) (result []T) {
}
return
}

func SliceTake[T any](slice []T, n int) (result []T) {
min := Min(len(slice), n)
for i := 0; i < min; i++ {
result = append(result, slice[i])
}
return
}

func SliceSkip[T any](slice []T, n int) (result []T) {
if n > 0 && n < len(slice) {
result = slice[n:]
} else {
result = slice
}
return
}

0 comments on commit 242c422

Please sign in to comment.