From dd50c4f3856730f1876dd97ad3b961a68ea786b1 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 5 Mar 2024 16:38:18 +0100 Subject: [PATCH] A bit of cleaning up --- cmd/data/articles.go | 26 ++++++++++---------------- cmd/data/db.go | 9 +++------ cmd/data/rss.go | 15 +++++---------- cmd/data/sessions.go | 11 +++-------- 4 files changed, 21 insertions(+), 40 deletions(-) diff --git a/cmd/data/articles.go b/cmd/data/articles.go index 4733ab8..e00eabd 100644 --- a/cmd/data/articles.go +++ b/cmd/data/articles.go @@ -37,7 +37,7 @@ type TagList struct { wg sync.WaitGroup } -func minArticleList() *ArticleList { +func initArticleList() *ArticleList { return &ArticleList{ addCh: make(chan *Article), delCh: make(chan uuid.UUID), @@ -46,7 +46,7 @@ func minArticleList() *ArticleList { } } -func minTagList() *TagList { +func initTagList() *TagList { return &TagList{ addCh: make(chan string), getCh: make(chan []string), @@ -89,7 +89,7 @@ func (tl *TagList) start() { } func NewArticleList() *ArticleList { - list := minArticleList() + list := initArticleList() list.articles = []*Article{} list.wg.Add(1) @@ -106,6 +106,7 @@ func (al *ArticleList) Add(a *Article) { func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) { al.delCh <- uuid article := <-al.retCh + if article == nil { return nil, false } @@ -123,10 +124,8 @@ func (al *ArticleList) Save(filename string) error { } defer file.Close() - encoder := gob.NewEncoder(file) articles := al.Get() - err = encoder.Encode(articles) - if err != nil { + if err = gob.NewEncoder(file).Encode(articles); err != nil { return fmt.Errorf("error ecoding key: %v", err) } @@ -139,10 +138,8 @@ func LoadArticleList(filename string) (*ArticleList, error) { return nil, fmt.Errorf("error opening key file: %v", err) } - decoder := gob.NewDecoder(file) articleList := NewArticleList() - err = decoder.Decode(&articleList.articles) - if err != nil { + if err = gob.NewDecoder(file).Decode(&articleList.articles); err != nil { return nil, fmt.Errorf("error decoding key: %v", err) } @@ -150,7 +147,7 @@ func LoadArticleList(filename string) (*ArticleList, error) { } func NewTagList() *TagList { - list := minTagList() + list := initTagList() list.tags = []string{} list.wg.Add(1) @@ -175,10 +172,8 @@ func (tl *TagList) Save(filename string) error { } defer file.Close() - encoder := gob.NewEncoder(file) tags := tl.Get() - err = encoder.Encode(tags) - if err != nil { + if err = gob.NewEncoder(file).Encode(tags); err != nil { return fmt.Errorf("error ecoding key: %v", err) } @@ -190,11 +185,10 @@ func LoadTagList(filename string) (*TagList, error) { if err != nil { return nil, fmt.Errorf("error opening key file: %v", err) } + defer file.Close() - decoder := gob.NewDecoder(file) tagList := NewTagList() - err = decoder.Decode(&tagList.tags) - if err != nil { + if err = gob.NewDecoder(file).Decode(&tagList.tags); err != nil { return nil, fmt.Errorf("error decoding key: %v", err) } diff --git a/cmd/data/db.go b/cmd/data/db.go index a631d28..0a260e9 100644 --- a/cmd/data/db.go +++ b/cmd/data/db.go @@ -45,8 +45,7 @@ func (db *DB) AddUser(user *User, pass string) error { (username, password, first_name, last_name, role) VALUES (?, ?, ?, ?, ?) ` - _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role) - if err != nil { + if _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role); err != nil { return fmt.Errorf("error inserting user into DB: %v", err) } @@ -104,8 +103,7 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error { SET password = ? WHERE id = ? ` - _, err = db.Exec(query, string(newHashedPass), id) - if err != nil { + if _, err = db.Exec(query, string(newHashedPass), id); err != nil { return fmt.Errorf("error updating password in DB: %v", err) } @@ -134,8 +132,7 @@ func (db *DB) GetUser(id int64) (*User, error) { ` row := db.QueryRow(query, id) - if err := row.Scan(&user.ID, &user.UserName, &user.FirstName, - &user.LastName, &user.Role); err != nil { + if err := row.Scan(&user.ID, &user.UserName, &user.FirstName, &user.LastName, &user.Role); err != nil { return nil, fmt.Errorf("error reading user information: %v", err) } diff --git a/cmd/data/rss.go b/cmd/data/rss.go index 3801f10..ed3c326 100644 --- a/cmd/data/rss.go +++ b/cmd/data/rss.go @@ -17,7 +17,7 @@ type Feed struct { wg sync.WaitGroup } -func minFeed() *Feed { +func initFeed() *Feed { return &Feed{ addCh: make(chan *feeds.Item), setCh: make(chan feeds.Feed), @@ -38,7 +38,7 @@ func (f *Feed) start() { } func NewFeed(title, link, desc string) *Feed { - feed := minFeed() + feed := initFeed() feed.feed = feeds.Feed{ Title: title, Link: &feeds.Link{Href: link}, @@ -67,20 +67,17 @@ func OpenFeed(filename string) (*Feed, error) { } defer file.Close() - feed := minFeed() + feed := initFeed() feed.wg.Add(1) go feed.start() feed.wg.Wait() - decoder := gob.NewDecoder(file) tmpFeed := new(feeds.Feed) - err = decoder.Decode(tmpFeed) - if err != nil { + if err = gob.NewDecoder(file).Decode(tmpFeed); err != nil { return nil, fmt.Errorf("error decoding file %v: %v", filename, err) } feed.Set(*tmpFeed) - return feed, nil } @@ -91,10 +88,8 @@ func (f *Feed) Save(filename string) error { } defer file.Close() - encoder := gob.NewEncoder(file) feed := f.Get() - err = encoder.Encode(feed) - if err != nil { + if err = gob.NewEncoder(file).Encode(feed); err != nil { return fmt.Errorf("error encoding file %v: %v", filename, err) } diff --git a/cmd/data/sessions.go b/cmd/data/sessions.go index d8458c0..81bd283 100644 --- a/cmd/data/sessions.go +++ b/cmd/data/sessions.go @@ -17,8 +17,7 @@ type CookieStore struct { func NewKey() ([]byte, error) { key := make([]byte, 32) - _, err := io.ReadFull(rand.Reader, key) - if err != nil { + if _, err := io.ReadFull(rand.Reader, key); err != nil { return nil, fmt.Errorf("error generating key: %v", err) } @@ -33,9 +32,7 @@ func SaveKey(key []byte, filename string) error { defer file.Close() file.Chmod(0600) - encoder := gob.NewEncoder(file) - err = encoder.Encode(key) - if err != nil { + if err = gob.NewEncoder(file).Encode(key); err != nil { return fmt.Errorf("error ecoding key: %v", err) } @@ -49,9 +46,7 @@ func LoadKey(filename string) ([]byte, error) { } key := make([]byte, 32) - decoder := gob.NewDecoder(file) - err = decoder.Decode(&key) - if err != nil { + if err = gob.NewDecoder(file).Decode(&key); err != nil { return nil, fmt.Errorf("error decoding key: %v", err) }