From c7b2307b2da768661e1c328e4cc27d45ea53ce28 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Thu, 21 Sep 2023 13:01:35 +0200 Subject: [PATCH] refactor: drop pgk/errors package (#173) --- go.mod | 1 - go.sum | 2 -- plugin/ansible.go | 22 ++++++++++++---------- plugin/impl.go | 11 ++++++++--- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 8d5a787..3709389 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.21 require ( github.com/drone-plugins/drone-plugin-lib v0.4.1 github.com/joho/godotenv v1.5.1 - github.com/pkg/errors v0.9.1 github.com/urfave/cli/v2 v2.25.7 golang.org/x/sys v0.12.0 ) diff --git a/go.sum b/go.sum index 60c1171..85ee658 100644 --- a/go.sum +++ b/go.sum @@ -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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= 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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= diff --git a/plugin/ansible.go b/plugin/ansible.go index 9aacb43..531500a 100644 --- a/plugin/ansible.go +++ b/plugin/ansible.go @@ -1,13 +1,13 @@ package plugin import ( + "errors" "fmt" "os" "path/filepath" "strconv" "strings" - "github.com/pkg/errors" "github.com/urfave/cli/v2" "golang.org/x/sys/execabs" ) @@ -31,13 +31,15 @@ const ansibleContent = ` host_key_checking = False ` +var ErrAnsiblePlaybookNotFound = errors.New("playbook not found") + func (p *Plugin) ansibleConfig() error { 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 { - return errors.Wrap(err, "failed to create ansible config") + return fmt.Errorf("failed to create ansible config: %w", err) } return nil @@ -46,15 +48,15 @@ func (p *Plugin) ansibleConfig() error { func (p *Plugin) privateKey() error { tmpfile, err := os.CreateTemp("", "privateKey") 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 { - 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 { - 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() @@ -65,15 +67,15 @@ func (p *Plugin) privateKey() error { func (p *Plugin) vaultPass() error { tmpfile, err := os.CreateTemp("", "vaultPass") 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 { - 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 { - 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() @@ -96,7 +98,7 @@ func (p *Plugin) playbooks() error { } if len(playbooks) == 0 { - return errors.New("failed to find playbook files") + return ErrAnsiblePlaybookNotFound } p.settings.Playbooks = *cli.NewStringSlice(playbooks...) diff --git a/plugin/impl.go b/plugin/impl.go index 7dddf97..95d7daa 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -1,9 +1,9 @@ package plugin import ( + "errors" "os" - "github.com/pkg/errors" "github.com/urfave/cli/v2" "golang.org/x/sys/execabs" ) @@ -47,14 +47,19 @@ type Settings struct { BecomeUser string } +var ( + ErrPluginPlaybookNotSet = errors.New("playbook is required") + ErrPluginInventoryNotSet = errors.New("inventory is required") +) + // Validate handles the settings validation of the plugin. func (p *Plugin) Validate() error { if len(p.settings.Playbooks.Value()) == 0 { - return errors.New("you must provide a playbook") + return ErrPluginPlaybookNotSet } if len(p.settings.Inventories.Value()) == 0 { - return errors.New("you must provide an inventory") + return ErrPluginInventoryNotSet } return nil