Skip to content

Commit

Permalink
automtically load template file if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
bjesus committed Sep 2, 2024
1 parent 241286a commit 18e1669
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/pipet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/bjesus/pipet/common"
"github.com/bjesus/pipet/internal/app"
"github.com/bjesus/pipet/outputs"
"github.com/bjesus/pipet/utils"
)

func main() {
Expand Down Expand Up @@ -93,6 +95,13 @@ func runPipet(c *cli.Context, specFile string) error {
log.SetOutput(io.Discard)
}

automaticTemplateFile := strings.TrimSuffix(specFile, filepath.Ext(specFile)) + ".tpl"

if templateFile == "" && utils.FileExists(automaticTemplateFile) {
log.Println("Detected template file at", specFile)
templateFile = automaticTemplateFile
}

pipet := &common.PipetApp{
MaxPages: maxPages,
Separator: separators,
Expand Down
6 changes: 6 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"os"

"github.com/bjesus/pipet/common"
)
Expand Down Expand Up @@ -31,3 +32,8 @@ func GetSeparator(app *common.PipetApp, depth int) string {
}
return ", "
}

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

0 comments on commit 18e1669

Please sign in to comment.