Skip to content

Commit

Permalink
much cleaner, still wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-huganir committed Jul 19, 2024
1 parent ffa1042 commit 75e5d13
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 71 deletions.
50 changes: 50 additions & 0 deletions cmd/yutc/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"bytes"
"fmt"
"github.com/adam-huganir/yutc/internal"
"github.com/pkg/errors"
"io"
"path/filepath"
)

func createWriter(path, outputPath string, overwrite bool) (io.Writer, error) {
outPath := filepath.Join(outputPath, path)
outDir := filepath.Dir(outPath)
exists, err := internal.Exists(outDir)
// we may have a file in the place where our output folder should be, we respect overwrite if there is
outDirIsDir, err := internal.IsDir(outDir)
if !exists && err == nil || !outDirIsDir {
if !overwrite {
return nil, fmt.Errorf("file found where output requires a folder, %s, you must use overwrite to delete existing file(s)", outDir)
}
err = internal.Fs.Remove(outDir)
err = internal.Fs.MkdirAll(outDir, 0755)
if err != nil {
return nil, err
}
}
outWriter, err := internal.Fs.Create(outPath)
if err != nil {
return nil, err
}
return outWriter, nil
}

func evalTemplate(t *internal.YutcTemplate, commonTemplates []*internal.FileData, data any, outWriter io.Writer) (*bytes.Buffer, error) {
var err error
for _, ct := range commonTemplates {
err = t.AddTemplate(ct.ReadWriter.String())
if err != nil {
return nil, errors.Wrapf(err, "error adding common template %s to %s", ct.Path, t.Path())
}
}
result, err := t.Execute(data)
if err != nil {
return nil, errors.Wrapf(err, "error executing template %s (%s)", t.Path(), t.ID())

}
_, err = outWriter.Write(result.Bytes())
return result, nil
}
98 changes: 73 additions & 25 deletions cmd/yutc/forEach.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
package main

import (
"github.com/adam-huganir/yutc/internal"
"github.com/spf13/cobra"
"io"
"os"
"path"
"slices"
)

