Skip to content
Snippets Groups Projects
Commit c883fbdcdd4d authored by steeve.chailloux's avatar steeve.chailloux
Browse files

can now print output to stdout using: -o stdout

parent 32cece3f2c91
No related branches found
No related tags found
No related merge requests found
......@@ -13,8 +13,8 @@
type BuildCmd struct {
Args struct {
DryRun bool `short:"d" long:"dry-run" description:"if set only prints commands but do not run them"`
Keep bool `short:"k" long:"keep" descriptions:"Keep the temporary files"`
Output string `short:"o" long:"output" descriptions:"output directory"`
Keep bool `short:"k" long:"keep" description:"Keep the temporary files"`
Output string `short:"o" long:"output" description:"output directory, use \"stdout\" to print to stdout"`
}
PositionalArgs struct {
DirName string `required:"yes" positional-arg-name:"directory"`
......
......@@ -54,11 +54,13 @@
if err != nil {
return fmt.Errorf("cannot list directory: %s - %w", preBuildDir, err)
}
if err := CleanDir(outputDir); err != nil {
return fmt.Errorf("cannot clean dir: %s: %w", outputDir, err)
if outputDir != "stdout" {
if err := CleanDir(outputDir); err != nil {
return fmt.Errorf("cannot clean dir: %s: %w", outputDir, err)
}
}
variables, err := r.config.prepareVariables(true)
if err != nil {
return fmt.Errorf("cannot prepare variables: %w", err)
}
for _, file := range files {
......@@ -59,7 +61,9 @@
}
variables, err := r.config.prepareVariables(true)
if err != nil {
return fmt.Errorf("cannot prepare variables: %w", err)
}
for _, file := range files {
var outFilePath string
var outFile *os.File
inFilePath := filepath.Join(preBuildDir, file.Name())
......@@ -65,6 +69,16 @@
inFilePath := filepath.Join(preBuildDir, file.Name())
outFilePath := filepath.Join(outputDir, file.Name())
outFile, err := os.Create(outFilePath)
if err != nil {
return fmt.Errorf("cannot open: %s - %w", outFilePath, err)
if outputDir == "stdout" {
outFilePath = "stdout"
outFile = os.Stdout
} else {
outFilePath = filepath.Join(outputDir, file.Name())
outFile, err = os.Create(outFilePath)
if err != nil {
return fmt.Errorf("cannot open: %s - %w", outFilePath, err)
}
defer func() {
if err := outFile.Close(); err != nil {
r.config.Logger.Fatal().Err(err).Msg("cannot close hydrated file")
}
}()
}
......@@ -70,9 +84,4 @@
}
defer func() {
if err := outFile.Close(); err != nil {
r.config.Logger.Fatal().Err(err).Msg("cannot close hydrated file")
}
}()
if err := hydrate(inFilePath, outFile, variables); err != nil {
return fmt.Errorf("cannot hydrate: %s - %w", outFilePath, err)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment