Skip to content

Commit

Permalink
chore: remove config responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvc committed Jul 16, 2023
1 parent 092ca36 commit 43d3d0b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
23 changes: 2 additions & 21 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package config

import (
"flag"
"io"
"os"

"github.com/charmbracelet/log"

intio "github.com/ivanvc/tpl/internal/io"
)

// Config represents the configuration options for the program.
type Config struct {
Input string
InputFile string
Env string
UseStdin bool
SetDebugLogLevel bool
Expand All @@ -27,21 +22,7 @@ func Load() *Config {
flag.BoolVar(&c.SetDebugLogLevel, "debug", false, "Set debug log level")
flag.Parse()

if len(c.Input) == 0 {
c.Input = string(intio.ReadFile(flag.Arg(0)))
}
if c.UseStdin {
b, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal("Could not read input template")
}
c.Input = string(b)
}

if c.SetDebugLogLevel {
log.Default().SetLevel(log.DebugLevel)
}
c.InputFile = flag.Arg(0)

log.Debug("Loaded configuration", "config", c)
return c
}
10 changes: 4 additions & 6 deletions internal/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package io
import (
"io"
"os"

"github.com/charmbracelet/log"
)

func ReadFile(file string) []byte {
func ReadFile(file string) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
log.Fatal("Error opening file", "file", file, "error", err)
return nil, err
}
b, err := io.ReadAll(f)
if err != nil {
log.Fatal("Error reading file", "file", file, "error", err)
return nil, err
}
return b
return b, nil
}
9 changes: 6 additions & 3 deletions internal/template/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ func loadEnvironment(cfg *config.Config) environment {
input := []byte(cfg.Env)

if cfg.Env[0] == '@' {
if _, err := os.Stat(cfg.Env[1:]); err == nil {
input = io.ReadFile(cfg.Env[1:])
f := cfg.Env[1:]
if _, err := os.Stat(f); err == nil {
if input, err = io.ReadFile(f); err != nil {
log.Warn("Error reading file", "error", err, "file", f)
}
} else {
log.Debug("Error opening file", "error", err, "file", cfg.Env[1:])
log.Debug("Error opening file", "error", err, "file", f)
}
}

Expand Down
33 changes: 32 additions & 1 deletion internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,54 @@ package template

import (
"html/template"
"io"
"os"

"github.com/Masterminds/sprig/v3"
"github.com/charmbracelet/log"

"github.com/ivanvc/tpl/internal/config"
intio "github.com/ivanvc/tpl/internal/io"
tpl "github.com/ivanvc/tpl/pkg/template"
)

// Runs the template to generate the output.
func Run(cfg *config.Config) {
input, err := loadInput(cfg)
if err != nil {
log.Fatal("Error loading input template", "error", err)
}
if len(input) == 0 {
log.Warn("Empty input template")
}
log.Debug("Loaded input template", "tpl", input)

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))
if err := tpl.Execute(os.Stdout, env); err != nil {
log.Fatal("Error executing template", "error", err)
}
}

func loadInput(cfg *config.Config) (string, error) {
var input string
if len(cfg.InputFile) > 0 {
b, err := intio.ReadFile(cfg.InputFile)
if err != nil {
return input, err
}
input = string(b)
} else if cfg.UseStdin {
b, err := io.ReadAll(os.Stdin)
if err != nil {
return input, err
}
input = string(b)
} else {
input = cfg.Input
}
return input, nil
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package main

import (
"github.com/charmbracelet/log"
"github.com/ivanvc/tpl/internal/config"
"github.com/ivanvc/tpl/internal/template"
)

func main() {
cfg := config.Load()
if cfg.SetDebugLogLevel {
log.Default().SetLevel(log.DebugLevel)
}
log.Debug("Loaded configuration", "config", cfg)

template.Run(cfg)
}

0 comments on commit 43d3d0b

Please sign in to comment.