Skip to content

Commit

Permalink
docs: hide system flags and refactor generator (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored May 4, 2024
1 parent 3735668 commit 4776687
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 238 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
58 changes: 0 additions & 58 deletions cmd/wp-opentofu/docs.go

This file was deleted.

76 changes: 0 additions & 76 deletions cmd/wp-opentofu/flags.go

This file was deleted.

15 changes: 1 addition & 14 deletions cmd/wp-opentofu/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package main

import (
"fmt"

"github.com/thegeeklab/wp-opentofu/plugin"

wp "github.com/thegeeklab/wp-plugin-go/plugin"
)

//nolint:gochecknoglobals
Expand All @@ -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()
}
18 changes: 0 additions & 18 deletions cmd/wp-opentofu/templates/docs-data.yaml.tmpl

This file was deleted.

14 changes: 14 additions & 0 deletions docs/data/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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.7.1
github.com/thegeeklab/wp-plugin-go/v2 v2.0.0
github.com/urfave/cli/v2 v2.27.2
golang.org/x/sys v0.19.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
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 v1.7.1 h1:zfR/rfNPuyVhXJu1fsLfp4+Mz2pTf6WwW/mIqw9750I=
github.com/thegeeklab/wp-plugin-go v1.7.1/go.mod h1:Ixi5plt9tpFGTu6yc/Inm5DcDpp3xPTeohfr86gf2EU=
github.com/thegeeklab/wp-plugin-go/v2 v2.0.0 h1:7dChME92qqpdtvDM4O2FZOoRv3jfkjlHCw/ijbE9SxQ=
github.com/thegeeklab/wp-plugin-go/v2 v2.0.0/go.mod h1:KRfDolkPSpO7Zx54Y0ofTFA7Cvd+7bHTHzYnYAo9WYg=
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=
Expand Down
42 changes: 42 additions & 0 deletions internal/docs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build generate
// +build generate

package main

import (
"context"
"flag"
"net/http"
"os"
"time"

"github.com/thegeeklab/wp-opentofu/plugin"
"github.com/thegeeklab/wp-plugin-go/v2/docs"
wp_template "github.com/thegeeklab/wp-plugin-go/v2/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 := wp_template.Render(context.Background(), client, tmpl, 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)
}
}
46 changes: 25 additions & 21 deletions plugin/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"

"github.com/thegeeklab/wp-plugin-go/trace"
"github.com/thegeeklab/wp-plugin-go/v2/trace"
)

var (
Expand Down Expand Up @@ -79,34 +80,33 @@ func (p *Plugin) Validate() error {

// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
batchCmd := make([]*Cmd, 0)
batchCmd = append(batchCmd, p.versionCommand())

if p.Settings.TofuVersion != "" {
err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize)
if err != nil {
return err
}
}

commands := []*pluginCommand{
p.versionCommand(),
}

commands = append(commands, p.initCommand())
commands = append(commands, p.getModulesCommand())
batchCmd = append(batchCmd, p.initCommand())
batchCmd = append(batchCmd, p.getModulesCommand())

for _, action := range p.Settings.Action.Value() {
switch action {
case "fmt":
commands = append(commands, p.fmtCommand())
batchCmd = append(batchCmd, p.fmtCommand())
case "validate":
commands = append(commands, p.validateCommand())
batchCmd = append(batchCmd, p.validateCommand())
case "plan":
commands = append(commands, p.planCommand(false))
batchCmd = append(batchCmd, p.planCommand(false))
case "plan-destroy":
commands = append(commands, p.planCommand(true))
batchCmd = append(batchCmd, p.planCommand(true))
case "apply":
commands = append(commands, p.applyCommand())
batchCmd = append(batchCmd, p.applyCommand())
case "destroy":
commands = append(commands, p.destroyCommand())
batchCmd = append(batchCmd, p.destroyCommand())
default:
return fmt.Errorf("%w: %s", ErrActionUnknown, action)
}
Expand All @@ -116,18 +116,22 @@ func (p *Plugin) Execute() error {
return err
}

for _, command := range commands {
command.cmd.Stdout = os.Stdout
command.cmd.Stderr = os.Stderr
command.cmd.Env = os.Environ()
for _, bc := range batchCmd {
bc.Stdout = os.Stdout
bc.Stderr = os.Stderr
trace.Cmd(bc.Cmd)

if p.Settings.RootDir != "" {
command.cmd.Dir = p.Settings.RootDir
bc.Env = os.Environ()

if bc.Private {
bc.Stdout = io.Discard
}

trace.Cmd(command.cmd)
if p.Settings.RootDir != "" {
bc.Dir = p.Settings.RootDir
}

if err := command.cmd.Run(); err != nil {
if err := bc.Run(); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit 4776687

Please sign in to comment.