Skip to content

Commit

Permalink
Fix print issue, add CI action (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnorth authored Feb 4, 2024
1 parent a28aa66 commit 25fb8b3
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "CI Checks"
on:
workflow_dispatch:
push:

jobs:
run-ci:
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build CLI
run: |
make build
- name: Run tests
run: |
make test
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"mode": "auto",
"program": "./main.go",
"cwd": ".",
"args": ["print", "-d", "test/contexts/standards"]
"args": ["print", "-d", "test/contexts/standard"]
}
]
}
27 changes: 22 additions & 5 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"

"github.com/maxnorth/nv/internal/resolver"
"github.com/spf13/cobra"
Expand All @@ -23,7 +25,17 @@ func NewPrintCmd() *cobra.Command {
return err
}

printEnv(values, cmd.Flag("output").Value.String())
keys := r.LoadedVars
for _, key := range r.LoadedVars {
delete(values, key)
}
for key := range values {
keys = append(keys, key)
}

sort.Strings(keys)

printEnv(keys, cmd.Flag("output").Value.String())

return nil
},
Expand All @@ -33,11 +45,16 @@ func NewPrintCmd() *cobra.Command {
return printCmd
}

func printEnv(values map[string]string, output string) error {
func printEnv(keys []string, output string) error {
if output == "" {
return errors.New("missing value for --output arg")
}

values := map[string]string{}
for _, key := range keys {
values[key] = os.Getenv(key)
}

if output == "json" {
jsonOutput, err := json.MarshalIndent(values, "", " ")
if err != nil {
Expand All @@ -58,8 +75,8 @@ func printEnv(values map[string]string, output string) error {
}

if outputTemplate != "" {
for key, value := range values {
fmt.Printf(outputTemplate, key, value)
for _, key := range keys {
fmt.Printf(outputTemplate, key, values[key])
}
return nil
}
Expand All @@ -70,7 +87,7 @@ func printEnv(values map[string]string, output string) error {
}

if outputTemplate != "" {
for key, _ := range values {
for _, key := range keys {
fmt.Printf(outputTemplate, key)
}
return nil
Expand Down
22 changes: 16 additions & 6 deletions internal/resolver/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (
)

func Load(env string) (*Resolver, error) {
err := runDotenv(env)
keys, err := runDotenv(env)
if err != nil {
return nil, err
}

r := &Resolver{
providers: map[string]providers.Provider{},
loadedProviders: map[providers.Provider]struct{}{},
LoadedVars: keys,
}

yamlBytes, err := os.ReadFile("./nv.yaml")
Expand Down Expand Up @@ -86,17 +87,26 @@ func yamlToJson(yamlBytes []byte) []byte {
return jsonBytes
}

func runDotenv(env string) error {
files := []string{".env", fmt.Sprintf(".env.%s", env)}
func runDotenv(env string) ([]string, error) {
files := []string{fmt.Sprintf(".env.%s", env), ".env"}

keys := []string{}
for _, f := range files {
switch err := godotenv.Load(f).(type) {
envMap, err := godotenv.Read(f)
switch err := err.(type) {
case nil:
case *fs.PathError:
default:
return fmt.Errorf("error: failed to load %s file: %s\n", f, err)
return nil, fmt.Errorf("error: failed to load %s file: %s\n", f, err)
}

for key, val := range envMap {
if _, exists := os.LookupEnv(key); !exists {
keys = append(keys, key)
os.Setenv(key, val)
}
}
}

return nil
return keys, nil
}
1 change: 1 addition & 0 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "github.com/maxnorth/nv/internal/providers"
type Resolver struct {
providers map[string]providers.Provider
loadedProviders map[providers.Provider]struct{}
LoadedVars []string
}
1 change: 1 addition & 0 deletions test/contexts/standard/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
EXAMPLE_VALUE=nv://echo/something/or/other
STATIC_VALUE=this did not use a resolver
6 changes: 5 additions & 1 deletion test/e2e/print.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ test:
cmd: nv print
out: |
EXAMPLE_VALUE=something/or/other
STATIC_VALUE=this did not use a resolver
- it: can print in yaml
with:
cmd: nv print -o yaml
out: |
EXAMPLE_VALUE: "something/or/other"
STATIC_VALUE: "this did not use a resolver"
- it: can print in json
with:
cmd: nv print -o json
out: |
{
"EXAMPLE_VALUE": "something/or/other"
"EXAMPLE_VALUE": "something/or/other",
"STATIC_VALUE": "this did not use a resolver"
}
- it: can print for shell eval
with:
cmd: nv print -o shell
out: |
export EXAMPLE_VALUE="something/or/other"
export STATIC_VALUE="this did not use a resolver"

0 comments on commit 25fb8b3

Please # to comment.