Skip to content

Commit

Permalink
Update to support stdin.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Oct 2, 2019
1 parent 7c71e80 commit 8e57e3a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.12.6
- 1.13

before_install:
- sudo apt-get install -y upx
Expand All @@ -12,6 +12,7 @@ install:
env:
global:
- GO111MODULE=on
- GOFLAGS="-mod=vendor"

script:
- go vet
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.1 // indirect
)

go 1.13
37 changes: 36 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type conf struct {
environmentFile string
templateFile string
rawTemplate string
stdin bool
customDelimiters string
strictMode bool

Expand Down Expand Up @@ -44,6 +45,7 @@ func main() {
root.Flags().StringVarP(&configs.templateFile, "file", "f", "", "the template file to process (required)")
root.Flags().StringVarP(&configs.customDelimiters, "delimiter", "d", "", `delimiter (default "{{}}")`)
root.Flags().StringVarP(&configs.rawTemplate, "execute", "x", "", "a raw template to execute directly, without providing --file")
root.Flags().BoolVarP(&configs.stdin, "stdin", "i", false, "a stdin input to execute directly, without providing --file or --execute")
root.Flags().BoolVarP(&configs.strictMode, "strict", "s", false, "enables strict mode: if an environment variable in the file is defined but not set, it'll fail")

if err := root.Execute(); err != nil {
Expand All @@ -66,6 +68,14 @@ func command(w io.Writer, c conf) error {
var b *bytes.Buffer

if c.templateFile != "" {
if c.rawTemplate != "" {
return errors.New("defined both --file and --raw, only one must be used")
}

if c.stdin {
return errors.New("defined both --file and --stdin, only one must be used")
}

bt, err := loadFile(c.templateFile)
if err != nil {
return err
Expand All @@ -75,11 +85,36 @@ func command(w io.Writer, c conf) error {
}

if c.rawTemplate != "" {
if c.templateFile != "" {
return errors.New("defined both --raw and --file, only one must be used")
}

if c.stdin {
return errors.New("defined both --raw and --stdin, only one must be used")
}

b = bytes.NewBufferString(c.rawTemplate)
}

if c.stdin {
if c.templateFile != "" {
return errors.New("defined both --stdin and --file, only one must be used")
}

if c.rawTemplate != "" {
return errors.New("defined both --stdin and --raw, only one must be used")
}

bt, err := loadFile(os.Stdin.Name())
if err != nil {
return err
}

b = bt
}

if b == nil {
return errors.New("needs to specify either a template file (using --file) or a raw template (using --raw)")
return errors.New("needs to specify either a template file (using --file) or a raw template (using --raw or --stdin)")
}

if err := loadVirtualEnv(c.environmentFile); err != nil {
Expand Down

0 comments on commit 8e57e3a

Please sign in to comment.