Skip to content

Commit

Permalink
feat: use single option for environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvc committed Jul 16, 2023
1 parent 5c3173b commit 092ca36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
19 changes: 11 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ import (

// Config represents the configuration options for the program.
type Config struct {
Input string
JSON string
TOML string
YAML string
UseStdin bool
Input string
Env string
UseStdin bool
SetDebugLogLevel bool
}

// Loads the configuration options.
func Load() *Config {
c := new(Config)
flag.StringVar(&c.Input, "input", "", "The template input to process.")
flag.StringVar(&c.JSON, "json", "", "The JSON environment for the template.")
flag.StringVar(&c.TOML, "toml", "", "The TOML environment for the template.")
flag.StringVar(&c.YAML, "yaml", "", "The YAML environment for the template.")
flag.StringVar(&c.Env, "env", "", "The environment for the template (YAML, JSON or TOML).")
flag.BoolVar(&c.UseStdin, "stdin", false, "Read template from stdin.")
flag.BoolVar(&c.SetDebugLogLevel, "debug", false, "Set debug log level")
flag.Parse()

if len(c.Input) == 0 {
Expand All @@ -40,5 +38,10 @@ func Load() *Config {
c.Input = string(b)
}

if c.SetDebugLogLevel {
log.Default().SetLevel(log.DebugLevel)
}

log.Debug("Loaded configuration", "config", c)
return c
}
49 changes: 19 additions & 30 deletions internal/template/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"encoding/json"
"os"

"github.com/BurntSushi/toml"
"github.com/charmbracelet/log"
Expand All @@ -14,41 +15,29 @@ import (
type environment map[string]any

func loadEnvironment(cfg *config.Config) environment {
var (
env environment
input []byte
)
if len(cfg.YAML) > 0 {
if cfg.YAML[0] == '@' {
input = io.ReadFile(cfg.YAML[1:])
} else {
input = []byte(cfg.YAML)
}
if err := yaml.Unmarshal(input, &env); err != nil {
log.Fatal("Error parsing YAML", "error", err)
}
}
var env environment
input := []byte(cfg.Env)

if len(cfg.JSON) > 0 {
if cfg.JSON[0] == '@' {
input = io.ReadFile(cfg.JSON[1:])
if cfg.Env[0] == '@' {
if _, err := os.Stat(cfg.Env[1:]); err == nil {
input = io.ReadFile(cfg.Env[1:])
} else {
input = []byte(cfg.JSON)
}
if err := json.Unmarshal(input, &env); err != nil {
log.Fatal("Error parsing JSON", "error", err)
log.Debug("Error opening file", "error", err, "file", cfg.Env[1:])
}
}

if len(cfg.TOML) > 0 {
if cfg.TOML[0] == '@' {
input = io.ReadFile(cfg.TOML[1:])
} else {
input = []byte(cfg.TOML)
}
if err := toml.Unmarshal(input, &env); err != nil {
log.Fatal("Error parsing TOML", "error", err)
}
if err := json.Unmarshal(input, &env); err == nil {
return env
} else {
log.Debug("Error parsing JSON", "error", err)
}
if err := yaml.Unmarshal(input, &env); err == nil {
return env
} else {
log.Debug("Error parsing YAML", "error", err)
}
if err := toml.Unmarshal(input, &env); err != nil {
log.Debug("Error parsing TOML", "error", err)
}

return env
Expand Down
1 change: 1 addition & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// Runs the template to generate the output.
func Run(cfg *config.Config) {
env := loadEnvironment(cfg)
log.Debug("Loaded environment", "env", env)
input := cfg.Input
t := template.New("input")
tpl := template.Must(t.Funcs(sprig.TxtFuncMap()).Funcs(tpl.IncludeFunc(t)).Parse(input))
Expand Down

0 comments on commit 092ca36

Please sign in to comment.