-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffa1042
commit 75e5d13
Showing
12 changed files
with
230 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.