Compare commits
1 Commits
v0.0.3
...
c124ce39c5
Author | SHA1 | Date | |
---|---|---|---|
|
c124ce39c5 |
1
.cuddle/base
Submodule
1
.cuddle/base
Submodule
Submodule .cuddle/base added at 86167c5614
110
.cuddle/tmp/cuddle-go-lib-plan.yaml
Normal file
110
.cuddle/tmp/cuddle-go-lib-plan.yaml
Normal file
@@ -0,0 +1,110 @@
|
||||
kind: pipeline
|
||||
name: cuddle-go-lib-plan
|
||||
type: docker
|
||||
|
||||
steps:
|
||||
- name: load_secret
|
||||
image: debian:buster-slim
|
||||
volumes:
|
||||
- name: ssh
|
||||
path: /root/.ssh/
|
||||
environment:
|
||||
SSH_KEY:
|
||||
from_secret: gitea_id_ed25519
|
||||
commands:
|
||||
- mkdir -p $HOME/.ssh/
|
||||
- echo "$SSH_KEY" | base64 -d > $HOME/.ssh/id_ed25519
|
||||
- chmod -R 600 ~/.ssh
|
||||
- |
|
||||
cat >$HOME/.ssh/config <<EOL
|
||||
Host git.front.kjuulh.io
|
||||
IdentityFile $HOME/.ssh/id_ed25519
|
||||
IdentitiesOnly yes
|
||||
UserKnownHostsFile=/dev/null
|
||||
StrictHostKeyChecking no
|
||||
EOL
|
||||
- chmod 700 ~/.ssh/config
|
||||
|
||||
- name: build pr
|
||||
image: kasperhermansen/{{bin_name}}:{{image_tag}}
|
||||
volumes:
|
||||
- name: ssh
|
||||
path: /root/.ssh/
|
||||
commands:
|
||||
- eval `ssh-agent`
|
||||
- ssh-add
|
||||
- echo "$DOCKER_PASSWORD" | docker login --password-stdin --username="$DOCKER_USERNAME" docker.io
|
||||
- export CLUSTER=clank-dev
|
||||
- cuddle --version
|
||||
- {{ bin_name }} pr
|
||||
environment:
|
||||
DAGGER_CLOUD_TOKEN:
|
||||
from_secret: dagger_cloud_token
|
||||
DRONE_HOST: "https://ci.i.kjuulh.io"
|
||||
DRONE_USER: "kjuulh"
|
||||
DRONE_TOKEN:
|
||||
from_secret: drone_token
|
||||
DOCKER_BUILDKIT: 1
|
||||
DOCKER_PASSWORD:
|
||||
from_secret: docker_password
|
||||
DOCKER_USERNAME:
|
||||
from_secret: docker_username
|
||||
CUDDLE_SSH_AGENT: "true"
|
||||
GIT_PASSWORD:
|
||||
from_secret: git_password
|
||||
CI_PREFIX: "/mnt/ci/ci"
|
||||
DOCKER_HOST: "tcp://192.168.1.155:2376"
|
||||
CUDDLE_PLEASE_TOKEN:
|
||||
from_secret: cuddle_please_token
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
depends_on:
|
||||
- "load_secret"
|
||||
|
||||
- name: build main
|
||||
image: kasperhermansen/{{bin_name}}:{{image_tag}}
|
||||
volumes:
|
||||
- name: ssh
|
||||
path: /root/.ssh/
|
||||
commands:
|
||||
- eval `ssh-agent`
|
||||
- ssh-add
|
||||
- echo "$DOCKER_PASSWORD" | docker login --password-stdin --username="$DOCKER_USERNAME" docker.io
|
||||
- export CLUSTER=clank-prod
|
||||
- cuddle --version
|
||||
- {{ bin_name }} main
|
||||
environment:
|
||||
DAGGER_CLOUD_TOKEN:
|
||||
from_secret: dagger_cloud_token
|
||||
DRONE_HOST: "https://ci.i.kjuulh.io"
|
||||
DRONE_USER: "kjuulh"
|
||||
DRONE_TOKEN:
|
||||
from_secret: drone_token
|
||||
DOCKER_BUILDKIT: 1
|
||||
DOCKER_PASSWORD:
|
||||
from_secret: docker_password
|
||||
DOCKER_USERNAME:
|
||||
from_secret: docker_username
|
||||
CUDDLE_SSH_AGENT: "true"
|
||||
GIT_PASSWORD:
|
||||
from_secret: git_password
|
||||
CI_PREFIX: "/mnt/ci/ci"
|
||||
DOCKER_HOST: "tcp://192.168.1.155:2376"
|
||||
CUDDLE_PLEASE_TOKEN:
|
||||
from_secret: cuddle_please_token
|
||||
when:
|
||||
event:
|
||||
- push
|
||||
branch:
|
||||
- main
|
||||
- master
|
||||
exclude:
|
||||
- pull_request
|
||||
depends_on:
|
||||
- "load_secret"
|
||||
|
||||
volumes:
|
||||
- name: ssh
|
||||
temp: {}
|
||||
|
@@ -1,3 +0,0 @@
|
||||
kind: template
|
||||
load: cuddle-go-lib-plan.yaml
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
.cuddle/
|
18
CHANGELOG.md
18
CHANGELOG.md
@@ -6,27 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.0.3] - 2025-07-23
|
||||
|
||||
### Added
|
||||
- handle errors via. required (tests)
|
||||
- only error on non required values
|
||||
|
||||
## [0.0.2] - 2025-07-22
|
||||
|
||||
### Added
|
||||
- add a global handler for getting items
|
||||
- rename tag
|
||||
|
||||
### Other
|
||||
- Configure Renovate (#2)
|
||||
Add renovate.json
|
||||
|
||||
## [0.0.1] - 2025-07-21
|
||||
|
||||
### Added
|
||||
- remove .cuddle
|
||||
- add cuddle please
|
||||
- add cuddle
|
||||
- remove fluff
|
||||
- add readme
|
||||
|
25
config.go
25
config.go
@@ -1,9 +1,9 @@
|
||||
package yourconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -11,17 +11,8 @@ import (
|
||||
"github.com/ettle/strcase"
|
||||
)
|
||||
|
||||
func MustLoadContext[T any](ctx context.Context) T {
|
||||
output, err := LoadContext[T](ctx)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("must load: %s", err.Error()))
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func MustLoad[T any]() T {
|
||||
output, err := LoadContext[T](context.Background())
|
||||
output, err := Load[T]()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("must load: %s", err.Error()))
|
||||
}
|
||||
@@ -30,10 +21,6 @@ func MustLoad[T any]() T {
|
||||
}
|
||||
|
||||
func Load[T any]() (T, error) {
|
||||
return LoadContext[T](context.Background())
|
||||
}
|
||||
|
||||
func LoadContext[T any](ctx context.Context) (T, error) {
|
||||
var cfg T
|
||||
|
||||
v := reflect.ValueOf(&cfg).Elem()
|
||||
@@ -44,7 +31,7 @@ func LoadContext[T any](ctx context.Context) (T, error) {
|
||||
OUTER:
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
tagStr := field.Tag.Get("cfg")
|
||||
tagStr := field.Tag.Get("conf")
|
||||
if tagStr == "" {
|
||||
continue
|
||||
}
|
||||
@@ -93,11 +80,7 @@ OUTER:
|
||||
}
|
||||
}
|
||||
|
||||
valueStr, err := defaultLogger.Load().Get(ctx, tag.Env)
|
||||
if err != nil && tag.Required {
|
||||
errs = append(errs, fmt.Errorf("field: %s failed to load: %w", field.Name, err))
|
||||
continue OUTER
|
||||
}
|
||||
valueStr := os.Getenv(tag.Env)
|
||||
if valueStr == "" && tag.Required {
|
||||
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
|
||||
continue OUTER
|
||||
|
@@ -24,7 +24,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("default tag, nothing set, no env set", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:""`
|
||||
SomeItem string `conf:""`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("default tag (required=true), nothing set, no env set, err", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"required:true"`
|
||||
SomeItem string `conf:"required:true"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("default tag (required=false), nothing set, no env set no error", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"required:false"`
|
||||
SomeItem string `conf:"required:false"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("env tag nothing set, no env set, no error", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"SOME_ITEM"`
|
||||
SomeItem string `conf:"SOME_ITEM"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("default tag (required=true), nothing set, no env set, err", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"SOME_ITEM,required:true"`
|
||||
SomeItem string `conf:"SOME_ITEM,required:true"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -91,7 +91,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("default tag (required), nothing set, no env set, err", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"SOME_ITEM,required"`
|
||||
SomeItem string `conf:"SOME_ITEM,required"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func TestLoad(t *testing.T) {
|
||||
t.Run("default tag private, trying to set, err", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string
|
||||
someOtherItem string `cfg:"required:true"`
|
||||
someOtherItem string `conf:"required:true"`
|
||||
someBool bool
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("env tag and env set, no error", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"required:true"`
|
||||
SomeItem string `conf:"required:true"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("env tag (different name) and env set, no error", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"DIFFERENT_NAME,required:true"`
|
||||
SomeItem string `conf:"DIFFERENT_NAME,required:true"`
|
||||
someOtherItem string
|
||||
someBool bool
|
||||
}
|
||||
@@ -151,8 +151,8 @@ func TestLoad(t *testing.T) {
|
||||
|
||||
t.Run("multiple env tag and env set, no error", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string `cfg:"required:true"`
|
||||
SomeOtherItem string `cfg:"required:true"`
|
||||
SomeItem string `conf:"required:true"`
|
||||
SomeOtherItem string `conf:"required:true"`
|
||||
someBool bool
|
||||
}
|
||||
|
||||
@@ -165,36 +165,4 @@ func TestLoad(t *testing.T) {
|
||||
assert.Equal(t, "some-item", val.SomeItem)
|
||||
assert.Equal(t, "some-other-item", val.SomeOtherItem)
|
||||
})
|
||||
|
||||
t.Run("required true, error returned", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string
|
||||
SomeOtherItem string `cfg:"required:true"`
|
||||
someBool bool
|
||||
}
|
||||
|
||||
t.Setenv("SOME_OTHER_ITEM", "")
|
||||
|
||||
val, err := yourconfig.Load[Config]()
|
||||
require.Error(t, err)
|
||||
require.Zero(t, val)
|
||||
|
||||
assert.Equal(t, "config failed: field: SomeOtherItem (env=SOME_OTHER_ITEM) is not set and is required", err.Error())
|
||||
})
|
||||
|
||||
t.Run("required false, no error returned", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string
|
||||
SomeOtherItem string `cfg:"required:false"`
|
||||
someBool bool
|
||||
}
|
||||
|
||||
t.Setenv("SOME_OTHER_ITEM", "")
|
||||
|
||||
val, err := yourconfig.Load[Config]()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Zero(t, val)
|
||||
})
|
||||
|
||||
}
|
||||
|
56
provider.go
56
provider.go
@@ -1,56 +0,0 @@
|
||||
package yourconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var defaultLogger atomic.Pointer[Provider]
|
||||
|
||||
func init() {
|
||||
defaultLogger.Store(newProvider())
|
||||
}
|
||||
|
||||
func SetDefault(provider *Provider) {
|
||||
defaultLogger.Store(provider)
|
||||
}
|
||||
|
||||
func Default() *Provider {
|
||||
return defaultLogger.Load()
|
||||
}
|
||||
|
||||
type Handler interface {
|
||||
Get(ctx context.Context, key string) (string, error)
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
handler Handler
|
||||
}
|
||||
|
||||
func (p *Provider) Get(ctx context.Context, key string) (string, error) {
|
||||
return p.handler.Get(ctx, key)
|
||||
}
|
||||
|
||||
func newProvider() *Provider {
|
||||
return &Provider{
|
||||
handler: defaultHandler(),
|
||||
}
|
||||
}
|
||||
|
||||
func New(handler Handler) *Provider {
|
||||
return &Provider{
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
type envHandler struct{}
|
||||
|
||||
func (e *envHandler) Get(ctx context.Context, key string) (string, error) {
|
||||
val := os.Getenv(key)
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func defaultHandler() Handler {
|
||||
return &envHandler{}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
||||
}
|
Reference in New Issue
Block a user