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
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)
}
}

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
}
// 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
}

View File

@ -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,
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:]...)
}
}