mirror of
https://github.com/thegeeklab/wp-opentofu.git
synced 2024-06-02 18:39:41 +02:00
Compare commits
39 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
9100977fad | ||
|
fe901455a4 | ||
|
10c31f11d3 | ||
53be7b2f70 | |||
|
5f8c89c973 | ||
|
6a3f6e9f3f | ||
3f6ad5ad7a | |||
dcc53a341d | |||
2eda2b50fc | |||
|
554c1ffc07 | ||
|
590346f123 | ||
|
b0e8a2232a | ||
|
c8e947b97a | ||
|
82aa613044 | ||
2122890118 | |||
4776687d47 | |||
|
373566872c | ||
|
47417623a2 | ||
|
3d2b38efbd | ||
|
38dcf61ddc | ||
|
5a86685d35 | ||
|
31ae059795 | ||
|
16ad0c2e21 | ||
|
d499d4b80a | ||
|
6490e69b7b | ||
|
89e1cadd5b | ||
|
7dae989732 | ||
|
c818d5f15e | ||
2187524271 | |||
|
4b5f3e55ad | ||
db2574154a | |||
|
342d98c4d9 | ||
|
68ccd398c5 | ||
|
52e5693b3b | ||
|
9ecdca6db9 | ||
|
366bfb8392 | ||
|
dbddcbe134 | ||
4695cd9603 | |||
|
c60503f256 |
|
@ -23,7 +23,6 @@ linters:
|
|||
- errchkjson
|
||||
- errname
|
||||
- errorlint
|
||||
- execinquery
|
||||
- exhaustive
|
||||
- exportloopref
|
||||
- forcetypeassert
|
||||
|
@ -37,12 +36,12 @@ linters:
|
|||
- gocyclo
|
||||
- godot
|
||||
- godox
|
||||
- goerr113
|
||||
- err113
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goheader
|
||||
- goimports
|
||||
- gomnd
|
||||
- mnd
|
||||
- gomoddirectives
|
||||
- gomodguard
|
||||
- goprintffuncname
|
||||
|
|
|
@ -4,13 +4,10 @@ when:
|
|||
- event: [push, manual]
|
||||
branch:
|
||||
- ${CI_REPO_DEFAULT_BRANCH}
|
||||
- event: [push]
|
||||
branch:
|
||||
- renovate/auto/*
|
||||
|
||||
steps:
|
||||
- name: dryrun
|
||||
image: quay.io/thegeeklab/wp-docker-buildx:3
|
||||
image: quay.io/thegeeklab/wp-docker-buildx:4
|
||||
settings:
|
||||
containerfile: Containerfile.multiarch
|
||||
dry_run: true
|
||||
|
@ -23,7 +20,7 @@ steps:
|
|||
- event: [pull_request]
|
||||
|
||||
- name: publish-dockerhub
|
||||
image: quay.io/thegeeklab/wp-docker-buildx:3
|
||||
image: quay.io/thegeeklab/wp-docker-buildx:4
|
||||
group: container
|
||||
settings:
|
||||
auto_tag: true
|
||||
|
@ -44,7 +41,7 @@ steps:
|
|||
- ${CI_REPO_DEFAULT_BRANCH}
|
||||
|
||||
- name: publish-quay
|
||||
image: quay.io/thegeeklab/wp-docker-buildx:3
|
||||
image: quay.io/thegeeklab/wp-docker-buildx:4
|
||||
group: container
|
||||
settings:
|
||||
auto_tag: true
|
||||
|
|
|
@ -4,9 +4,6 @@ when:
|
|||
- event: [push, manual]
|
||||
branch:
|
||||
- ${CI_REPO_DEFAULT_BRANCH}
|
||||
- event: [push]
|
||||
branch:
|
||||
- renovate/auto/*
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
|
|
|
@ -4,9 +4,6 @@ when:
|
|||
- event: [push, manual]
|
||||
branch:
|
||||
- ${CI_REPO_DEFAULT_BRANCH}
|
||||
- event: [push]
|
||||
branch:
|
||||
- renovate/auto/*
|
||||
|
||||
steps:
|
||||
- name: markdownlint
|
||||
|
|
|
@ -4,9 +4,6 @@ when:
|
|||
- event: [push, manual]
|
||||
branch:
|
||||
- ${CI_REPO_DEFAULT_BRANCH}
|
||||
- event: [push]
|
||||
branch:
|
||||
- renovate/auto/*
|
||||
|
||||
steps:
|
||||
- name: lint
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:ef61a20960397f4d44b0e729298bf02327ca94f1519239ddc6d91689615b1367 as build
|
||||
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:f43c6f049f04cbbaeb28f0aad3eea15274a7d0a7899a617d0037aec48d7ab010 as build
|
||||
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
|
@ -8,7 +8,7 @@ WORKDIR /src
|
|||
|
||||
RUN make build
|
||||
|
||||
FROM docker.io/alpine:3.19@sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48
|
||||
FROM docker.io/alpine:3.20@sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd
|
||||
|
||||
LABEL maintainer="Robert Kaussow <mail@thegeeklab.de>"
|
||||
LABEL org.opencontainers.image.authors="Robert Kaussow <mail@thegeeklab.de>"
|
||||
|
@ -22,7 +22,7 @@ ARG TARGETARCH
|
|||
ARG TOFU_VERSION
|
||||
|
||||
# renovate: datasource=github-releases depName=opentofu/opentofu
|
||||
ENV TOFU_VERSION="${TOFU_VERSION:-v1.6.1}"
|
||||
ENV TOFU_VERSION="${TOFU_VERSION:-v1.7.1}"
|
||||
|
||||
RUN apk --update add --virtual .build-deps libarchive-tools && \
|
||||
apk add --no-cache curl git openssh-client && \
|
||||
|
|
9
Makefile
9
Makefile
|
@ -1,7 +1,7 @@
|
|||
# renovate: datasource=github-releases depName=mvdan/gofumpt
|
||||
GOFUMPT_PACKAGE_VERSION := v0.6.0
|
||||
# renovate: datasource=github-releases depName=golangci/golangci-lint
|
||||
GOLANGCI_LINT_PACKAGE_VERSION := v1.55.2
|
||||
GOLANGCI_LINT_PACKAGE_VERSION := v1.59.0
|
||||
|
||||
EXECUTABLE := wp-opentofu
|
||||
|
||||
|
@ -19,7 +19,6 @@ GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(G
|
|||
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
|
||||
GOTESTSUM_PACKAGE ?= gotest.tools/gotestsum@latest
|
||||
|
||||
GENERATE ?=
|
||||
XGO_VERSION := go-1.22.x
|
||||
XGO_TARGETS ?= linux/amd64,linux/arm64
|
||||
|
||||
|
@ -65,11 +64,7 @@ lint: golangci-lint
|
|||
|
||||
.PHONY: generate
|
||||
generate:
|
||||
$(GO) generate $(GENERATE)
|
||||
|
||||
.PHONY: generate-docs
|
||||
generate-docs:
|
||||
$(GO) generate ./cmd/$(EXECUTABLE)/flags.go
|
||||
$(GO) generate $(PACKAGES)
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
//go:build generate
|
||||
// +build generate
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"github.com/thegeeklab/wp-opentofu/plugin"
|
||||
"github.com/thegeeklab/wp-plugin-go/docs"
|
||||
wp "github.com/thegeeklab/wp-plugin-go/plugin"
|
||||
wp_template "github.com/thegeeklab/wp-plugin-go/template"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
//go:embed templates/docs-data.yaml.tmpl
|
||||
var yamlTemplate embed.FS
|
||||
|
||||
func main() {
|
||||
settings := &plugin.Settings{}
|
||||
app := &cli.App{
|
||||
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
|
||||
}
|
||||
|
||||
out, err := toYAML(app)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fi, err := os.Create("../../docs/data/data-raw.yaml")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer fi.Close()
|
||||
if _, err := fi.WriteString(out); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func toYAML(app *cli.App) (string, error) {
|
||||
var w bytes.Buffer
|
||||
|
||||
yamlTmpl, err := template.New("docs").Funcs(wp_template.LoadFuncMap()).ParseFS(yamlTemplate, "templates/docs-data.yaml.tmpl")
|
||||
if err != nil {
|
||||
fmt.Println(yamlTmpl)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := yamlTmpl.ExecuteTemplate(&w, "docs-data.yaml.tmpl", docs.GetTemplateData(app)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return w.String(), nil
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/thegeeklab/wp-opentofu/plugin"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// SettingsFlags has the cli.Flags for the plugin.Settings.
|
||||
//
|
||||
//go:generate go run docs.go flags.go
|
||||
func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringSliceFlag{
|
||||
Name: "action",
|
||||
Usage: "tofu actions to execute",
|
||||
EnvVars: []string{"PLUGIN_ACTION"},
|
||||
Value: cli.NewStringSlice("validate", "plan", "apply"),
|
||||
Destination: &settings.Action,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "init-option",
|
||||
Usage: "tofu init command options, see https://opentofu.org/docs/cli/commands/init/",
|
||||
EnvVars: []string{"PLUGIN_INIT_OPTION"},
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "fmt-option",
|
||||
Usage: "options for the fmt command, see https://opentofu.org/docs/cli/commands/fmt/",
|
||||
EnvVars: []string{"PLUGIN_FMT_OPTION"},
|
||||
Category: category,
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "parallelism",
|
||||
Usage: "number of concurrent operations",
|
||||
EnvVars: []string{"PLUGIN_PARALLELISM"},
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "root-dir",
|
||||
Usage: "root directory where the tofu files live",
|
||||
EnvVars: []string{"PLUGIN_ROOT_DIR"},
|
||||
Destination: &settings.RootDir,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "no-log",
|
||||
Usage: "suppress tofu command output for `plan`, `apply` and `destroy` action",
|
||||
EnvVars: []string{"PLUGIN_NO_LOG"},
|
||||
Destination: &settings.NoLog,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "targets",
|
||||
Usage: "targets to run `plan` or `apply` action on",
|
||||
EnvVars: []string{"PLUGIN_TARGETS"},
|
||||
Destination: &settings.Targets,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "tofu-version",
|
||||
Usage: "tofu version to use",
|
||||
EnvVars: []string{"PLUGIN_TOFU_VERSION"},
|
||||
Destination: &settings.TofuVersion,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "refresh",
|
||||
Usage: "enables refreshing of the state before `plan` and `apply` commands",
|
||||
EnvVars: []string{"PLUGIN_REFRESH"},
|
||||
Destination: &settings.Refresh,
|
||||
Value: true,
|
||||
Category: category,
|
||||
},
|
||||
}
|
||||
}
|
|
@ -1,11 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/wp-opentofu/plugin"
|
||||
|
||||
wp "github.com/thegeeklab/wp-plugin-go/plugin"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
|
@ -15,14 +11,5 @@ var (
|
|||
)
|
||||
|
||||
func main() {
|
||||
settings := &plugin.Settings{}
|
||||
options := wp.Options{
|
||||
Name: "wp-opentofu",
|
||||
Description: "Manage infrastructure with OpenTofu",
|
||||
Version: BuildVersion,
|
||||
VersionMetadata: fmt.Sprintf("date=%s", BuildDate),
|
||||
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
|
||||
}
|
||||
|
||||
plugin.New(options, settings).Run()
|
||||
plugin.New(nil, BuildVersion, BuildDate).Run()
|
||||
}
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
---
|
||||
{{- if .GlobalArgs }}
|
||||
properties:
|
||||
{{- range $v := .GlobalArgs }}
|
||||
- name: {{ $v.Name }}
|
||||
{{- with $v.Description }}
|
||||
description: |
|
||||
{{ . | ToSentence }}
|
||||
{{- end }}
|
||||
{{- with $v.Type }}
|
||||
type: {{ . }}
|
||||
{{- end }}
|
||||
{{- with $v.Default }}
|
||||
defaultValue: {{ . }}
|
||||
{{- end }}
|
||||
required: {{ default false $v.Required }}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
|
@ -55,5 +55,9 @@ docker build --file Containerfile.multiarch --tag thegeeklab/wp-opentofu .
|
|||
## Test
|
||||
|
||||
```Shell
|
||||
docker run -it thegeeklab/wp-opentofu --action validate
|
||||
docker run --rm \
|
||||
-e PLUGIN_ACTION=validate \
|
||||
-v $(pwd):/build:z \
|
||||
-w /build \
|
||||
thegeeklab/wp-opentofu
|
||||
```
|
||||
|
|
|
@ -19,6 +19,20 @@ properties:
|
|||
type: string
|
||||
required: false
|
||||
|
||||
- name: insecure_skip_verify
|
||||
description: |
|
||||
Skip SSL verification.
|
||||
type: bool
|
||||
defaultValue: false
|
||||
required: false
|
||||
|
||||
- name: log_level
|
||||
description: |
|
||||
Plugin log level.
|
||||
type: string
|
||||
defaultValue: "info"
|
||||
required: false
|
||||
|
||||
- name: no_log
|
||||
description: |
|
||||
Suppress tofu command output for `plan`, `apply` and `destroy` action.
|
||||
|
|
20
go.mod
20
go.mod
|
@ -4,16 +4,17 @@ go 1.22
|
|||
|
||||
require (
|
||||
github.com/Masterminds/semver/v3 v3.2.1
|
||||
github.com/rs/zerolog v1.32.0
|
||||
github.com/thegeeklab/wp-plugin-go v1.6.0
|
||||
github.com/urfave/cli/v2 v2.27.1
|
||||
golang.org/x/sys v0.17.0
|
||||
github.com/rs/zerolog v1.33.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2
|
||||
github.com/urfave/cli/v2 v2.27.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/google/uuid v1.1.1 // indirect
|
||||
github.com/huandu/xstrings v1.3.3 // indirect
|
||||
github.com/imdario/mergo v0.3.11 // indirect
|
||||
|
@ -22,10 +23,13 @@ require (
|
|||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mitchellh/copystructure v1.0.0 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/shopspring/decimal v1.2.0 // indirect
|
||||
github.com/spf13/cast v1.3.1 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
|
||||
golang.org/x/crypto v0.19.0 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
39
go.sum
39
go.sum
|
@ -6,14 +6,12 @@ github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
|
|||
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
|
||||
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
|
||||
|
@ -36,8 +34,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
|||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
|
||||
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
|
||||
|
@ -47,27 +45,27 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
|
|||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/thegeeklab/wp-plugin-go v1.6.0 h1:DQ+oLs67FusgeZe3881x9pxH+UrddWygE0eGHLM7jgg=
|
||||
github.com/thegeeklab/wp-plugin-go v1.6.0/go.mod h1:r+Oi0CpnnlG59u4Qp2n6xHGTxd6Krk+OPBZ9F7LQT8A=
|
||||
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
|
||||
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
|
||||
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g=
|
||||
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2/go.mod h1:ij1iJcAVgzerBTqXnmq0bu1VA+hhVVwzXKqiqfoGjjg=
|
||||
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
||||
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
|
||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
@ -79,8 +77,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
|
@ -92,6 +90,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
|
|||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
|
|
42
internal/docs/main.go
Normal file
42
internal/docs/main.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
//go:build generate
|
||||
// +build generate
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/thegeeklab/wp-opentofu/plugin"
|
||||
plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs"
|
||||
plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tmpl := "https://raw.githubusercontent.com/thegeeklab/woodpecker-plugins/main/templates/docs-data.yaml.tmpl"
|
||||
client := http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
p := plugin.New(nil)
|
||||
|
||||
out, err := plugin_template.Render(context.Background(), client, tmpl, plugin_docs.GetTemplateData(p.App))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
outputFile := flag.String("output", "", "Output file path")
|
||||
flag.Parse()
|
||||
|
||||
if *outputFile == "" {
|
||||
panic("no output file specified")
|
||||
}
|
||||
|
||||
err = os.WriteFile(*outputFile, []byte(out), 0o644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
|
@ -7,7 +7,8 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/thegeeklab/wp-plugin-go/trace"
|
||||
"github.com/thegeeklab/wp-opentofu/tofu"
|
||||
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -42,21 +43,21 @@ func (p *Plugin) run(ctx context.Context) error {
|
|||
|
||||
func (p *Plugin) FlagsFromContext() error {
|
||||
if p.Context.String("init-option") != "" {
|
||||
initOptions := InitOptions{}
|
||||
initOptions := tofu.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
|
||||
p.Settings.Tofu.InitOptions = initOptions
|
||||
}
|
||||
|
||||
if p.Context.String("fmt-option") != "" {
|
||||
fmtOptions := FmtOptions{}
|
||||
fmtOptions := tofu.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
|
||||
p.Settings.Tofu.FmtOptions = fmtOptions
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -69,9 +70,9 @@ func (p *Plugin) Validate() error {
|
|||
p.Settings.DataDir = value
|
||||
}
|
||||
|
||||
p.Settings.OutFile = "plan.tfout"
|
||||
p.Settings.Tofu.OutFile = "plan.tfout"
|
||||
if p.Settings.DataDir == ".terraform" {
|
||||
p.Settings.OutFile = fmt.Sprintf("%s.plan.tfout", p.Settings.DataDir)
|
||||
p.Settings.Tofu.OutFile = fmt.Sprintf("%s.plan.tfout", p.Settings.DataDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -79,6 +80,9 @@ func (p *Plugin) Validate() error {
|
|||
|
||||
// Execute provides the implementation of the plugin.
|
||||
func (p *Plugin) Execute() error {
|
||||
batchCmd := make([]*plugin_exec.Cmd, 0)
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Version())
|
||||
|
||||
if p.Settings.TofuVersion != "" {
|
||||
err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize)
|
||||
if err != nil {
|
||||
|
@ -86,51 +90,45 @@ func (p *Plugin) Execute() error {
|
|||
}
|
||||
}
|
||||
|
||||
commands := []*pluginCommand{
|
||||
p.versionCommand(),
|
||||
}
|
||||
|
||||
commands = append(commands, p.initCommand())
|
||||
commands = append(commands, p.getModulesCommand())
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Init())
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.GetModules())
|
||||
|
||||
for _, action := range p.Settings.Action.Value() {
|
||||
switch action {
|
||||
case "fmt":
|
||||
commands = append(commands, p.fmtCommand())
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Fmt())
|
||||
case "validate":
|
||||
commands = append(commands, p.validateCommand())
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Validate())
|
||||
case "plan":
|
||||
commands = append(commands, p.planCommand(false))
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Plan(false))
|
||||
case "plan-destroy":
|
||||
commands = append(commands, p.planCommand(true))
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Plan(true))
|
||||
case "apply":
|
||||
commands = append(commands, p.applyCommand())
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Apply())
|
||||
case "destroy":
|
||||
commands = append(commands, p.destroyCommand())
|
||||
batchCmd = append(batchCmd, p.Settings.Tofu.Destroy())
|
||||
default:
|
||||
return fmt.Errorf("%w: %s", ErrActionUnknown, action)
|
||||
}
|
||||
}
|
||||
|
||||
if err := deleteDir(p.Settings.DataDir); err != nil {
|
||||
if err := os.RemoveAll(p.Settings.DataDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, command := range commands {
|
||||
command.cmd.Stdout = os.Stdout
|
||||
command.cmd.Stderr = os.Stderr
|
||||
command.cmd.Env = os.Environ()
|
||||
|
||||
if p.Settings.RootDir != "" {
|
||||
command.cmd.Dir = p.Settings.RootDir
|
||||
for _, cmd := range batchCmd {
|
||||
if cmd == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
trace.Cmd(command.cmd)
|
||||
if p.Settings.RootDir != "" {
|
||||
cmd.Dir = p.Settings.RootDir
|
||||
}
|
||||
|
||||
if err := command.cmd.Run(); err != nil {
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return deleteDir(p.Settings.DataDir)
|
||||
return os.RemoveAll(p.Settings.DataDir)
|
||||
}
|
||||
|
|
137
plugin/plugin.go
137
plugin/plugin.go
|
@ -1,57 +1,124 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
wp "github.com/thegeeklab/wp-plugin-go/plugin"
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/wp-opentofu/tofu"
|
||||
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
//go:generate go run ../internal/docs/main.go -output=../docs/data/data-raw.yaml
|
||||
|
||||
// Plugin implements provide the plugin.
|
||||
type Plugin struct {
|
||||
*wp.Plugin
|
||||
*plugin_base.Plugin
|
||||
Settings *Settings
|
||||
}
|
||||
|
||||
// Settings for the Plugin.
|
||||
type Settings struct {
|
||||
Action cli.StringSlice
|
||||
|
||||
TofuVersion string
|
||||
InitOptions InitOptions
|
||||
FmtOptions FmtOptions
|
||||
|
||||
Action cli.StringSlice
|
||||
RootDir string
|
||||
DataDir string
|
||||
OutFile string
|
||||
Parallelism int
|
||||
Targets cli.StringSlice
|
||||
Refresh bool
|
||||
NoLog bool
|
||||
TofuVersion string
|
||||
Tofu tofu.Tofu
|
||||
}
|
||||
|
||||
// InitOptions include options for the OpenTofu init command.
|
||||
type InitOptions struct {
|
||||
BackendConfig []string `json:"backend-config"`
|
||||
Lock *bool `json:"lock"`
|
||||
LockTimeout string `json:"lock-timeout"`
|
||||
}
|
||||
|
||||
// FmtOptions fmt options for the OpenTofu fmt command.
|
||||
type FmtOptions struct {
|
||||
List *bool `json:"list"`
|
||||
Write *bool `json:"write"`
|
||||
Diff *bool `json:"diff"`
|
||||
Check *bool `json:"check"`
|
||||
}
|
||||
|
||||
func New(options wp.Options, settings *Settings) *Plugin {
|
||||
p := &Plugin{}
|
||||
|
||||
if options.Execute == nil {
|
||||
options.Execute = p.run
|
||||
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
|
||||
p := &Plugin{
|
||||
Settings: &Settings{},
|
||||
}
|
||||
|
||||
p.Plugin = wp.New(options)
|
||||
p.Settings = settings
|
||||
options := plugin_base.Options{
|
||||
Name: "wp-opentofu",
|
||||
Description: "Manage infrastructure with OpenTofu",
|
||||
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
|
||||
Execute: p.run,
|
||||
HideWoodpeckerFlags: true,
|
||||
}
|
||||
|
||||
if len(build) > 0 {
|
||||
options.Version = build[0]
|
||||
}
|
||||
|
||||
if len(build) > 1 {
|
||||
options.VersionMetadata = fmt.Sprintf("date=%s", build[1])
|
||||
}
|
||||
|
||||
if e != nil {
|
||||
options.Execute = e
|
||||
}
|
||||
|
||||
p.Plugin = plugin_base.New(options)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// Flags returns a slice of CLI flags for the plugin.
|
||||
func Flags(settings *Settings, category string) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringSliceFlag{
|
||||
Name: "action",
|
||||
Usage: "tofu actions to execute",
|
||||
EnvVars: []string{"PLUGIN_ACTION"},
|
||||
Value: cli.NewStringSlice("validate", "plan", "apply"),
|
||||
Destination: &settings.Action,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "init-option",
|
||||
Usage: "tofu init command options, see https://opentofu.org/docs/cli/commands/init/",
|
||||
EnvVars: []string{"PLUGIN_INIT_OPTION"},
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "fmt-option",
|
||||
Usage: "options for the fmt command, see https://opentofu.org/docs/cli/commands/fmt/",
|
||||
EnvVars: []string{"PLUGIN_FMT_OPTION"},
|
||||
Category: category,
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "parallelism",
|
||||
Usage: "number of concurrent operations",
|
||||
EnvVars: []string{"PLUGIN_PARALLELISM"},
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "root-dir",
|
||||
Usage: "root directory where the tofu files live",
|
||||
EnvVars: []string{"PLUGIN_ROOT_DIR"},
|
||||
Destination: &settings.RootDir,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "no-log",
|
||||
Usage: "suppress tofu command output for `plan`, `apply` and `destroy` action",
|
||||
EnvVars: []string{"PLUGIN_NO_LOG"},
|
||||
Destination: &settings.Tofu.NoLog,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "targets",
|
||||
Usage: "targets to run `plan` or `apply` action on",
|
||||
EnvVars: []string{"PLUGIN_TARGETS"},
|
||||
Destination: &settings.Tofu.Targets,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "tofu-version",
|
||||
Usage: "tofu version to use",
|
||||
EnvVars: []string{"PLUGIN_TOFU_VERSION"},
|
||||
Destination: &settings.TofuVersion,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "refresh",
|
||||
Usage: "enables refreshing of the state before `plan` and `apply` commands",
|
||||
EnvVars: []string{"PLUGIN_REFRESH"},
|
||||
Destination: &settings.Tofu.Refresh,
|
||||
Value: true,
|
||||
Category: category,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
181
plugin/tofu.go
181
plugin/tofu.go
|
@ -1,181 +0,0 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
const (
|
||||
tofuBin = "/usr/local/bin/tofu"
|
||||
)
|
||||
|
||||
type pluginCommand struct {
|
||||
cmd *execabs.Cmd
|
||||
private bool
|
||||
}
|
||||
|
||||
func (p *Plugin) versionCommand() *pluginCommand {
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, "version"),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) initCommand() *pluginCommand {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) getModulesCommand() *pluginCommand {
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, "get"),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) validateCommand() *pluginCommand {
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, "validate"),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) fmtCommand() *pluginCommand {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) planCommand(destroy bool) *pluginCommand {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) applyCommand() *pluginCommand {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) destroyCommand() *pluginCommand {
|
||||
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,
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ import (
|
|||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/thegeeklab/wp-opentofu/tofu"
|
||||
)
|
||||
|
||||
func installPackage(ctx context.Context, client *http.Client, version string, maxSize int64) error {
|
||||
|
@ -33,7 +34,9 @@ func installPackage(ctx context.Context, client *http.Client, version string, ma
|
|||
return fmt.Errorf("failed to create tmp dir: %w", err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(tmpdir)
|
||||
defer func() {
|
||||
_ = os.RemoveAll(tmpdir)
|
||||
}()
|
||||
|
||||
log.Debug().
|
||||
Str("tmpdir", tmpdir).
|
||||
|
@ -49,7 +52,7 @@ func installPackage(ctx context.Context, client *http.Client, version string, ma
|
|||
return fmt.Errorf("failed to unzip: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Rename(filepath.Join(tmpdir, "tofu"), tofuBin); err != nil {
|
||||
if err := os.Rename(filepath.Join(tmpdir, "tofu"), tofu.TofuBin); err != nil {
|
||||
return fmt.Errorf("failed to rename: %w", err)
|
||||
}
|
||||
|
||||
|
@ -126,6 +129,7 @@ func unzip(src, dest string, maxSize int64) error {
|
|||
_ = os.MkdirAll(path, f.Mode())
|
||||
} else {
|
||||
_ = os.MkdirAll(filepath.Dir(path), f.Mode())
|
||||
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -166,11 +170,3 @@ func sanitizeArchivePath(d, t string) (string, error) {
|
|||
|
||||
return "", fmt.Errorf("%w: %v", ErrTaintedPath, t)
|
||||
}
|
||||
|
||||
func deleteDir(path string) error {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
|
224
tofu/tofu.go
Normal file
224
tofu/tofu.go
Normal file
|
@ -0,0 +1,224 @@
|
|||
package tofu
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const TofuBin = "/usr/local/bin/tofu"
|
||||
|
||||
type Tofu struct {
|
||||
InitOptions InitOptions
|
||||
FmtOptions FmtOptions
|
||||
|
||||
OutFile string
|
||||
Parallelism int
|
||||
Targets cli.StringSlice
|
||||
Refresh bool
|
||||
NoLog bool
|
||||
}
|
||||
|
||||
// InitOptions include options for the OpenTofu init command.
|
||||
type InitOptions struct {
|
||||
BackendConfig []string `json:"backend-config"`
|
||||
Lock *bool `json:"lock"`
|
||||
LockTimeout string `json:"lock-timeout"`
|
||||
}
|
||||
|
||||
// FmtOptions fmt options for the OpenTofu fmt command.
|
||||
type FmtOptions struct {
|
||||
List *bool `json:"list"`
|
||||
Write *bool `json:"write"`
|
||||
Diff *bool `json:"diff"`
|
||||
Check *bool `json:"check"`
|
||||
}
|
||||
|
||||
func (t *Tofu) Version() *plugin_exec.Cmd {
|
||||
cmd := plugin_exec.Command(TofuBin, "version")
|
||||
|
||||
if !t.NoLog {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) Init() *plugin_exec.Cmd {
|
||||
args := []string{
|
||||
"init",
|
||||
}
|
||||
|
||||
for _, v := range t.InitOptions.BackendConfig {
|
||||
args = append(args, fmt.Sprintf("-backend-config=%s", v))
|
||||
}
|
||||
|
||||
// Fail tofu execution on prompt
|
||||
args = append(args, "-input=false")
|
||||
|
||||
cmd := plugin_exec.Command(TofuBin, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) GetModules() *plugin_exec.Cmd {
|
||||
cmd := plugin_exec.Command(TofuBin, "get")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) Validate() *plugin_exec.Cmd {
|
||||
cmd := plugin_exec.Command(TofuBin, "validate")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) Fmt() *plugin_exec.Cmd {
|
||||
args := []string{
|
||||
"fmt",
|
||||
}
|
||||
|
||||
if t.FmtOptions.List != nil {
|
||||
args = append(args, fmt.Sprintf("-list=%t", *t.FmtOptions.List))
|
||||
}
|
||||
|
||||
if t.FmtOptions.Write != nil {
|
||||
args = append(args, fmt.Sprintf("-write=%t", *t.FmtOptions.Write))
|
||||
}
|
||||
|
||||
if t.FmtOptions.Diff != nil {
|
||||
args = append(args, fmt.Sprintf("-diff=%t", *t.FmtOptions.Diff))
|
||||
}
|
||||
|
||||
if t.FmtOptions.Check != nil {
|
||||
args = append(args, fmt.Sprintf("-check=%t", *t.FmtOptions.Check))
|
||||
}
|
||||
|
||||
cmd := plugin_exec.Command(TofuBin, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) Plan(destroy bool) *plugin_exec.Cmd {
|
||||
args := []string{
|
||||
"plan",
|
||||
}
|
||||
|
||||
if destroy {
|
||||
args = append(args, "-destroy")
|
||||
} else if t.OutFile != "" {
|
||||
args = append(args, fmt.Sprintf("-out=%s", t.OutFile))
|
||||
}
|
||||
|
||||
for _, value := range t.Targets.Value() {
|
||||
args = append(args, "--target", value)
|
||||
}
|
||||
|
||||
if t.Parallelism > 0 {
|
||||
args = append(args, fmt.Sprintf("-parallelism=%d", t.Parallelism))
|
||||
}
|
||||
|
||||
if t.InitOptions.Lock != nil {
|
||||
args = append(args, fmt.Sprintf("-lock=%t", *t.InitOptions.Lock))
|
||||
}
|
||||
|
||||
if t.InitOptions.LockTimeout != "" {
|
||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", t.InitOptions.LockTimeout))
|
||||
}
|
||||
|
||||
if !t.Refresh {
|
||||
args = append(args, "-refresh=false")
|
||||
}
|
||||
|
||||
cmd := plugin_exec.Command(TofuBin, args...)
|
||||
|
||||
if !t.NoLog {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) Apply() *plugin_exec.Cmd {
|
||||
args := []string{
|
||||
"apply",
|
||||
}
|
||||
|
||||
for _, v := range t.Targets.Value() {
|
||||
args = append(args, "--target", v)
|
||||
}
|
||||
|
||||
if t.Parallelism > 0 {
|
||||
args = append(args, fmt.Sprintf("-parallelism=%d", t.Parallelism))
|
||||
}
|
||||
|
||||
if t.InitOptions.Lock != nil {
|
||||
args = append(args, fmt.Sprintf("-lock=%t", *t.InitOptions.Lock))
|
||||
}
|
||||
|
||||
if t.InitOptions.LockTimeout != "" {
|
||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", t.InitOptions.LockTimeout))
|
||||
}
|
||||
|
||||
if !t.Refresh {
|
||||
args = append(args, "-refresh=false")
|
||||
}
|
||||
|
||||
if t.OutFile != "" {
|
||||
args = append(args, t.OutFile)
|
||||
}
|
||||
|
||||
cmd := plugin_exec.Command(TofuBin, args...)
|
||||
|
||||
if !t.NoLog {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *Tofu) Destroy() *plugin_exec.Cmd {
|
||||
args := []string{
|
||||
"destroy",
|
||||
}
|
||||
|
||||
for _, v := range t.Targets.Value() {
|
||||
args = append(args, fmt.Sprintf("-target=%s", v))
|
||||
}
|
||||
|
||||
if t.Parallelism > 0 {
|
||||
args = append(args, fmt.Sprintf("-parallelism=%d", t.Parallelism))
|
||||
}
|
||||
|
||||
if t.InitOptions.Lock != nil {
|
||||
args = append(args, fmt.Sprintf("-lock=%t", *t.InitOptions.Lock))
|
||||
}
|
||||
|
||||
if t.InitOptions.LockTimeout != "" {
|
||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", t.InitOptions.LockTimeout))
|
||||
}
|
||||
|
||||
args = append(args, "-auto-approve")
|
||||
|
||||
cmd := plugin_exec.Command(TofuBin, args...)
|
||||
|
||||
if !t.NoLog {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
552
tofu/tofu_test.go
Normal file
552
tofu/tofu_test.go
Normal file
|
@ -0,0 +1,552 @@
|
|||
package tofu
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func boolPtr(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
|
||||
func TestTofu_Version(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "test version command",
|
||||
tofu: &Tofu{},
|
||||
want: []string{TofuBin, "version"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Version()
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_Init(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "init with no backend config",
|
||||
tofu: &Tofu{},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"init",
|
||||
"-input=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "init with single backend config",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
BackendConfig: []string{"key=value"},
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"init",
|
||||
"-backend-config=key=value",
|
||||
"-input=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "init with multiple backend configs",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
BackendConfig: []string{"key1=value1", "key2=value2"},
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"init",
|
||||
"-backend-config=key1=value1",
|
||||
"-backend-config=key2=value2",
|
||||
"-input=false",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Init()
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_GetModules(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "get modules command",
|
||||
tofu: &Tofu{},
|
||||
want: []string{TofuBin, "get"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.GetModules()
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "validate command",
|
||||
tofu: &Tofu{},
|
||||
want: []string{TofuBin, "validate"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Validate()
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_Fmt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "fmt with no options",
|
||||
tofu: &Tofu{},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"fmt",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fmt with list option",
|
||||
tofu: &Tofu{
|
||||
FmtOptions: FmtOptions{
|
||||
List: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"fmt",
|
||||
"-list=true",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fmt with write option",
|
||||
tofu: &Tofu{
|
||||
FmtOptions: FmtOptions{
|
||||
Write: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"fmt",
|
||||
"-write=true",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fmt with diff option",
|
||||
tofu: &Tofu{
|
||||
FmtOptions: FmtOptions{
|
||||
Diff: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"fmt",
|
||||
"-diff=true",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fmt with check option",
|
||||
tofu: &Tofu{
|
||||
FmtOptions: FmtOptions{
|
||||
Check: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"fmt",
|
||||
"-check=true",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fmt with multiple options",
|
||||
tofu: &Tofu{
|
||||
FmtOptions: FmtOptions{
|
||||
List: boolPtr(true),
|
||||
Write: boolPtr(true),
|
||||
Diff: boolPtr(true),
|
||||
Check: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"fmt",
|
||||
"-list=true",
|
||||
"-write=true",
|
||||
"-diff=true",
|
||||
"-check=true",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Fmt()
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_Plan(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
destroy bool
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "plan with no options",
|
||||
tofu: &Tofu{},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with output options",
|
||||
tofu: &Tofu{
|
||||
OutFile: "plan.tfout",
|
||||
},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"-out=plan.tfout",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with destroy option",
|
||||
tofu: &Tofu{},
|
||||
destroy: true,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"-destroy",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with targets",
|
||||
tofu: &Tofu{
|
||||
Targets: *cli.NewStringSlice("target1", "target2"),
|
||||
},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"--target", "target1",
|
||||
"--target", "target2",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with parallelism",
|
||||
tofu: &Tofu{
|
||||
Parallelism: 10,
|
||||
},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"-parallelism=10",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with lock option",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
Lock: boolPtr(true),
|
||||
},
|
||||
},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"-lock=true",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with lock timeout",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
LockTimeout: "10s",
|
||||
},
|
||||
},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
"-lock-timeout=10s",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan with refresh option",
|
||||
tofu: &Tofu{
|
||||
Refresh: true,
|
||||
},
|
||||
destroy: false,
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"plan",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Plan(tt.destroy)
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_Apply(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "apply with no options",
|
||||
tofu: &Tofu{},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with targets",
|
||||
tofu: &Tofu{
|
||||
Targets: *cli.NewStringSlice("target1", "target2"),
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"--target", "target1",
|
||||
"--target", "target2",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with parallelism",
|
||||
tofu: &Tofu{
|
||||
Parallelism: 10,
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"-parallelism=10",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with lock option",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
Lock: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"-lock=true",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with lock timeout",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
LockTimeout: "10s",
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"-lock-timeout=10s",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with refresh option",
|
||||
tofu: &Tofu{
|
||||
Refresh: true,
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with output file",
|
||||
tofu: &Tofu{
|
||||
OutFile: "out.tfout",
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"-refresh=false",
|
||||
"out.tfout",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "apply with no log",
|
||||
tofu: &Tofu{
|
||||
NoLog: true,
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"apply",
|
||||
"-refresh=false",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Apply()
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
|
||||
if tt.tofu.NoLog {
|
||||
assert.Equal(t, cmd.Stdout, nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTofu_Destroy(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tofu *Tofu
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "destroy with no options",
|
||||
tofu: &Tofu{},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"destroy",
|
||||
"-auto-approve",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "destroy with targets",
|
||||
tofu: &Tofu{
|
||||
Targets: *cli.NewStringSlice("target1", "target2"),
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"destroy",
|
||||
"-target=target1",
|
||||
"-target=target2",
|
||||
"-auto-approve",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "destroy with parallelism",
|
||||
tofu: &Tofu{
|
||||
Parallelism: 10,
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"destroy",
|
||||
"-parallelism=10",
|
||||
"-auto-approve",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "destroy with lock option",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
Lock: boolPtr(true),
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"destroy",
|
||||
"-lock=true",
|
||||
"-auto-approve",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "destroy with lock timeout",
|
||||
tofu: &Tofu{
|
||||
InitOptions: InitOptions{
|
||||
LockTimeout: "10s",
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"destroy",
|
||||
"-lock-timeout=10s",
|
||||
"-auto-approve",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "destroy with no log",
|
||||
tofu: &Tofu{
|
||||
NoLog: true,
|
||||
},
|
||||
want: []string{
|
||||
TofuBin,
|
||||
"destroy",
|
||||
"-auto-approve",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.tofu.Destroy()
|
||||
|
||||
assert.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
|
||||
if tt.tofu.NoLog {
|
||||
assert.Equal(t, cmd.Stdout, nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user