Skip to content

Commit

Permalink
Merge branch 'remove_wrapf'
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaslovsky committed Nov 27, 2023
2 parents 723bf52 + 36be8f5 commit 154eeae
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 36 deletions.
5 changes: 2 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/dkaslovsky/textnote/pkg/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions cmd/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -228,26 +228,26 @@ 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)
}

}
}

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)
}
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -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())
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,22 @@ 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
opts.AppDir = appDir

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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/template/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/dkaslovsky/textnote/pkg/config"
"github.com/pkg/errors"
)

// MonthArchiveTemplate contains the structure of a month archive
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/dkaslovsky/textnote/pkg/config"
"github.com/pkg/errors"
)

// Template contains the structure of a note
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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]
Expand Down

0 comments on commit 154eeae

Please sign in to comment.