Switching to my laptop

This commit is contained in:
Brian Buller 2017-09-21 09:46:59 -05:00
parent 845521f1a8
commit 11770850b8
4 changed files with 152 additions and 22 deletions

70
main.go
View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"net/url"
"os" "os"
userConfig "github.com/br0xen/user-config" userConfig "github.com/br0xen/user-config"
@ -24,8 +26,6 @@ const (
OpError OpError
) )
var performOp = OpSearch
func main() { func main() {
err := initialize() err := initialize()
if err != nil { if err != nil {
@ -33,38 +33,73 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// mk is the Bookmark that we're adding/editing
var mk *Bookmark
parms := os.Args parms := os.Args
if len(parms) > 1 { if len(parms) > 1 {
// Trim the program name
parms = parms[1:] parms = parms[1:]
for _, v := range parms { // Parse all of the parameters
switch v { for i := range parms {
switch parms[i] {
case "-h": case "-h":
printUsage() printUsage()
os.Exit(0) os.Exit(0)
case "-a": case "-a":
// Adding a new bookmark // Adding a new bookmark
setOperation(OpAdd) mk, err = doOperation(OpAdd, parms[i+1:])
assertError(err)
case "-u": case "-u":
// Updating an existing bookmark // Updating an existing bookmark
setOperation(OpUpdate) mk, err = doOperation(OpUpdate, parms[i+1:])
assertError(err)
case "-d": case "-d":
// Delete an existing bookmark // Delete an existing bookmark
setOperation(OpDelete) mk, err = doOperation(OpDelete, parms[i+1:])
assertError(err)
} }
} }
_ = mk
} else { } else {
// Enter cui mode // Enter cui mode
fmt.Println("Entering CUI Mode") mainCui()
} }
} }
func setOperation(which int) { // doOperation performs the requested operation
if performOp != OpSearch || which >= OpError { // which is the constant that specifies the operation we're performing
// We've already tried to set the operation // parms is the slice of all remaining parameters passed on the cli
fmt.Println("Error parsing command. For help use '-h'.") func doOperation(which int, parms []string) (*Bookmark, error) {
os.Exit(1) 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 // 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)
}
}

7
main_cui.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "fmt"
func mainCui() {
fmt.Println("Entering CUI Mode")
}

View File

@ -70,7 +70,17 @@ func (mdb *MarkDB) initDatabase() error {
return nil return nil
} }
// getNextIndex returns the next // GetLastIndex returns the last used index for a bookmark
func (mdb *MarkDB) getNextIndex() int { // If we had a problem getting it, we return the error
return 0 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
} }

View File

@ -1,23 +1,94 @@
package main package main
import "time" import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/PuerkitoBio/goquery"
)
type Bookmark struct { type Bookmark struct {
Id int Id int
Name string Name string
Url string Url *url.URL
Desc string Desc string
Tags []string Tags []string
Created time.Time Created time.Time
LastUsed 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 // First, search for a bookmark with this same URL
// If we didn't find one, create a new bookmark struct // If we didn't find one, create a new bookmark struct
b := Bookmark{ b := Bookmark{
Id: 0, Id: 0,
Url: url, Url: url,
Created: time.Now(),
} }
return &b 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:]...)
}
}