0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-24 13:20:39 +00:00
wp-opentofu/plugin/tofu.go

182 lines
3.8 KiB
Go
Raw Normal View History

2024-02-05 19:44:47 +00:00
package plugin
import (
"fmt"
"golang.org/x/sys/execabs"
)
const (
tofuBin = "/usr/local/bin/tofu"
)
type pluginCommand struct {
cmd *execabs.Cmd
private bool
}
2024-02-05 19:44:47 +00:00
func (p *Plugin) versionCommand() *pluginCommand {
return &pluginCommand{
execabs.Command(tofuBin, "version"),
false,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) initCommand() *pluginCommand {
2024-02-05 19:44:47 +00:00
args := []string{
"init",
}
for _, v := range p.Settings.InitOptions.BackendConfig {
args = append(args, fmt.Sprintf("-backend-config=%s", v))
}
// Fail tofu execution on prompt
args = append(args, "-input=false")
return &pluginCommand{
execabs.Command(tofuBin, args...),
false,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) getModulesCommand() *pluginCommand {
return &pluginCommand{
execabs.Command(tofuBin, "get"),
false,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) validateCommand() *pluginCommand {
return &pluginCommand{
execabs.Command(tofuBin, "validate"),
false,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) fmtCommand() *pluginCommand {
2024-02-05 19:44:47 +00:00
args := []string{
"fmt",
}
if p.Settings.FmtOptions.List != nil {
args = append(args, fmt.Sprintf("-list=%t", *p.Settings.FmtOptions.List))
}
if p.Settings.FmtOptions.Write != nil {
args = append(args, fmt.Sprintf("-write=%t", *p.Settings.FmtOptions.Write))
}
if p.Settings.FmtOptions.Diff != nil {
args = append(args, fmt.Sprintf("-diff=%t", *p.Settings.FmtOptions.Diff))
}
if p.Settings.FmtOptions.Check != nil {
args = append(args, fmt.Sprintf("-check=%t", *p.Settings.FmtOptions.Check))
}
return &pluginCommand{
execabs.Command(tofuBin, args...),
false,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) planCommand(destroy bool) *pluginCommand {
2024-02-05 19:44:47 +00:00
args := []string{
"plan",
}
if destroy {
args = append(args, "-destroy")
} else {
args = append(args, fmt.Sprintf("-out=%s", p.Settings.OutFile))
}
for _, value := range p.Settings.Targets.Value() {
args = append(args, "--target", value)
}
if p.Settings.Parallelism > 0 {
args = append(args, fmt.Sprintf("-parallelism=%d", p.Settings.Parallelism))
}
if p.Settings.InitOptions.Lock != nil {
args = append(args, fmt.Sprintf("-lock=%t", *p.Settings.InitOptions.Lock))
}
if p.Settings.InitOptions.LockTimeout != "" {
args = append(args, fmt.Sprintf("-lock-timeout=%s", p.Settings.InitOptions.LockTimeout))
}
if !p.Settings.Refresh {
args = append(args, "-refresh=false")
}
return &pluginCommand{
execabs.Command(tofuBin, args...),
p.Settings.NoLog,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) applyCommand() *pluginCommand {
2024-02-05 19:44:47 +00:00
args := []string{
"apply",
}
for _, v := range p.Settings.Targets.Value() {
args = append(args, "--target", v)
}
if p.Settings.Parallelism > 0 {
args = append(args, fmt.Sprintf("-parallelism=%d", p.Settings.Parallelism))
}
if p.Settings.InitOptions.Lock != nil {
args = append(args, fmt.Sprintf("-lock=%t", *p.Settings.InitOptions.Lock))
}
if p.Settings.InitOptions.LockTimeout != "" {
args = append(args, fmt.Sprintf("-lock-timeout=%s", p.Settings.InitOptions.LockTimeout))
}
if !p.Settings.Refresh {
args = append(args, "-refresh=false")
}
args = append(args, p.Settings.OutFile)
return &pluginCommand{
execabs.Command(tofuBin, args...),
p.Settings.NoLog,
}
2024-02-05 19:44:47 +00:00
}
func (p *Plugin) destroyCommand() *pluginCommand {
2024-02-05 19:44:47 +00:00
args := []string{
"destroy",
}
for _, v := range p.Settings.Targets.Value() {
args = append(args, fmt.Sprintf("-target=%s", v))
}
if p.Settings.Parallelism > 0 {
args = append(args, fmt.Sprintf("-parallelism=%d", p.Settings.Parallelism))
}
if p.Settings.InitOptions.Lock != nil {
args = append(args, fmt.Sprintf("-lock=%t", *p.Settings.InitOptions.Lock))
}
if p.Settings.InitOptions.LockTimeout != "" {
args = append(args, fmt.Sprintf("-lock-timeout=%s", p.Settings.InitOptions.LockTimeout))
}
args = append(args, "-auto-approve")
return &pluginCommand{
execabs.Command(tofuBin, args...),
p.Settings.NoLog,
}
2024-02-05 19:44:47 +00:00
}