Skip to content

Commit

Permalink
Merge pull request #52 from grisu48/macros
Browse files Browse the repository at this point in the history
Macros
  • Loading branch information
grisu48 authored Apr 5, 2023
2 parents c5727a3 + cabf144 commit 9149009
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/pastad/pastad.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@ func main() {
if cf.PastaDir == "" {
cf.PastaDir = "."
}

// Preparation steps
baseURL, err := ApplyMacros(cf.BaseUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "error applying macros: %s", err)
os.Exit(1)
}
cf.BaseUrl = baseURL
bowl.Directory = cf.PastaDir
os.Mkdir(bowl.Directory, os.ModePerm)

Expand Down
14 changes: 14 additions & 0 deletions cmd/pastad/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,17 @@ func timeHumanReadable(timestamp int64) string {
return fmt.Sprintf("%d days", days)
}
}

/* Apply custom macros in the given string and return the result. The following macros are supports:
* `$hostname` - Replace with the system hostname
*/
func ApplyMacros(txt string) (string, error) {
if strings.Contains(txt, "$hostname") {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
txt = strings.ReplaceAll(txt, "$hostname", hostname)
}
return txt, nil
}

0 comments on commit 9149009

Please sign in to comment.