func runForEachCommand(cmd *cobra.Command, args []string) error {
//YutcLog.Trace().Msg("yutc.runForEachCommand() called")
//templateFiles, dataFiles := parseInputs(args)
//err := parseArgs(templateFiles, dataFiles)
//if len(dataFiles) == 1 {
// // we assume that if the datafile is a list, we will iterate over it
// data, dataType, err := internal.GatherData(dataFiles, runSettings.Append)
// if err != nil {
// return err
// }
// fmt.Println(data, dataType)
//} else {
// // we assume that each datafile is a separate iteration, and in this case we don't care what each individual
// // file contains
// for _, dataFile := range dataFiles {
// data, dataType, err := internal.GatherData([]string{dataFile}, runSettings.Append)
// if err != nil {
// return err
// }
// fmt.Println(data, dataType)
// }
//}
//if err != nil {
// return err
//}
func runForEachCommand(cmd *cobra.Command, args []string) (err error) {
YutcLog.Trace().Msg("yutc.runForEachCommand() called")

runSettings.TemplatePaths = args

err = parseArgs(runSettings)
if err != nil {
return err
}

var datas []any
if len(runSettings.DataFiles) == 1 {
data, _, err := internal.GatherData(runSettings.DataFiles, false, runSettings.BasicAuth, runSettings.BearerToken)
if err != nil {
return err
}
datas = data.([]any)
} else {
for _, dataFile := range runSettings.DataFiles {
data, _, err := internal.GatherData([]string{dataFile}, false, runSettings.BasicAuth, runSettings.BearerToken)
if err != nil {
return err
}
datas = append(datas, data)
}
}

var commonTemplates []*internal.FileData
commonTemplates, err = internal.LoadFiles(runSettings.CommonTemplateFiles, runSettings.BasicAuth, runSettings.BearerToken)
if err != nil {
return err
}

var templates []*internal.YutcTemplate
templates, err = internal.LoadTemplates(runSettings.TemplatePaths, runSettings.BasicAuth, runSettings.BearerToken)
if err != nil {
return err
}

slices.SortFunc(templates, internal.CmpTemplatePathLength) // sort templates by their file path length (shortest first)
templateRootDir := internal.NormalizeFilepath(path.Dir(templates[0].Path())) // because of above we know this is the root dir

// see if we need to dive into these and pull files out of them

var outWriter io.Writer

// Load up our output templates with any common definitions from the shared templates
first := true
for _, t := range templates {
t.SetRelativePath(templateRootDir)
for _, data := range datas {
if runSettings.Output == "-" {
outWriter = os.Stdout
if !first {
_, _ = outWriter.Write([]byte("---\n"))
} else {
first = false
}
} else {
outWriter, err = createWriter(t.RelativePath(), runSettings.Output, runSettings.Overwrite)
if err != nil {
return err
}
}
_, err = evalTemplate(t, commonTemplates, data, outWriter)
}
}

return nil
}
10 changes: 1 addition & 9 deletions cmd/yutc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ func initRoot(rootCommand *cobra.Command, settings *internal.YutcSettings) {
func initCommon(cmd *cobra.Command, settings *internal.YutcSettings) {
YutcLog.Trace().Msg("yutc.initCommon() called")
cmd.Flags().SortFlags = false

cmd.PersistentFlags().BoolVarP(
&settings.Verbose,
"verbose",
"v",
false,
"Verbose output",
)
cmd.Flags().BoolVar(&settings.Version, "version", false, "Print the version and exit")
cmd.Flags().StringArrayVarP(
&settings.DataFiles,
"data",
Expand Down Expand Up @@ -105,5 +96,6 @@ func initCli(settings ...*internal.YutcSettings) *cobra.Command {
}
initRoot(rootCommand, runSettings)
initCommon(templateCommand, runSettings)
initCommon(forEachCommand, runSettings)
return rootCommand
}
1 change: 0 additions & 1 deletion cmd/yutc/refactor.go

This file was deleted.

7 changes: 5 additions & 2 deletions cmd/yutc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/adam-huganir/yutc/internal"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -94,6 +95,9 @@ func A(templateFiles, dataFiles []string) {
if err == nil && inputIsRecursive {
resolveRoot = firstTemplatePath
}
var templates []*internal.YutcTemplate
var commonTemplates []*bytes.Buffer
var data []internal.FileData
for templateIndex, tmpl := range templates {
templateOriginalPath := templateFiles[templateIndex] // as the user provided

Expand Down Expand Up @@ -134,7 +138,7 @@ func A(templateFiles, dataFiles []string) {
}
var outData *bytes.Buffer
outData = new(bytes.Buffer)
err = tmpl.Execute(outData, data)
outData, err = tmpl.Execute(data)
if err != nil {
YutcLog.Panic().Msg(err.Error())
}
Expand Down Expand Up @@ -187,7 +191,6 @@ func A(templateFiles, dataFiles []string) {
}
}
}
return err
}

func parseInputs(args []string) ([]string, []string) {
Expand Down
37 changes: 6 additions & 31 deletions cmd/yutc/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package main

import (
"github.com/adam-huganir/yutc/internal"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"io"
"os"
"path/filepath"
"path"
"slices"
)

Expand Down Expand Up @@ -36,49 +35,25 @@ func runTemplateCommand(cmd *cobra.Command, args []string) (err error) {
return err
}

slices.SortFunc(templates, internal.CmpTemplatePathLength) // sort templates by their file path length (shortest first)
templateRootPath := internal.NormalizeFilepath(templates[0].Path())
slices.SortFunc(templates, internal.CmpTemplatePathLength) // sort templates by their file path length (shortest first)
templateRootDir := internal.NormalizeFilepath(path.Dir(templates[0].Path())) // because of above we know this is the root dir

// see if we need to dive into these and pull files out of them

var outWriter io.Writer

// Load up our output templates with any common definitions from the shared templates
for _, t := range templates {
t.SetRelativePath(templateRootPath)
t.SetRelativePath(templateRootDir)
if runSettings.Output == "-" {
outWriter = os.Stdout
} else {
outPath := filepath.Join(runSettings.Output, t.RelativePath())
outDir := filepath.Dir(outPath)
exists, err := internal.Exists(outDir)
if !exists && err == nil {
err = internal.Fs.Mkdir(outDir, 0755)
if err != nil {
return err
}
}
outWriter, err = internal.Fs.Create(outPath)
if err != nil {
return err
}
}

for _, ct := range commonTemplates {
err = t.AddTemplate(ct.ReadWriter.String())
outWriter, err = createWriter(t.RelativePath(), runSettings.Output, runSettings.Overwrite)
if err != nil {
return err
}
}
result, err := t.Execute(data)
if err != nil {
return errors.Wrapf(err, "error executing template %s", t.ID())
}
err = nil
for err == nil {
_, err = outWriter.Write(result.Bytes())
}
println("---")
_, err = evalTemplate(t, commonTemplates, data, outWriter)
}

return nil
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/adam-huganir/yutc
go 1.22

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/google/uuid v1.4.0
github.com/imdario/mergo v0.3.11
github.com/isbm/textwrap v0.0.0-20190729202254-22edad10bd84
Expand All @@ -16,13 +17,21 @@ require (
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gotest.tools v2.2.0+incompatible // indirect
Expand Down
Loading

0 comments on commit 75e5d13

Please sign in to comment.