-
Notifications
You must be signed in to change notification settings - Fork 25
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
Showing
5 changed files
with
81 additions
and
11 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
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,3 +1,3 @@ | ||
module "github.com/ffuf/pencode" | ||
module github.com/ffuf/pencode | ||
|
||
go 1.14 |
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,3 +1,3 @@ | ||
package pencode | ||
|
||
const VERSION = "0.1" | ||
const VERSION = "0.2" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pencode | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
//isTemplate checks if the name ends with string ".tmpl" | ||
func isTemplate(name string) bool { | ||
return strings.HasSuffix(name, ".tmpl") | ||
} | ||
|
||
//hasTemplate checks if the name points to an existing file | ||
func hasTemplate(name string) (bool, error) { | ||
if _, err := os.Stat(name); err == nil { | ||
return true, nil | ||
} else { | ||
return false, err | ||
} | ||
} | ||
|
||
type Template struct { | ||
Data []byte | ||
Keyword string | ||
} | ||
|
||
func NewTemplateEncoder(name string) (Encoder, error) { | ||
var ok bool | ||
var err error | ||
t := Template{} | ||
// Using static keyword for now | ||
t.Keyword = "#PAYLOAD#" | ||
|
||
if ok, err = hasTemplate(name); ok { | ||
t.Data, err = ioutil.ReadFile(name) | ||
} | ||
return t, err | ||
} | ||
|
||
func (t Template) Encode(input []byte) ([]byte, error) { | ||
return []byte(strings.ReplaceAll(string(t.Data), t.Keyword, string(input))), nil | ||
} | ||
|
||
func (t Template) HelpText() string { | ||
return "Replaces string #PAYLOAD# in content of a file that has .tmpl extension." | ||
} |