Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaqx committed Jun 12, 2024
0 parents commit 3ff5cbc
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
-
package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
-
package-ecosystem: "gomod"
directory: /
schedule:
interval: daily
50 changes: 50 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Go

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
uses: actions/setup-go@v5
with:
go-version: stable
cache: true
-
name: Test
run: go test -race -coverprofile=coverage.out -covermode=atomic ./...
-
name: Upload Coverage Artifact
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: coverage.out

codecov:
runs-on: ubuntu-latest
needs: [ test ]
steps:
- uses: actions/checkout@v4
-
name: Download Coverage Artifact
uses: actions/download-artifact@v4
with:
name: code-coverage-report
-
name: Validate codecov.yml
if: ${{ hashFiles('.codecov.yml') != '' }}
run: cat .codecov.yml | curl --data-binary @- https://codecov.io/validate
-
name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.4.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: golangci-lint
on:
push:
branches:
- main
- master
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.58
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Chase Pierce

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# env

[![Go Reference](https://pkg.go.dev/badge/github.com/syntaqx/env.svg)](https://pkg.go.dev/github.com/syntaqx/env)
[![codecov](https://codecov.io/gh/syntaqx/env/graph/badge.svg?token=m4bBKy3UG3)](https://codecov.io/gh/syntaqx/env)

`env` is a utility package to aid when using environment variables.

### Usage

```go
package main

import (
"fmt"
"github.com/syntaqx/env"
)

func main() {
// TODO
}
```

### Roadmap

> [!NOTE]
> This project is going to be updated on an as needed basis for my other
> projects, but these are things I expect I'll need in the future.
- [ ] Add type casting for environment variables
- [ ] Add support for loading .env files
- [ ] Add support for struct tags
30 changes: 30 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package env

import (
"os"
)

// Set sets an environment variable.
func Set(key, value string) error {
return os.Setenv(key, value)
}

// Lookup returns the value of an environment variable and a boolean indicating
// whether the variable is present in the environment.
func Lookup(key string) (string, bool) {
return os.LookupEnv(key)
}

// Get returns the value of an environment variable.
func Get(key string) string {
return os.Getenv(key)
}

// GetWithFallback returns the value of an environment variable or a fallback
// value if the environment variable is not set.
func GetWithFallback(key string, fallback string) string {
if value, ok := Lookup(key); ok {
return value
}
return fallback
}
58 changes: 58 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package env

import (
"os"
"testing"
)

func TestGetWithFallback(t *testing.T) {
tests := []struct {
setValue bool
key string
fallback string
envValue string
wantValue string
}{
{
setValue: true,
key: "EXISTING_KEY",
fallback: "fallback_value",
envValue: "existing_value",
wantValue: "existing_value",
},
{
setValue: false,
key: "NON_EXISTING_KEY",
fallback: "fallback_value",
envValue: "",
wantValue: "fallback_value",
},
{
setValue: true,
key: "EMPTY_VALUE_KEY",
fallback: "fallback_value",
envValue: "",
wantValue: "",
},
}

for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
// Set the environment variable
if tt.setValue {
os.Setenv(tt.key, tt.envValue)
}

// Call the function under test
got := GetWithFallback(tt.key, tt.fallback)

// Check if the returned value matches the expected value
if got != tt.wantValue {
t.Errorf("GetWithFallback(%q, %q) = %q, want %q", tt.key, tt.fallback, got, tt.wantValue)
}

// Unset the environment variable
os.Unsetenv(tt.key)
})
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/syntaqx/env

go 1.22.3

0 comments on commit 3ff5cbc

Please sign in to comment.