0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-25 09:40:39 +00:00
wp-opentofu/plugin/impl.go

141 lines
3.2 KiB
Go
Raw Normal View History

2024-02-05 19:44:47 +00:00
package plugin
import (
"context"
"encoding/json"
2024-02-05 19:44:47 +00:00
"errors"
"fmt"
"io"
2024-02-05 19:44:47 +00:00
"os"
"github.com/thegeeklab/wp-plugin-go/v2/trace"
2024-02-05 19:44:47 +00:00
)
var (
2024-02-05 20:50:41 +00:00
ErrTaintedPath = errors.New("filepath is tainted")
ErrMaxSizeSizeLimit = errors.New("max size limit of decoded data exceeded")
ErrActionUnknown = errors.New("action not found")
ErrInvalidTofuVersion = errors.New("invalid version string")
ErrHTTPError = errors.New("http error")
2024-02-05 19:44:47 +00:00
)
const (
maxDecompressionSize = 100 * 1024 * 1024
2024-02-05 19:44:47 +00:00
defaultDirPerm = 0o755
)
//nolint:revive
func (p *Plugin) run(ctx context.Context) error {
if err := p.FlagsFromContext(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
2024-02-05 19:44:47 +00:00
if err := p.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
if err := p.Execute(); err != nil {
return fmt.Errorf("execution failed: %w", err)
}
return nil
}
func (p *Plugin) FlagsFromContext() error {
if p.Context.String("init-option") != "" {
initOptions := InitOptions{}
if err := json.Unmarshal([]byte(p.Context.String("init-option")), &initOptions); err != nil {
return fmt.Errorf("cannot unmarshal init_option: %w", err)
}
p.Settings.InitOptions = initOptions
}
if p.Context.String("fmt-option") != "" {
fmtOptions := FmtOptions{}
if err := json.Unmarshal([]byte(p.Context.String("fmt-option")), &fmtOptions); err != nil {
return fmt.Errorf("cannot unmarshal fmt_option: %w", err)
}
p.Settings.FmtOptions = fmtOptions
}
return nil
}
2024-02-05 19:44:47 +00:00
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
p.Settings.DataDir = ".terraform"
if value, ok := os.LookupEnv("TF_DATA_DIR"); ok {
p.Settings.DataDir = value
}
p.Settings.OutFile = "plan.tfout"
if p.Settings.DataDir == ".terraform" {
p.Settings.OutFile = fmt.Sprintf("%s.plan.tfout", p.Settings.DataDir)
}
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
batchCmd := make([]*Cmd, 0)
batchCmd = append(batchCmd, p.versionCommand())
2024-02-05 20:50:41 +00:00
if p.Settings.TofuVersion != "" {
err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize)
2024-02-05 19:44:47 +00:00
if err != nil {
return err
}
}
batchCmd = append(batchCmd, p.initCommand())
batchCmd = append(batchCmd, p.getModulesCommand())
2024-02-05 19:44:47 +00:00
for _, action := range p.Settings.Action.Value() {
switch action {
case "fmt":
batchCmd = append(batchCmd, p.fmtCommand())
2024-02-05 19:44:47 +00:00
case "validate":
batchCmd = append(batchCmd, p.validateCommand())
2024-02-05 19:44:47 +00:00
case "plan":
batchCmd = append(batchCmd, p.planCommand(false))
2024-02-05 19:44:47 +00:00
case "plan-destroy":
batchCmd = append(batchCmd, p.planCommand(true))
2024-02-05 19:44:47 +00:00
case "apply":
batchCmd = append(batchCmd, p.applyCommand())
2024-02-05 19:44:47 +00:00
case "destroy":
batchCmd = append(batchCmd, p.destroyCommand())
2024-02-05 19:44:47 +00:00
default:
return fmt.Errorf("%w: %s", ErrActionUnknown, action)
}
}
if err := deleteDir(p.Settings.DataDir); err != nil {
2024-02-05 19:44:47 +00:00
return err
}
for _, bc := range batchCmd {
bc.Stdout = os.Stdout
bc.Stderr = os.Stderr
trace.Cmd(bc.Cmd)
2024-02-05 19:44:47 +00:00
bc.Env = os.Environ()
if bc.Private {
bc.Stdout = io.Discard
2024-02-05 19:44:47 +00:00
}
if p.Settings.RootDir != "" {
bc.Dir = p.Settings.RootDir
}
2024-02-05 19:44:47 +00:00
if err := bc.Run(); err != nil {
2024-02-05 19:44:47 +00:00
return err
}
}
return deleteDir(p.Settings.DataDir)
2024-02-05 19:44:47 +00:00
}