Skip to content

Commit

Permalink
Add read function (#274)
Browse files Browse the repository at this point in the history
- Added the read function and make it able to read both binary or text file
- Modify the save function to be able to save binary files
- Fix a problem with help function (title was printed twice)

- Rename the master branch to main
  • Loading branch information
jocgir authored Nov 14, 2024
1 parent 56da36f commit 6e066dc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
18 changes: 7 additions & 11 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@
"criticalf",
"cutset",
"debugf",
"Debugf",
"defval",
"diffmatchpatch",
"divf",
"dolore",
"DONT",
"dont",
"doublestar",
"drhodes",
"eiusmod",
"elit",
"endexpr",
"Envar",
"Envars",
"envar",
"envars",
"erfc",
"expm",
"extensionfiles",
Expand All @@ -61,15 +60,14 @@
"hexa",
"htpasswd",
"hypot",
"IHCL",
"ihcl",
"ilogb",
"incididunt",
"infof",
"Infof",
"Infoln",
"infoln",
"interpretated",
"kebabcase",
"Kenobi",
"kenobi",
"keymap",
"keypair",
"kindis",
Expand Down Expand Up @@ -116,7 +114,6 @@
"semicolumn",
"sergi",
"signbit",
"simplifiable",
"sincos",
"sindent",
"sirupsen",
Expand All @@ -134,11 +131,10 @@
"substr",
"swapcase",
"tempfile",
"templating",
"tempor",
"tfvars",
"TFVARS",
"tracef",
"Tracef",
"traiecta",
"trimall",
"tsed",
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build
on:
push:
branches:
- master
- main
tags:
- "*"
jobs:
Expand All @@ -23,7 +23,7 @@ jobs:
- run: make test-all

- name: Run GoReleaser
if: github.ref != 'refs/heads/master'
if: github.ref != 'refs/heads/main'
uses: goreleaser/goreleaser-action@7ec5c2b0c6cdda6e8bbb49444bc797dd33d74dd8
with:
distribution: goreleaser
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_doc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Deploy documentation site

on:
push:
branches: ["master"]
branches: ["main"]

workflow_dispatch:

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [ "master" ]
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
branches: [ "main" ]
schedule:
- cron: '19 8 * * 0'

Expand Down
2 changes: 1 addition & 1 deletion collections/convert_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var TypeConverters = make(map[string]func([]byte, interface{}) error)
func ConvertData(data string, out interface{}) (err error) {
trySimplified := func() error {
if strings.Count(data, "=") == 0 {
return fmt.Errorf("not simplifiable")
return fmt.Errorf("not reducible")
}
// Special case where we want to have a map and the supplied string is simplified such as "a = 10 b = string"
// so we try transform the supplied string in valid YAML
Expand Down
45 changes: 42 additions & 3 deletions template/extra_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var osFuncs = dictionary{
"isWriteable": isWriteable,
"joinPath": path.Join,
"lastMod": lastMod,
"load": loadFromFile,
"lookPath": lookPath,
"mode": fileMode,
"pwd": utils.Pwd,
Expand All @@ -49,6 +50,7 @@ var osFuncsArgs = arguments{
"isReadable": {"filename"},
"isWriteable": {"filename"},
"lastMod": {"filename"},
"load": {"filename", "binary"},
"mode": {"filename"},
"save": {"filename", "object"},
"size": {"filename"},
Expand All @@ -62,10 +64,11 @@ var osFuncsAliases = aliases{
"home": {"homeDir", "homeFolder"},
"isDir": {"isDirectory", "isFolder"},
"lastMod": {"lastModification", "lastModificationTime"},
"save": {"write", "writeTo"},
"lookPath": {"whereIs", "look", "which", "type"},
"mode": {"fileMode"},
"pwd": {"currentDir"},
"save": {"write", "writeTo"},
"load": {"read", "readFrom"},
"size": {"fileSize"},
"stat": {"fileStat"},
"user": {"currentUser"},
Expand All @@ -84,10 +87,11 @@ var osFuncsHelp = descriptions{
"isWriteable": "Determines if the file is writeable by the current user.",
"joinPath": "Joins any number of path elements into a single path, adding a separating slash if necessary. The result is Cleaned; in particular all empty strings are ignored.",
"lastMod": "Returns the last modification time of the file.",
"load": "Read object from file. If binary is true, the object is returned as a byte array.",
"lookPath": "Returns the location of the specified executable (returns empty string if not found).",
"mode": "Returns the file mode.",
"pwd": "Returns the current working directory.",
"save": "Save object to file.",
"save": "Save object to file. If the object is not an array of byte, then the object is converted to a string.",
"size": "Returns the file size.",
"stat": "Returns the file Stat information (os.Stat object).",
"user": "Returns the current user information (user.User object).",
Expand Down Expand Up @@ -186,14 +190,49 @@ func diff(text1, text2 interface{}) interface{} {
return dmp.DiffPrettyText(diffs)
}

func loadFromFile(filename string, binary ...bool) (interface{}, error) {
isBinary := false
switch len(binary) {
case 0:
break
case 1:
isBinary = binary[0]
default:
return "", fmt.Errorf("invalid number of arguments")
}
content, err := os.ReadFile(filename)
if isBinary {
return content, err
}
return string(content), err
}

func saveToFile(filename string, object interface{}) (string, error) {
folder := path.Dir(filename)
if _, err := os.Stat(folder); os.IsNotExist(err) {
if err = os.Mkdir(folder, 0777); err != nil {
return "", err
}
}
return "", os.WriteFile(filename, []byte(fmt.Sprint(object)), 0644)
if list, err := collections.TryAsList(object); err != nil {
// If object is not a []byte, convert it to a string
object = []byte(fmt.Sprint(object))
} else {
// Convert each element of the list to byte
byteArray := make([]byte, len(list.AsArray()))
for i, v := range list.AsArray() {
byteValue, ok := v.(byte)
if !ok {
// Not all elements are byte, convert the whole list to a string
byteArray = []byte(fmt.Sprint(object))
break
}
byteArray[i] = byteValue
}
object = byteArray
}

return "", os.WriteFile(filename, object.([]byte), 0644)
}

func username() string {
Expand Down
1 change: 0 additions & 1 deletion template/template_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func (t *Template) printFunctionsDetailed(functions []string, maxLength int, ali
for i := range functions {
fi := t.functions[functions[i]]
if fi.description != "" {
fmt.Println(fi.description)
text := String(fi.description).Wrap(100).Indent("// ").Lines().TrimSuffix(" ").Join("\n").String()
Println(color.GreenString(text))
}
Expand Down

0 comments on commit 6e066dc

Please sign in to comment.