From 36be8f504dda431074beaeb198ade3932b6877b0 Mon Sep 17 00:00:00 2001 From: dkaslovsky Date: Sun, 26 Nov 2023 20:08:58 -0700 Subject: [PATCH] remove use of errors.Wrapf --- cmd/config/config.go | 5 ++--- cmd/open/open.go | 22 +++++++++++----------- pkg/archive/archive.go | 12 ++++++------ pkg/config/config.go | 12 ++++++------ pkg/template/archive.go | 5 ++--- pkg/template/template.go | 13 ++++++------- 6 files changed, 33 insertions(+), 36 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index 367bb77..c04cd2d 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -7,7 +7,6 @@ import ( "os" "github.com/dkaslovsky/textnote/pkg/config" - "github.com/pkg/errors" "github.com/spf13/cobra" "gopkg.in/yaml.v3" ) @@ -77,11 +76,11 @@ func displayConfigFile(configPath string) error { } f, err := os.Open(configPath) if err != nil { - return errors.Wrapf(err, "unable to open configuration file [%s]", configPath) + return fmt.Errorf("unable to open configuration file [%s]: %w", configPath, err) } c, err := io.ReadAll(f) if err != nil { - return errors.Wrapf(err, "unable to read configuration file [%s]", configPath) + return fmt.Errorf("unable to read configuration file [%s]: %w", configPath, err) } log.Print(string(c)) return nil diff --git a/cmd/open/open.go b/cmd/open/open.go index 66c3884..24d5da2 100644 --- a/cmd/open/open.go +++ b/cmd/open/open.go @@ -147,7 +147,7 @@ func setCopyDateOpt(cmdOpts *commandOptions, templateOpts config.Opts, getFiles if cmdOpts.copyDate != "" { if _, err := time.Parse(templateOpts.Cli.TimeFormat, cmdOpts.copyDate); err != nil { - return numFiles, errors.Wrapf(err, "cannot copy note from malformed date [%s]", cmdOpts.copyDate) + return numFiles, fmt.Errorf("cannot copy note from malformed date [%s]: %w", cmdOpts.copyDate, err) } return numFiles, nil } @@ -178,7 +178,7 @@ func setDeleteOpts(cmdOpts *commandOptions) { func run(templateOpts config.Opts, cmdOpts commandOptions) error { date, err := time.Parse(templateOpts.Cli.TimeFormat, cmdOpts.date) if err != nil { - return errors.Wrapf(err, "cannot create note for malformed date [%s]", cmdOpts.date) + return fmt.Errorf("cannot create note for malformed date [%s]: %w", cmdOpts.date, err) } t := template.NewTemplate(templateOpts, date) @@ -205,18 +205,18 @@ func run(templateOpts config.Opts, cmdOpts commandOptions) error { } copyDate, err := time.Parse(templateOpts.Cli.TimeFormat, cmdOpts.copyDate) if err != nil { - return errors.Wrapf(err, "cannot copy note from malformed date [%s]", cmdOpts.copyDate) + return fmt.Errorf("cannot copy note from malformed date [%s]: %w", cmdOpts.copyDate, err) } src := template.NewTemplate(templateOpts, copyDate) err = rw.Read(src) if err != nil { - return errors.Wrap(err, "cannot read source file for copy") + return fmt.Errorf("cannot read source file for copy: %w", err) } // load template contents if it exists if rw.Exists(t) { err := rw.Read(t) if err != nil { - return errors.Wrap(err, "cannot load template file") + return fmt.Errorf("cannot load template file: %w", err) } } // copy from source to template @@ -228,18 +228,18 @@ func run(templateOpts config.Opts, cmdOpts commandOptions) error { if cmdOpts.deleteSections { err = deleteSections(src, cmdOpts.sections) if err != nil { - return errors.Wrap(err, "failed to remove section content from source file") + return fmt.Errorf("failed to remove section content from source file: %w", err) } if cmdOpts.deleteEmpty && src.IsEmpty() { err = os.Remove(src.GetFilePath()) if err != nil { - return errors.Wrapf(err, "failed to remove empty source file") + return fmt.Errorf("failed to remove empty source file: %w", err) } } else { err = rw.Overwrite(src) if err != nil { - return errors.Wrap(err, "failed to save changes to source file") + return fmt.Errorf("failed to save changes to source file: %w", err) } } @@ -247,7 +247,7 @@ func run(templateOpts config.Opts, cmdOpts commandOptions) error { err = rw.Overwrite(t) if err != nil { - return errors.Wrap(err, "failed to write file") + return fmt.Errorf("failed to write file: %w", err) } return openInEditor(t, ed) } @@ -256,7 +256,7 @@ func copySections(src *template.Template, tgt *template.Template, sectionNames [ for _, sectionName := range sectionNames { err := tgt.CopySectionContents(src, sectionName) if err != nil { - return errors.Wrap(err, fmt.Sprintf("cannot copy section [%s] from source to target", sectionName)) + return fmt.Errorf("cannot copy section [%s] from source to target: %w", sectionName, err) } } return nil @@ -266,7 +266,7 @@ func deleteSections(t *template.Template, sectionNames []string) error { for _, sectionName := range sectionNames { err := t.DeleteSectionContents(sectionName) if err != nil { - return errors.Wrap(err, fmt.Sprintf("cannot delete section [%s] from template", sectionName)) + return fmt.Errorf("cannot delete section [%s] from template: %w", sectionName, err) } } return nil diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 84a6b2a..487911f 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -1,13 +1,13 @@ package archive import ( + "fmt" "log" "time" "github.com/dkaslovsky/textnote/pkg/config" "github.com/dkaslovsky/textnote/pkg/file" "github.com/dkaslovsky/textnote/pkg/template" - "github.com/pkg/errors" ) // Archiver consolidates templates into archives @@ -44,7 +44,7 @@ func (a *Archiver) Add(date time.Time) error { t := template.NewTemplate(a.opts, date) err := a.rw.Read(t) if err != nil { - return errors.Wrapf(err, "cannot add unreadable file [%s] to archive", t.GetFilePath()) + return fmt.Errorf("cannot add unreadable file [%s] to archive: %w", t.GetFilePath(), err) } monthKey := date.Format(a.opts.Archive.MonthTimeFormat) @@ -56,7 +56,7 @@ func (a *Archiver) Add(date time.Time) error { for _, section := range a.opts.Section.Names { err := archive.ArchiveSectionContents(t, section) if err != nil { - return errors.Wrapf(err, "cannot add contents from [%s] to archive", t.GetFilePath()) + return fmt.Errorf("cannot add contents from [%s] to archive: %w", t.GetFilePath(), err) } } @@ -71,17 +71,17 @@ func (a *Archiver) Write() error { existing := template.NewMonthArchiveTemplate(a.opts, t.GetDate()) err := a.rw.Read(existing) if err != nil { - return errors.Wrapf(err, "unable to open existing archive file [%s]", existing.GetFilePath()) + return fmt.Errorf("unable to open existing archive file [%s]: %w", existing.GetFilePath(), err) } err = t.Merge(existing) if err != nil { - return errors.Wrapf(err, "unable to from merge existing archive file [%s]", existing.GetFilePath()) + return fmt.Errorf("unable to from merge existing archive file [%s] %w", existing.GetFilePath(), err) } } err := a.rw.Overwrite(t) if err != nil { - return errors.Wrapf(err, "failed to write archive file [%s]", t.GetFilePath()) + return fmt.Errorf("failed to write archive file [%s]: %w", t.GetFilePath(), err) } log.Printf("wrote archive file [%s]", t.GetFilePath()) } diff --git a/pkg/config/config.go b/pkg/config/config.go index b45e0ad..adb2da7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -128,14 +128,14 @@ func Load() (Opts, error) { // parse config file allowing environment variable overrides err := loadFromEnv(GetConfigFilePath(), &opts) if err != nil { - return opts, errors.Wrap(err, "unable to read config file") + return opts, fmt.Errorf("unable to read config file: %w", err) } // overwrite defaults with opts from file/env defaults := getDefaultOpts() err = mergo.Merge(&opts, defaults) if err != nil { - return opts, errors.Wrap(err, "unable to integrate configuration from file with defaults") + return opts, fmt.Errorf("unable to integrate configuration from file with defaults: %w", err) } // set AppDir as read from environment @@ -143,7 +143,7 @@ func Load() (Opts, error) { err = ValidateOpts(opts) if err != nil { - return opts, errors.Wrapf(err, "configuration error in [%s]", fileName) + return opts, fmt.Errorf("configuration error in [%s]: %w", fileName, err) } return opts, nil @@ -157,7 +157,7 @@ func loadFromEnv(path string, opts *Opts) error { err = loadBackCompat(path, opts) if err != nil { - return errors.Wrapf(err, "unable to read config file for backwards compatibility fields") + return fmt.Errorf("unable to read config file for backwards compatibility fields: %w", err) } return nil @@ -189,11 +189,11 @@ func CreateIfNotExists() error { defaults := getDefaultOpts() yml, err := yaml.Marshal(defaults) if err != nil { - return errors.Wrap(err, "unable to generate config file") + return fmt.Errorf("unable to generate config file: %w", err) } err = os.WriteFile(configPath, yml, 0o644) if err != nil { - return errors.Wrap(err, fmt.Sprintf("unable to create configuration file: [%s]", configPath)) + return fmt.Errorf("unable to create configuration file [%s]: %w", configPath, err) } log.Printf("created default configuration file: [%s]", configPath) return nil diff --git a/pkg/template/archive.go b/pkg/template/archive.go index 63be989..e06a4c7 100644 --- a/pkg/template/archive.go +++ b/pkg/template/archive.go @@ -9,7 +9,6 @@ import ( "time" "github.com/dkaslovsky/textnote/pkg/config" - "github.com/pkg/errors" ) // MonthArchiveTemplate contains the structure of a month archive @@ -49,11 +48,11 @@ func (t *MonthArchiveTemplate) GetFilePath() string { func (t *MonthArchiveTemplate) ArchiveSectionContents(src *Template, sectionName string) error { tgtSec, err := t.getSection(sectionName) if err != nil { - return errors.Wrap(err, "failed to find section in target") + return fmt.Errorf("failed to find section in target: %w", err) } srcSec, err := src.getSection(sectionName) if err != nil { - return errors.Wrap(err, "failed to find section in source") + return fmt.Errorf("failed to find section in source: %w", err) } // flatten text from contents into a single string diff --git a/pkg/template/template.go b/pkg/template/template.go index 12c0a52..cdf4229 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -8,7 +8,6 @@ import ( "time" "github.com/dkaslovsky/textnote/pkg/config" - "github.com/pkg/errors" ) // Template contains the structure of a note @@ -69,11 +68,11 @@ type sectionGettable interface { func (t *Template) CopySectionContents(src sectionGettable, sectionName string) error { tgtSec, err := t.getSection(sectionName) if err != nil { - return errors.Wrap(err, "failed to find section in target") + return fmt.Errorf("failed to find section in target: %w", err) } srcSec, err := src.getSection(sectionName) if err != nil { - return errors.Wrap(err, "failed to find section in source") + return fmt.Errorf("failed to find section in source: %w", err) } tgtSec.contents = append(tgtSec.contents, srcSec.contents...) return nil @@ -83,7 +82,7 @@ func (t *Template) CopySectionContents(src sectionGettable, sectionName string) func (t *Template) DeleteSectionContents(sectionName string) error { sec, err := t.getSection(sectionName) if err != nil { - return errors.Wrap(err, "cannot delete section") + return fmt.Errorf("cannot delete section: %w", err) } sec.deleteContents() return nil @@ -103,13 +102,13 @@ func (t *Template) IsEmpty() bool { func (t *Template) Load(r io.Reader) error { raw, err := io.ReadAll(r) if err != nil { - return errors.Wrap(err, "error loading template") + return fmt.Errorf("error loading template: %w", err) } sectionText := string(raw) sectionNameRegex, err := getSectionNameRegex(t.opts.Section.Prefix, t.opts.Section.Suffix) if err != nil { - return errors.Wrap(err, "cannot parse sections") + return fmt.Errorf("cannot parse sections: %w", err) } sectionBoundaries := sectionNameRegex.FindAllStringSubmatchIndex(sectionText, -1) numSections := len(sectionBoundaries) @@ -127,7 +126,7 @@ func (t *Template) Load(r io.Reader) error { section, err := parseSection(sectionText[idxs[0]:curSecEnd], t.opts) if err != nil { - return errors.Wrap(err, "failed to parse section while reading textnote") + return fmt.Errorf("failed to parse section while reading textnote: %w", err) } idx, found := t.sectionIdx[section.name]