0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-06-03 04:49:42 +02:00

Compare commits

...

45 Commits
v0.1.2 ... main

Author SHA1 Message Date
renovate[bot]
9100977fad
chore(deps): update docker.io/alpine docker tag to v3.20 (#49)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-06-01 13:21:08 +02:00
renovate[bot]
fe901455a4 chore(deps): update dependency golangci/golangci-lint to v1.59.0 2024-05-27 04:13:15 +00:00
renovate[bot]
10c31f11d3
fix(deps): update module github.com/rs/zerolog to v1.33.0 (#50)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-24 09:37:10 +02:00
53be7b2f70
chore: migrate to wp-plugin-go v3 (#48) 2024-05-17 21:55:22 +02:00
renovate[bot]
5f8c89c973
chore(docker): update docker.io/library/golang:1.22 docker digest to f43c6f0 (#46)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-16 13:40:55 +02:00
renovate[bot]
6a3f6e9f3f chore(deps): update dependency golangci/golangci-lint to v1.58.1 2024-05-13 04:47:03 +00:00
3f6ad5ad7a
fix: fix duplicate command trace 2024-05-12 13:25:37 +02:00
dcc53a341d
refactor: switch to plugin Cmd and add tests (#44) 2024-05-12 12:03:10 +02:00
2eda2b50fc
ci: fix golangci-lint deprecations 2024-05-12 11:08:33 +02:00
renovate[bot]
554c1ffc07
fix(deps): update module github.com/thegeeklab/wp-plugin-go/v2 to v2.3.1 (#40)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-10 08:47:42 +02:00
renovate[bot]
590346f123
chore(deps): update dependency opentofu/opentofu to v1.7.1 (#43)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-10 08:47:33 +02:00
renovate[bot]
b0e8a2232a chore(docker): update docker.io/library/golang:1.22 docker digest to b1e05e2 2024-05-08 05:05:59 +00:00
renovate[bot]
c8e947b97a
fix(deps): update module golang.org/x/sys to v0.20.0 (#39)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-07 12:15:48 +02:00
renovate[bot]
82aa613044 chore(deps): update dependency golangci/golangci-lint to v1.58.0 2024-05-06 05:04:19 +00:00
2122890118
fix: fix overwrite of ExecuteFunc (#38) 2024-05-04 16:19:48 +02:00
4776687d47
docs: hide system flags and refactor generator (#37) 2024-05-04 15:51:32 +02:00
renovate[bot]
373566872c
chore(deps): update dependency opentofu/opentofu to v1.7.0 (#34)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-01 12:36:52 +02:00
renovate[bot]
47417623a2
fix(deps): update module github.com/urfave/cli/v2 to v2.27.2 (#33)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-29 10:25:51 +02:00
renovate[bot]
3d2b38efbd chore(docker): update docker.io/library/golang:1.22 docker digest to d5302d4 2024-04-25 03:15:11 +00:00
renovate[bot]
38dcf61ddc
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.7.1 (#31)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-22 12:36:53 +02:00
renovate[bot]
5a86685d35 chore(docker): update docker.io/library/golang:1.22 docker digest to 450e382 2024-04-11 07:46:01 +00:00
renovate[bot]
31ae059795 chore(docker): update docker.io/library/golang:1.22 docker digest to 3cb9b4d 2024-04-10 06:41:30 +00:00
renovate[bot]
16ad0c2e21
fix(deps): update module golang.org/x/sys to v0.19.0 (#28)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-05 08:55:13 +02:00
renovate[bot]
d499d4b80a chore(docker): update docker.io/library/golang:1.22 docker digest to c4fb952 2024-04-04 04:40:32 +00:00
renovate[bot]
6490e69b7b chore(deps): update dependency golangci/golangci-lint to v1.57.2 2024-04-01 22:41:50 +00:00
renovate[bot]
89e1cadd5b
chore(deps): update quay.io/thegeeklab/wp-docker-buildx docker tag to v4 (#25)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-27 08:42:53 +01:00
renovate[bot]
7dae989732 chore(deps): update dependency golangci/golangci-lint to v1.57.1 2024-03-25 05:12:39 +00:00
renovate[bot]
c818d5f15e
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.7.0 (#22)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-14 20:46:37 +01:00
2187524271
fix: delete workdir recursively 2024-03-14 20:45:00 +01:00
renovate[bot]
4b5f3e55ad chore(docker): update docker.io/library/golang:1.22 docker digest to 0b55ab8 2024-03-13 06:13:44 +00:00
db2574154a
cleanup docs and examples 2024-03-12 20:43:40 +01:00
renovate[bot]
342d98c4d9
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.6.1 (#21)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-07 21:26:55 +01:00
renovate[bot]
68ccd398c5
fix(deps): update module golang.org/x/sys to v0.18.0 (#19)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-06 14:42:15 +01:00
renovate[bot]
52e5693b3b chore(docker): update docker.io/library/golang:1.22 docker digest to 34ce21a 2024-03-06 04:46:43 +00:00
renovate[bot]
9ecdca6db9
chore(deps): update dependency opentofu/opentofu to v1.6.2 (#18)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-02-26 10:12:45 +01:00
renovate[bot]
366bfb8392 chore(deps): update dependency golangci/golangci-lint to v1.56.2 2024-02-19 03:41:53 +00:00
renovate[bot]
dbddcbe134 chore(docker): update docker digests 2024-02-16 03:46:18 +00:00
4695cd9603
[skip ci] revert renovate automerge config 2024-02-15 12:19:34 +01:00
renovate[bot]
c60503f256
chore(deps): update dependency golangci/golangci-lint to v1.56.1 (#15)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
2024-02-12 09:25:46 +01:00
renovate[bot]
7181d1486b
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.6.0 (#14)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-02-09 22:08:05 +01:00
renovate[bot]
810bcbe855
fix(deps): update module golang.org/x/sys to v0.17.0 (#12)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-02-07 22:35:36 +01:00
renovate[bot]
d7b43d5755
chore(deps): update docker.io/library/golang docker tag to v1.22 (#6)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
2024-02-07 22:32:12 +01:00
4aa972e38e
fix: add back parsing for fmt_option and init_option (#13) 2024-02-07 20:32:15 +01:00
641b83d579
docs: add missing test command (#11) 2024-02-07 13:56:06 +01:00
9785aac2c6
fix: add dependencies required by tofu or providers (#10) 2024-02-07 13:48:35 +01:00
22 changed files with 1047 additions and 483 deletions

View File

@ -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

View File

@ -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

View File

@ -4,13 +4,10 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: build
image: docker.io/techknowlogick/xgo:go-1.21.x
image: docker.io/techknowlogick/xgo:go-1.22.x
commands:
- ln -s $(pwd) /source
- make release

View File

@ -4,9 +4,6 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: markdownlint

View File

@ -4,17 +4,14 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: lint
image: docker.io/library/golang:1.21
image: docker.io/library/golang:1.22
commands:
- make lint
- name: test
image: docker.io/library/golang:1.21
image: docker.io/library/golang:1.22
commands:
- make test

View File

@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.21@sha256:7b575fe0d9c2e01553b04d9de8ffea6d35ca3ab3380d2a8db2acc8f0f1519a53 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,9 +22,10 @@ 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 curl libarchive-tools && \
RUN apk --update add --virtual .build-deps libarchive-tools && \
apk add --no-cache curl git openssh-client && \
curl -SsfL "https://github.com/opentofu/opentofu/releases/download/${TOFU_VERSION}/tofu_${TOFU_VERSION##v}_linux_amd64.zip" | \
bsdtar -xf - -C /usr/local/bin tofu && \
chmod 755 /usr/local/bin/tofu && \

View File

@ -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,8 +19,7 @@ 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.21.x
XGO_VERSION := go-1.22.x
XGO_TARGETS ?= linux/amd64,linux/arm64
TARGETOS ?= linux
@ -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:

View File

@ -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
}

View File

@ -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-options",
Usage: "tofu init command options, see https://opentofu.org/docs/cli/commands/init/",
EnvVars: []string{"PLUGIN_INIT_OPTIONS"},
Category: category,
},
&cli.StringFlag{
Name: "fmt-options",
Usage: "options for the fmt command, see https://opentofu.org/docs/cli/commands/fmt/",
EnvVars: []string{"PLUGIN_FMT_OPTIONS"},
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,
},
}
}

View File

@ -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()
}

View File

@ -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 -}}

View File

@ -55,5 +55,9 @@ docker build --file Containerfile.multiarch --tag thegeeklab/wp-opentofu .
## Test
```Shell
docker run --rm \
-e PLUGIN_ACTION=validate \
-v $(pwd):/build:z \
-w /build \
thegeeklab/wp-opentofu
```

View File

@ -7,18 +7,32 @@ properties:
defaultValue: "validate,plan,apply"
required: false
- name: fmt_options
- name: fmt_option
description: |
Options for the fmt command, see the OpenTofu [fmt command](https://opentofu.org/docs/cli/commands/fmt/) documentation.
type: string
required: false
- name: init_options
- name: init_option
description: |
Tofu init command options, see the OpenTofu [init command](https://opentofu.org/docs/cli/commands/init/) documentation.
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.

22
go.mod
View File

@ -1,19 +1,20 @@
module github.com/thegeeklab/wp-opentofu
go 1.21
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.5.0
github.com/urfave/cli/v2 v2.27.1
golang.org/x/sys v0.16.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.17.0 // indirect
golang.org/x/net v0.19.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
View File

@ -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.5.0 h1:fA/kQwQrntjxdxePKKGecMRnPbj1xwpy6D2JrrPD2qA=
github.com/thegeeklab/wp-plugin-go v1.5.0/go.mod h1:ULBD5SNC7bkIB/UbLMQpcPWj7iZObCTWg8bqXf1zqsA=
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.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
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.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
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.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.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
View 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)
}
}

View File

@ -2,11 +2,13 @@ package plugin
import (
"context"
"encoding/json"
"errors"
"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 (
@ -24,6 +26,10 @@ const (
//nolint:revive
func (p *Plugin) run(ctx context.Context) error {
if err := p.FlagsFromContext(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
if err := p.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
@ -35,6 +41,28 @@ func (p *Plugin) run(ctx context.Context) error {
return nil
}
func (p *Plugin) FlagsFromContext() error {
if p.Context.String("init-option") != "" {
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.Tofu.InitOptions = initOptions
}
if p.Context.String("fmt-option") != "" {
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.Tofu.FmtOptions = fmtOptions
}
return nil
}
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
p.Settings.DataDir = ".terraform"
@ -42,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
@ -52,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 {
@ -59,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)
}

View File

@ -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,
},
}
}

View File

@ -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,
}
}

View File

@ -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
View 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
View 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)
}
})
}
}