Skip to content

Commit 382d460

Browse files
authored
refactor: clean up code (#25)
* refactor: remove unnecessary packages * fix: list sizing on toggle * refactor: entry.Model to entry.Entry for Gorm * refactor: remove unused vars * feat: remove final render * fix: fix render on quit * chore: add goreleaser * refactor: remove unused selectProjectCmd * chore: update dependencies
1 parent 470ccf0 commit 382d460

File tree

17 files changed

+241
-295
lines changed

17 files changed

+241
-295
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ bin/
44
*.md
55
*.pdf
66
debug.log
7+
8+
dist/

.goreleaser.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
before:
4+
hooks:
5+
# You may remove this if you don't use go modules.
6+
- go mod tidy
7+
# you may remove this if you don't need go generate
8+
- go generate ./...
9+
builds:
10+
- env:
11+
# go-sqlite3 requires cgo
12+
- CGO_ENABLED=1
13+
goos:
14+
- linux
15+
- windows
16+
- darwin
17+
archives:
18+
- replacements:
19+
darwin: Darwin
20+
linux: Linux
21+
windows: Windows
22+
386: i386
23+
amd64: x86_64
24+
checksum:
25+
name_template: 'checksums.txt'
26+
snapshot:
27+
name_template: "{{ incpatch .Version }}-next"
28+
changelog:
29+
sort: asc
30+
filters:
31+
exclude:
32+
- '^docs:'
33+
- '^test:'
34+
35+
# modelines, feel free to remove those if you don't want/use them:
36+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
37+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

entry/entry.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package entry
22

33
import (
4-
"github.com/pkg/errors"
54
"gorm.io/gorm"
65
)
76

@@ -28,29 +27,26 @@ type GormRepository struct {
2827
// DeleteEntryByID delete an entry by its ID
2928
func (g *GormRepository) DeleteEntryByID(entryID uint) error {
3029
result := g.DB.Delete(&Entry{}, entryID)
31-
resultErr := result.Error
32-
if resultErr != nil {
33-
return errors.Wrap(resultErr, cannotDeleteEntry)
34-
}
35-
return nil
30+
return result.Error
3631
}
3732

33+
// TODO: unused
3834
// DeleteEntries delete all entries for a given project
3935
func (g *GormRepository) DeleteEntries(projectID uint) error {
4036
result := g.DB.Where("project_id = ?", projectID).Delete(&Entry{})
41-
return errors.Wrap(result.Error, cannotDeleteEntry)
37+
return result.Error
4238
}
4339

4440
// GetEntriesByProjectID get all entries for a given project
4541
func (g *GormRepository) GetEntriesByProjectID(projectID uint) ([]Entry, error) {
4642
var Entries []Entry
4743
result := g.DB.Where("project_id = ?", projectID).Find(&Entries)
48-
return Entries, errors.Wrap(result.Error, cannotFindProject)
44+
return Entries, result.Error
4945
}
5046

5147
// CreateEntry create a new entry in the database
5248
func (g *GormRepository) CreateEntry(message []byte, projectID uint) error {
5349
entry := Entry{Message: string(message[:]), ProjectID: projectID}
5450
result := g.DB.Create(&entry)
55-
return errors.Wrap(result.Error, cannotCreateEntry)
51+
return result.Error
5652
}

entry/entry_output.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7-
8-
"github.com/pkg/errors"
97
)
108

119
const divider = "---"
@@ -24,10 +22,11 @@ func FormatEntry(entry Entry) string {
2422
return fmt.Sprintf("ID: %d\nCreated: %s\nMessage:\n\n %s\n %s\n", entry.ID, entry.CreatedAt.Format("2006-01-02"), entry.Message, divider)
2523
}
2624

27-
func ReverseEntries(entries []Entry) []Entry {
25+
// ReverseList reverse the provided list
26+
func ReverseList(list []Entry) []Entry {
2827
var output []Entry
29-
for i := len(entries) - 1; i > 0; i-- {
30-
output = append(output, entries[i])
28+
for i := len(list) - 1; i >= 0; i-- {
29+
output = append(output, list[i])
3130
}
3231
return output
3332
}
@@ -36,15 +35,15 @@ func ReverseEntries(entries []Entry) []Entry {
3635
func OutputEntriesToMarkdown(entries []Entry) error {
3736
file, err := os.OpenFile("./output.md", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
3837
if err != nil {
39-
return errors.Wrap(err, errCannotCreateFile)
38+
return fmt.Errorf("Cannot create file: %v", err)
4039
}
4140
defer func() {
4241
err = file.Close() // want defer as close to acquisition of resources as possible
4342
}()
4443
output := FormattedOutputFromEntries(entries)
4544
_, err = file.Write(output)
4645
if err != nil {
47-
return errors.Wrap(err, errCannotSaveFile)
46+
return fmt.Errorf("Cannot save file: %v", err)
4847
}
4948
return err
5049
}
@@ -55,7 +54,7 @@ func OutputEntriesToPDF(entries []Entry) error {
5554
pandoc := exec.Command("pandoc", "-s", "-o", "output.pdf") // c is going to run pandoc, so I'm assigning the pipe to c
5655
wc, wcerr := pandoc.StdinPipe() // io.WriteCloser, err
5756
if wcerr != nil {
58-
return errors.Wrap(wcerr, errPandoc)
57+
return fmt.Errorf("Cannot stdin to pandoc: %v", wcerr)
5958
}
6059
goerr := make(chan error)
6160
done := make(chan bool)
@@ -70,11 +69,11 @@ func OutputEntriesToPDF(entries []Entry) error {
7069
close(done)
7170
}()
7271
if err := <-goerr; err != nil {
73-
return errors.Wrap(err, errCannotWriteToFilePandoc)
72+
return fmt.Errorf("Cannot write file to pandoc: %v", err)
7473
}
7574
err := pandoc.Run()
7675
if err != nil {
77-
return errors.Wrap(err, errCannotRunPandoc)
76+
return fmt.Errorf("Cannot run pandoc: %v", err)
7877
}
7978
return nil
8079
}

entry/errors.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

go.mod

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,42 @@ module github.com/bashbunni/project-management
33
go 1.17
44

55
require (
6-
github.com/charmbracelet/bubbles v0.13.0
6+
github.com/charmbracelet/bubbles v0.14.0
77
github.com/charmbracelet/bubbletea v0.22.1
88
github.com/charmbracelet/glamour v0.5.0
9-
github.com/charmbracelet/lipgloss v0.5.0
9+
github.com/charmbracelet/lipgloss v0.6.0
1010
github.com/pkg/errors v0.9.1
11-
gorm.io/driver/sqlite v1.3.2
12-
gorm.io/gorm v1.23.6
11+
gorm.io/driver/sqlite v1.4.3
12+
gorm.io/gorm v1.24.0
1313
)
1414

1515
require (
1616
github.com/alecthomas/chroma v0.10.0 // indirect
1717
github.com/atotto/clipboard v0.1.4 // indirect
18+
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
1819
github.com/aymerick/douceur v0.2.0 // indirect
1920
github.com/containerd/console v1.0.3 // indirect
20-
github.com/dlclark/regexp2 v1.4.0 // indirect
21+
github.com/dlclark/regexp2 v1.7.0 // indirect
2122
github.com/gorilla/css v1.0.0 // indirect
2223
github.com/jinzhu/inflection v1.0.0 // indirect
2324
github.com/jinzhu/now v1.1.5 // indirect
2425
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2526
github.com/mattn/go-isatty v0.0.16 // indirect
2627
github.com/mattn/go-localereader v0.0.1 // indirect
27-
github.com/mattn/go-runewidth v0.0.13 // indirect
28-
github.com/mattn/go-sqlite3 v1.14.12 // indirect
29-
github.com/microcosm-cc/bluemonday v1.0.18 // indirect
28+
github.com/mattn/go-runewidth v0.0.14 // indirect
29+
github.com/mattn/go-sqlite3 v1.14.16 // indirect
30+
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
3031
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
3132
github.com/muesli/cancelreader v0.2.2 // indirect
3233
github.com/muesli/reflow v0.3.0 // indirect
33-
github.com/muesli/termenv v0.12.0 // indirect
34+
github.com/muesli/termenv v0.13.0 // indirect
3435
github.com/olekukonko/tablewriter v0.0.5 // indirect
35-
github.com/rivo/uniseg v0.3.4 // indirect
36+
github.com/rivo/uniseg v0.4.2 // indirect
3637
github.com/sahilm/fuzzy v0.1.0 // indirect
37-
github.com/yuin/goldmark v1.4.12 // indirect
38+
github.com/yuin/goldmark v1.5.2 // indirect
3839
github.com/yuin/goldmark-emoji v1.0.1 // indirect
39-
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
40-
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
41-
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
42-
golang.org/x/text v0.3.7 // indirect
40+
golang.org/x/net v0.1.0 // indirect
41+
golang.org/x/sys v0.1.0 // indirect
42+
golang.org/x/term v0.1.0 // indirect
43+
golang.org/x/text v0.4.0 // indirect
4344
)

0 commit comments

Comments
 (0)