Skip to content

Commit

Permalink
Merge pull request #9 from adam-huganir/feature/file-and-path-methods
Browse files Browse the repository at this point in the history
added some file and path methods, added ls example
  • Loading branch information
adam-huganir authored Jun 9, 2024
2 parents 13bd6ce + d8adf7e commit 81bcf08
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 14 deletions.
11 changes: 10 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ repos:
args: ["--fix", "lf"]


- repo: local
hooks:
- id: go-fmt
name: go format
language: system
entry: gofmt
types:
- go
args: ["-l", "-w"]

- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: go-fmt
- id: go-build
- id: go-mod-tidy
- repo: local
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ alternate form using matching
--data-match './talosPatches/.*\.yaml' \
<(echo "{{ . | toYaml }}")
```
### Listing files in a directory

For some reason you want to list the files in a directory and embed them in a file in a custom format:

```template
{{- $files := fileGlob "./*/*" -}}
{{- range $path := $files }}
{{- $stat := fileStat $path }}
{{- $username := (env "USERNAME" | default (env "USER") )}}
{{- $usernameFString := printf "%s%d%s " "%-" (len $username) "s"}}
{{ printf "%-12s" $stat.Mode }}{{ printf $usernameFString $username }}{{ pathAbsolute $path}}
{{- end }}
```
```
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\COMMIT_EDITMSG
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\FETCH_HEAD
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\HEAD
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\ORIG_HEAD
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\config
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\description
drwxrwxrwx adam C:\Users\adam\code\yet-unnamed-template-cli\.git\hooks
........
```
### Merging 2 data files and applying them to a template

```pwsh
Expand Down
9 changes: 4 additions & 5 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ tasks:
cmd: |-
go run ./cmd/yutc/yutc.go --help
run-test1:
run-tests:
desc: "Run X"
vars:
ARGS: ""
deps:
- "build"
run: always
dir: "./tests"
cmd: |-
go run ../cmd/yutc/yutc.go --shared ./testTemplates/def1.tmpl --data ./testFiles/data1.yaml ./testTemplates/template1.tmpl
go test ./cmd/yutc
go test ./internal
go test ./pkg
25 changes: 25 additions & 0 deletions docs/_data/README.data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ examples:
--data-match './talosPatches/.*\.yaml' \
<(echo "{{ . | toYaml }}")
```
- |-
### Listing files in a directory
For some reason you want to list the files in a directory and embed them in a file in a custom format:
```template
{{- $files := fileGlob "./*/*" -}}
{{- range $path := $files }}
{{- $stat := fileStat $path }}
{{- $username := (env "USERNAME" | default (env "USER") )}}
{{- $usernameFString := printf "%s%d%s " "%-" (len $username) "s"}}
{{ printf "%-12s" $stat.Mode }}{{ printf $usernameFString $username }}{{ pathAbsolute $path}}
{{- end }}
```
```
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\COMMIT_EDITMSG
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\FETCH_HEAD
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\HEAD
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\ORIG_HEAD
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\config
-rw-rw-rw- adam C:\Users\adam\code\yet-unnamed-template-cli\.git\description
drwxrwxrwx adam C:\Users\adam\code\yet-unnamed-template-cli\.git\hooks
........
```
- |-
### Merging 2 data files and applying them to a template
Expand Down
13 changes: 11 additions & 2 deletions internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ func BuildTemplate(text string, sharedTemplateBuffers []*bytes.Buffer, name stri
"mustToToml": yutc.MustToToml,
"mustFromToml": yutc.MustFromToml,
// "stringMap": yutc.stringMap,
"wrapText": yutc.WrapText,
"wrapComment": yutc.WrapComment,
"wrapText": yutc.WrapText,
"wrapComment": yutc.WrapComment,
"fileGlob": yutc.PathGlob,
"fileStat": yutc.PathStat,
"fileRead": yutc.FileRead,
"fileReadN": yutc.FileReadN,
"type": yutc.TypeOf,
"pathAbsolute": yutc.PathAbsolute,
"pathIsDir": yutc.PathIsDir,
"pathIsFile": yutc.PathIsFile,
"pathExists": yutc.PathExists,
})
for _, sharedTemplateBuffer := range sharedTemplateBuffers {
tmpl, err = tmpl.Parse(sharedTemplateBuffer.String())
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package internal

import "fmt"

var yutcVersion = "0.0.6"
var yutcVersion = "0.1.1"

func PrintVersion() {
fmt.Println(GetVersion())
Expand Down
112 changes: 107 additions & 5 deletions pkg/customFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/isbm/textwrap"
"github.com/pelletier/go-toml/v2"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -69,11 +71,6 @@ func FromToml(s string) interface{} {

}

func stringMap(v interface{}) (map[string]interface{}, error) {
// i don't feel like writing a recursive function right now
return nil, errors.New("not implemented")
}

// WrapText wraps text to a given width
func WrapText(width int, text string) []string {
wrapper := textwrap.NewTextWrap()
Expand All @@ -89,3 +86,108 @@ func WrapComment(prefix string, width int, comment string) string {
}
return strings.Join(wrapped, "\n")
}

func PathAbsolute(path string) string {
path = pathCommonClean(path)
path, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return path
}

func PathGlob(path string) []string {
path = pathCommonClean(path)
files, err := filepath.Glob(path)
if err != nil {
panic(err)
}
return files
}

func PathStat(path string) map[string]interface{} {
path = pathCommonClean(path)
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
panic(errors.Join(fmt.Errorf("file not found: %s", path)))
}
if os.IsPermission(err) {
panic(errors.Join(fmt.Errorf("permission denied: %s", path)))
}
panic(errors.Join(fmt.Errorf("unknown error %v: %s", err, path)))

}
return map[string]interface{}{
"Name": stat.Name(),
"Size": stat.Size(),
"Mode": stat.Mode().String(),
"ModTime": stat.ModTime(),
"IsDir": stat.IsDir(),
"Sys": stat.Sys(),
}
}

func pathCommonClean(path string) string {
return filepath.Clean(os.ExpandEnv(path))
}

func PathIsDir(path string) bool {
path = pathCommonClean(path)
stat, err := os.Stat(path)
if err != nil {
return false
}
return stat.IsDir()
}

func PathIsFile(path string) bool {
path = pathCommonClean(path)
stat, err := os.Stat(path)
if err != nil {
return false
}
return !stat.IsDir()
}

func PathExists(path string) bool {
path = pathCommonClean(path)
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

func FileRead(path string) string {
path = pathCommonClean(path)
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return ""
} else {
panic(fmt.Errorf("file not found: %s", path))
}
}
if info.IsDir() {
panic(fmt.Errorf("cannot read a directory: %s", path))
}
nBytes := int(info.Size())
return FileReadN(nBytes, path)
}

func FileReadN(nBytes int, path string) string {
path = pathCommonClean(path)
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
data := make([]byte, nBytes)
n, err := f.Read(data)
if err != nil {
panic(err)
}
return string(data[:n])
}

func TypeOf(v interface{}) string {
return fmt.Sprintf("%T", v)
}
7 changes: 7 additions & 0 deletions testFiles/ls-like.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{- $files := fileGlob "./*/*" -}}
{{- range $path := $files }}
{{- $stat := fileStat $path }}
{{- $username := (env "USERNAME" | default (env "USER") )}}
{{- $usernameFString := printf "%s%d%s " "%-" (len $username) "s"}}
{{ printf "%-12s" $stat.Mode }}{{ printf $usernameFString $username }}{{ pathAbsolute $path}}
{{- end }}

0 comments on commit 81bcf08

Please sign in to comment.