Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
- Functions accessible in templates are now stored in a separate files
- Functions are documented in Go
  • Loading branch information
VOID404 committed Sep 11, 2024
1 parent 2b097aa commit 75d39d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
25 changes: 25 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "os"

var funcMap map[string]any

func init() {
funcMap = make(map[string]any)
funcMap["file"] = File
funcMap["env"] = Env
}

// File reads a file and returns its content as a string
func File(name string) string {
data, err := os.ReadFile(name)
if err != nil {
panic(err)
}
return string(data)
}

// Env expands environment variables in a string
func Env(text string) string {
return os.ExpandEnv(text)
}
13 changes: 0 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ func main() {
panic(err)
}

funcMap := make(map[string]any)
funcMap["file"] = func(name string) string {
data, err := os.ReadFile(name)
if err != nil {
panic(err)
}
return string(data)
}

funcMap["env"] = func(text string) string {
return os.ExpandEnv(text)
}

tpl := template.Must(
template.New(path.Base(tplFile)). //
Funcs(funcMap).
Expand Down

0 comments on commit 75d39d8

Please sign in to comment.