From 11770850b844bdf05d797aca310e1bcc6810df91 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 21 Sep 2017 09:46:59 -0500 Subject: [PATCH] Switching to my laptop --- main.go | 70 ++++++++++++++++++++++++++++++++-------- main_cui.go | 7 ++++ model.go | 16 ++++++++-- model_bookmark.go | 81 ++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 main_cui.go diff --git a/main.go b/main.go index 913d15a..3dce0d3 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "errors" "fmt" + "net/url" "os" userConfig "github.com/br0xen/user-config" @@ -24,8 +26,6 @@ const ( OpError ) -var performOp = OpSearch - func main() { err := initialize() if err != nil { @@ -33,38 +33,73 @@ func main() { os.Exit(1) } + // mk is the Bookmark that we're adding/editing + var mk *Bookmark + parms := os.Args if len(parms) > 1 { + // Trim the program name parms = parms[1:] - for _, v := range parms { - switch v { + // Parse all of the parameters + for i := range parms { + switch parms[i] { case "-h": printUsage() os.Exit(0) case "-a": // Adding a new bookmark - setOperation(OpAdd) + mk, err = doOperation(OpAdd, parms[i+1:]) + assertError(err) case "-u": // Updating an existing bookmark - setOperation(OpUpdate) + mk, err = doOperation(OpUpdate, parms[i+1:]) + assertError(err) case "-d": // Delete an existing bookmark - setOperation(OpDelete) + mk, err = doOperation(OpDelete, parms[i+1:]) + assertError(err) } } + _ = mk } else { // Enter cui mode - fmt.Println("Entering CUI Mode") + mainCui() } } -func setOperation(which int) { - if performOp != OpSearch || which >= OpError { - // We've already tried to set the operation - fmt.Println("Error parsing command. For help use '-h'.") - os.Exit(1) +// doOperation performs the requested operation +// which is the constant that specifies the operation we're performing +// parms is the slice of all remaining parameters passed on the cli +func doOperation(which int, parms []string) (*Bookmark, error) { + var mk *Bookmark + + var nxt string + if len(parms) > 0 { + nxt = parms[0] } - performOp = which + switch which { + case OpAdd: + // nxt should be the url, make sure it's valid + fmt.Println("Adding Bookmark") + url, err := url.Parse(nxt) + if err != nil { + return nil, err + } + mk = NewBookmark(url) + if err = mk.DownloadDetail(); err != nil { + fmt.Println(err) + } + fmt.Println(mk.Desc) + case OpUpdate: + + case OpDelete: + + case OpSearch: + + default: + return nil, errors.New("Invalid operation") + } + return mk, nil } // initialize sets up the application for general use @@ -141,3 +176,10 @@ func printUsage() { } } } + +func assertError(err error) { + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } +} diff --git a/main_cui.go b/main_cui.go new file mode 100644 index 0000000..15a2322 --- /dev/null +++ b/main_cui.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func mainCui() { + fmt.Println("Entering CUI Mode") +} diff --git a/model.go b/model.go index de6da30..aa620e7 100644 --- a/model.go +++ b/model.go @@ -70,7 +70,17 @@ func (mdb *MarkDB) initDatabase() error { return nil } -// getNextIndex returns the next -func (mdb *MarkDB) getNextIndex() int { - return 0 +// GetLastIndex returns the last used index for a bookmark +// If we had a problem getting it, we return the error +func (mdb *MarkDB) GetLastIndex() (int, error) { + return mdb.db.GetInt([]string{"config"}, "lastIdx") +} + +// GetNextIndex returns the next available index for a bookmark +func (mdb *MarkDB) GetNextIndex() int { + idx, err := mdb.GetLastIndex() + if err != nil { + return -1 + } + return idx + 1 } diff --git a/model_bookmark.go b/model_bookmark.go index 4475aff..1ef6be4 100644 --- a/model_bookmark.go +++ b/model_bookmark.go @@ -1,23 +1,94 @@ package main -import "time" +import ( + "fmt" + "net/http" + "net/url" + "time" + + "github.com/PuerkitoBio/goquery" +) type Bookmark struct { Id int Name string - Url string + Url *url.URL Desc string Tags []string Created time.Time LastUsed time.Time } -func NewBookmark(url string) *Bookmark { +func NewBookmark(url *url.URL) *Bookmark { // First, search for a bookmark with this same URL // If we didn't find one, create a new bookmark struct b := Bookmark{ - Id: 0, - Url: url, + Id: 0, + Url: url, + Created: time.Now(), } return &b } + +func (b *Bookmark) DownloadDetail() error { + fmt.Println("Downloading Page: ", b.Url.String()) + resp, err := http.Get(b.Url.String()) + if err != nil { + return err + } + defer resp.Body.Close() + + var doc *goquery.Document + doc, err = goquery.NewDocumentFromResponse(resp) + if err != nil { + return err + } + title := doc.Find("title").Text() + fmt.Println("Bookmark Title: " + title) + metas := doc.Find("meta") + metas.Each(func(i int, s *goquery.Selection) { + // For each item, check the 'name' attr + if v, has := s.Attr("name"); has { + if v == "description" { + str := s.Get(i) + fmt.Println(str) + //b.Desc = s.Get(i) + } + } + }) + return nil +} + +func (b *Bookmark) SetName(nm string) { + b.Name = nm +} + +func (b *Bookmark) SetUrl(url *url.URL) { + b.Url = url +} + +func (b *Bookmark) SetDescription(desc string) { + b.Desc = desc +} + +func (b *Bookmark) AddTag(tg string) { + for _, v := range b.Tags { + if v == tg { + return + } + } + b.Tags = append(b.Tags, tg) +} + +func (b *Bookmark) RemoveTag(tg string) { + remId := -1 + for idx := range b.Tags { + if b.Tags[idx] == tg { + remId = idx + break + } + } + if remId >= 0 { + b.Tags = append(b.Tags[:remId], b.Tags[remId+1:]...) + } +}