0
0
mirror of https://github.com/thegeeklab/wp-ansible.git synced 2024-11-21 23:50:40 +00:00

refactor: drop pgk/errors package (#173)

This commit is contained in:
Robert Kaussow 2023-09-21 13:01:35 +02:00 committed by GitHub
parent 01b1ca20f3
commit c7b2307b2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 16 deletions

1
go.mod
View File

@ -5,7 +5,6 @@ go 1.21
require ( require (
github.com/drone-plugins/drone-plugin-lib v0.4.1 github.com/drone-plugins/drone-plugin-lib v0.4.1
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.25.7 github.com/urfave/cli/v2 v2.25.7
golang.org/x/sys v0.12.0 golang.org/x/sys v0.12.0
) )

2
go.sum
View File

@ -7,8 +7,6 @@ github.com/drone-plugins/drone-plugin-lib v0.4.1 h1:47rZlmcMpr1hSp+6Gl+1Z4t+efi/
github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM= github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=

View File

@ -1,13 +1,13 @@
package plugin package plugin
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/pkg/errors"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/sys/execabs" "golang.org/x/sys/execabs"
) )
@ -31,13 +31,15 @@ const ansibleContent = `
host_key_checking = False host_key_checking = False
` `
var ErrAnsiblePlaybookNotFound = errors.New("playbook not found")
func (p *Plugin) ansibleConfig() error { func (p *Plugin) ansibleConfig() error {
if err := os.MkdirAll(ansibleFolder, os.ModePerm); err != nil { if err := os.MkdirAll(ansibleFolder, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create ansible directory") return fmt.Errorf("failed to create ansible directory: %w", err)
} }
if err := os.WriteFile(ansibleConfig, []byte(ansibleContent), strictFilePerm); err != nil { if err := os.WriteFile(ansibleConfig, []byte(ansibleContent), strictFilePerm); err != nil {
return errors.Wrap(err, "failed to create ansible config") return fmt.Errorf("failed to create ansible config: %w", err)
} }
return nil return nil
@ -46,15 +48,15 @@ func (p *Plugin) ansibleConfig() error {
func (p *Plugin) privateKey() error { func (p *Plugin) privateKey() error {
tmpfile, err := os.CreateTemp("", "privateKey") tmpfile, err := os.CreateTemp("", "privateKey")
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create private key file") return fmt.Errorf("failed to create private key file: %w", err)
} }
if _, err := tmpfile.Write([]byte(p.settings.PrivateKey)); err != nil { if _, err := tmpfile.Write([]byte(p.settings.PrivateKey)); err != nil {
return errors.Wrap(err, "failed to write private key file") return fmt.Errorf("failed to write private key file: %w", err)
} }
if err := tmpfile.Close(); err != nil { if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close private key file") return fmt.Errorf("failed to close private key file: %w", err)
} }
p.settings.PrivateKeyFile = tmpfile.Name() p.settings.PrivateKeyFile = tmpfile.Name()
@ -65,15 +67,15 @@ func (p *Plugin) privateKey() error {
func (p *Plugin) vaultPass() error { func (p *Plugin) vaultPass() error {
tmpfile, err := os.CreateTemp("", "vaultPass") tmpfile, err := os.CreateTemp("", "vaultPass")
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create vault password file") return fmt.Errorf("failed to create vault password file: %w", err)
} }
if _, err := tmpfile.Write([]byte(p.settings.VaultPassword)); err != nil { if _, err := tmpfile.Write([]byte(p.settings.VaultPassword)); err != nil {
return errors.Wrap(err, "failed to write vault password file") return fmt.Errorf("failed to write vault password file: %w", err)
} }
if err := tmpfile.Close(); err != nil { if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close vault password file") return fmt.Errorf("failed to close vault password file: %w", err)
} }
p.settings.VaultPasswordFile = tmpfile.Name() p.settings.VaultPasswordFile = tmpfile.Name()
@ -96,7 +98,7 @@ func (p *Plugin) playbooks() error {
} }
if len(playbooks) == 0 { if len(playbooks) == 0 {
return errors.New("failed to find playbook files") return ErrAnsiblePlaybookNotFound
} }
p.settings.Playbooks = *cli.NewStringSlice(playbooks...) p.settings.Playbooks = *cli.NewStringSlice(playbooks...)

View File

@ -1,9 +1,9 @@
package plugin package plugin
import ( import (
"errors"
"os" "os"
"github.com/pkg/errors"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/sys/execabs" "golang.org/x/sys/execabs"
) )
@ -47,14 +47,19 @@ type Settings struct {
BecomeUser string BecomeUser string
} }
var (
ErrPluginPlaybookNotSet = errors.New("playbook is required")
ErrPluginInventoryNotSet = errors.New("inventory is required")
)
// Validate handles the settings validation of the plugin. // Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error { func (p *Plugin) Validate() error {
if len(p.settings.Playbooks.Value()) == 0 { if len(p.settings.Playbooks.Value()) == 0 {
return errors.New("you must provide a playbook") return ErrPluginPlaybookNotSet
} }
if len(p.settings.Inventories.Value()) == 0 { if len(p.settings.Inventories.Value()) == 0 {
return errors.New("you must provide an inventory") return ErrPluginInventoryNotSet
} }
return nil return nil