forked from mattolenik/hclq
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from matthieudelaro/master
Exposed some functions
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
|
||
hclq "github.com/LudovicTOURMAN/hclq/cmd" | ||
) | ||
|
||
// SetTerraformAttribute Set attribute value | ||
func SetTerraformAttribute(tfParamPath, tfMainLocation, value string) (err error) { | ||
if _, err = os.Stat(tfMainLocation); os.IsNotExist(err) { | ||
return | ||
} | ||
|
||
cmd := hclq.RootCmd | ||
cmd.SetArgs([]string{ | ||
"-i", tfMainLocation, | ||
"-o", tfMainLocation, | ||
"set", tfParamPath, string(value), | ||
}) | ||
return cmd.Execute() | ||
} | ||
|
||
// GetTerraformAttribute Get attribute value | ||
func GetTerraformAttribute(tfParamPath, tfMainLocation string) (result string, err error) { | ||
if _, err := os.Stat(tfMainLocation); os.IsNotExist(err) { | ||
return "", err | ||
} | ||
|
||
cmd := hclq.RootCmd | ||
cmd.SetArgs([]string{ | ||
"-i", tfMainLocation, | ||
"get", tfParamPath, | ||
}) | ||
result, err = CaptureStdout(cmd.Execute) | ||
|
||
return | ||
} | ||
|
||
// CaptureStdout Record stdout and retrieve it as a string | ||
func CaptureStdout(f func() error) (stdout string, err error) { | ||
old := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
if err = f(); err != nil { | ||
return | ||
} | ||
|
||
w.Close() | ||
os.Stdout = old | ||
|
||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
stdout = buf.String() | ||
return | ||
} |