Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cenv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cenv

import "github.com/echo-webkom/cenv/cenv"
import "github.com/echo-webkom/cenv/internal"

const (
defaultEnvPath = ".env"
Expand All @@ -14,9 +14,14 @@ const (
// The default path for both .env and cenv.schema.json are used, which are
// both in the project root directory. To specify path use LoadEx()
func Load() error {
return cenv.LoadAndCheck(defaultEnvPath, defaultSchemaPath)
return internal.LoadAndCheck(defaultEnvPath, defaultSchemaPath, false)
}

func LoadEx(envPath string, schemaPath string) error {
return cenv.LoadAndCheck(envPath, schemaPath)
return internal.LoadAndCheck(envPath, schemaPath, false)
}

// Verify that the values in the loaded environment mathces the schema.
func Verify() error {
return internal.LoadAndCheck(defaultEnvPath, defaultSchemaPath, true)
}
37 changes: 31 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"os/exec"

"github.com/echo-webkom/cenv/cenv"
"github.com/echo-webkom/cenv/internal"
"github.com/fatih/color"
)

Expand All @@ -15,8 +15,12 @@ func showHelp() {
fmt.Println("Commands:")
fmt.Println(" check Check if .env matches schema")
fmt.Println(" update Generate schema based on .env")
fmt.Println(" fix Automatically fix issues with the .env")
fmt.Println(" Tries to reuse previous env values")
fmt.Println(" fix Fix will")
fmt.Println(" create .env if one does not exist")
fmt.Println(" fill in default/public values for empty fields")
fmt.Println(" automatically fix issues with the .env if any")
fmt.Println(" always reuse values already in the .env")
fmt.Println(" tags Show list of available tags and their uses")
fmt.Println(" help Show this help message")
fmt.Println(" version Show version")
fmt.Println(" upgrade Upgrade to latest version")
Expand All @@ -29,6 +33,22 @@ func showHelp() {
fmt.Println()
}

func showTags() {
fmt.Println("List of tags")
fmt.Println()
fmt.Println(" required Field has to have a non-empty value")
fmt.Println(" public Field is static and shown in schema")
fmt.Println(" default <value> Field will be set to given default value if empty")
fmt.Println(" length <n> Field must have given length")
fmt.Println(" format <fmt> Field must have given format (gokenizer pattern)")
fmt.Println(" See github.com/jesperkha/gokenizer")
fmt.Println(" enum <values> Field must be one of the given values")
fmt.Println(" Values are separated by a |")
fmt.Println(" Example: @enum user | admin | guest")
fmt.Println()

}

func errorExitS(message string) {
color.RGB(237, 93, 83).Println(fmt.Sprintf("cenv: %s", message))
os.Exit(1)
Expand Down Expand Up @@ -78,6 +98,11 @@ func Run() {
return
}

if command == "tags" {
showTags()
return
}

if command == "version" || command == "--version" || command == "-v" {
if Version == "" {
fmt.Println("you are running a development version")
Expand All @@ -101,21 +126,21 @@ func Run() {
}

if command == "fix" {
if err := cenv.Fix(envPath, schemaPath); err != nil {
if err := internal.Fix(envPath, schemaPath); err != nil {
errorExit(err)
}
return
}

if command == "check" {
if err := cenv.Check(envPath, schemaPath); err != nil {
if err := internal.Check(envPath, schemaPath); err != nil {
errorExit(err)
}
return
}

if command == "update" {
if err := cenv.Update(envPath, schemaPath); err != nil {
if err := internal.Update(envPath, schemaPath); err != nil {
errorExit(err)
}
return
Expand Down
2 changes: 1 addition & 1 deletion cenv/cenv.go → internal/cenv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cenv
package internal

import (
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions cenv/cenv_test.go → internal/cenv_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cenv_test
package internal_test

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/echo-webkom/cenv/cenv"
"github.com/echo-webkom/cenv/internal"
)

func TestUpdate(t *testing.T) {
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestUpdate(t *testing.T) {
t.Fatalf("failed to write cenv.schema.json file: %v", err)
}

err := cenv.Update(envPath, schemaPath)
err := internal.Update(envPath, schemaPath)
if (err != nil) && !test.expectError {
t.Fatalf("Update failed: %v", err)
}
Expand Down Expand Up @@ -103,7 +103,7 @@ FOO=bar`,
t.Fatalf("failed to write cenv.schema.json file: %v", err)
}

err := cenv.Check(envPath, schemaPath)
err := internal.Check(envPath, schemaPath)
if (err != nil) && !test.expectError {
t.Fatalf("Check failed: %v", err)
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestFix(t *testing.T) {
t.Fatalf("failed to write cenv.schema.json file: %v", err)
}

if err := cenv.Fix(envPath, schemaPath); err != nil {
if err := internal.Fix(envPath, schemaPath); err != nil {
t.Fatalf("Fix failed: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cenv/error.go → internal/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cenv
package internal

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion cenv/parser.go → internal/parser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cenv
package internal

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cenv/parser_test.go → internal/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cenv
package internal

import (
"os"
Expand Down
6 changes: 3 additions & 3 deletions cenv/pkg.go → internal/pkg.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package cenv
package internal

import (
"os"

"github.com/joho/godotenv"
)

func LoadAndCheck(envPath, schemaPath string) error {
func LoadAndCheck(envPath, schemaPath string, ignoreEnv bool) error {
env, err := ReadSchema(schemaPath)
if err != nil {
return err
}

if err := godotenv.Load(envPath); err != nil {
if err := godotenv.Load(envPath); !ignoreEnv && err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cenv/schema.go → internal/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cenv
package internal

import (
"encoding/json"
Expand Down