2017-09-20 22:12:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-21 14:46:59 +00:00
|
|
|
"errors"
|
2017-09-20 22:12:17 +00:00
|
|
|
"fmt"
|
2017-09-21 14:46:59 +00:00
|
|
|
"net/url"
|
2017-09-20 22:12:17 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
userConfig "github.com/br0xen/user-config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const AppName = "mark"
|
|
|
|
const DefDBName = AppName + ".db"
|
|
|
|
|
|
|
|
var mdb *MarkDB
|
|
|
|
var cfg *userConfig.Config
|
|
|
|
|
|
|
|
// Valid command line options
|
|
|
|
var validFlags []cliFlag
|
|
|
|
|
2017-09-20 23:04:39 +00:00
|
|
|
const (
|
|
|
|
OpSearch = iota
|
|
|
|
OpAdd
|
|
|
|
OpUpdate
|
|
|
|
OpDelete
|
|
|
|
OpError
|
|
|
|
)
|
|
|
|
|
2017-09-20 22:12:17 +00:00
|
|
|
func main() {
|
|
|
|
err := initialize()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-21 14:46:59 +00:00
|
|
|
// mk is the Bookmark that we're adding/editing
|
|
|
|
var mk *Bookmark
|
|
|
|
|
2017-09-20 23:04:39 +00:00
|
|
|
parms := os.Args
|
|
|
|
if len(parms) > 1 {
|
2017-09-21 14:46:59 +00:00
|
|
|
// Trim the program name
|
2017-09-20 23:04:39 +00:00
|
|
|
parms = parms[1:]
|
2017-09-21 14:46:59 +00:00
|
|
|
// Parse all of the parameters
|
|
|
|
for i := range parms {
|
|
|
|
switch parms[i] {
|
2017-09-20 23:04:39 +00:00
|
|
|
case "-h":
|
|
|
|
printUsage()
|
|
|
|
os.Exit(0)
|
|
|
|
case "-a":
|
|
|
|
// Adding a new bookmark
|
2017-09-21 14:46:59 +00:00
|
|
|
mk, err = doOperation(OpAdd, parms[i+1:])
|
|
|
|
assertError(err)
|
2017-09-20 23:04:39 +00:00
|
|
|
case "-u":
|
|
|
|
// Updating an existing bookmark
|
2017-09-21 14:46:59 +00:00
|
|
|
mk, err = doOperation(OpUpdate, parms[i+1:])
|
|
|
|
assertError(err)
|
2017-09-20 23:04:39 +00:00
|
|
|
case "-d":
|
|
|
|
// Delete an existing bookmark
|
2017-09-21 14:46:59 +00:00
|
|
|
mk, err = doOperation(OpDelete, parms[i+1:])
|
|
|
|
assertError(err)
|
2017-09-20 23:04:39 +00:00
|
|
|
}
|
2017-09-20 22:12:17 +00:00
|
|
|
}
|
2017-09-21 14:46:59 +00:00
|
|
|
_ = mk
|
2017-09-20 22:12:17 +00:00
|
|
|
} else {
|
|
|
|
// Enter cui mode
|
2017-09-21 14:46:59 +00:00
|
|
|
mainCui()
|
2017-09-20 22:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 14:46:59 +00:00
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
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")
|
2017-09-20 23:04:39 +00:00
|
|
|
}
|
2017-09-21 14:46:59 +00:00
|
|
|
return mk, nil
|
2017-09-20 23:04:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 22:12:17 +00:00
|
|
|
// initialize sets up the application for general use
|
|
|
|
func initialize() error {
|
|
|
|
var err error
|
|
|
|
// Build the list of valid command line options
|
|
|
|
addValidFlag("-h", "help", []string{"Print the usage (this message)"})
|
2017-09-20 23:04:39 +00:00
|
|
|
addValidFlag("-a", "add", []string{"Add a new bookmark"})
|
|
|
|
addValidFlag("-u", "update", []string{"Update an existing bookmark"})
|
|
|
|
addValidFlag("-d", "delete", []string{"Delete an existing bookmark"})
|
|
|
|
addValidFlag("-s", "search", []string{"Search for a bookmark"})
|
|
|
|
addValidFlag("-n", "name (optional)", []string{
|
2017-09-20 22:12:17 +00:00
|
|
|
"When adding/updating, specify the name you wish to give a bookmark.",
|
|
|
|
"When searching, specifiy the name of the bookmark(s) you're searching for.",
|
|
|
|
})
|
|
|
|
addValidFlag("-t", "tag", []string{
|
|
|
|
"Comma delimited tags",
|
|
|
|
"When adding/updating, specify the tags you wish to give a bookmark.",
|
|
|
|
"When searching, specifiy the name of the tags you're searching for.",
|
|
|
|
})
|
|
|
|
|
|
|
|
// Load the config
|
|
|
|
cfg, err = userConfig.NewConfig(AppName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
fmt.Println("Creating new config")
|
|
|
|
cfg.Save()
|
|
|
|
}
|
|
|
|
// if dbdir isn't set, set it to the config directory
|
|
|
|
if cfg.Get("dbdir") == "" {
|
|
|
|
cfg.Set("dbdir", cfg.GetConfigPath()+"/")
|
|
|
|
}
|
|
|
|
// if dbname isn't set, set it to the default db name
|
|
|
|
if cfg.Get("dbname") == "" {
|
|
|
|
cfg.Set("dbname", DefDBName)
|
|
|
|
}
|
|
|
|
// Get a reference to the database
|
|
|
|
if mdb, err = NewDatabase(cfg.Get("dbdir"), cfg.Get("dbname")); err != nil {
|
|
|
|
fmt.Println("Error loading the database")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an option to the "validFlags" slice
|
|
|
|
func addValidFlag(flag, name string, desc []string) {
|
|
|
|
validFlags = append(validFlags, cliFlag{
|
|
|
|
Flag: flag,
|
|
|
|
Name: name,
|
|
|
|
Description: desc,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func printUsage() {
|
|
|
|
help := []string{
|
|
|
|
"mark is a tool for keeping your bookmarks organized",
|
|
|
|
"",
|
|
|
|
"Usage: ",
|
2017-09-20 23:04:39 +00:00
|
|
|
"\tmark [operation] [optional flags]",
|
2017-09-20 22:12:17 +00:00
|
|
|
"",
|
|
|
|
"If no arguments are given, we enter gui mode",
|
2017-09-20 23:04:39 +00:00
|
|
|
"If no 'operation' arguments are given (-a, -u, -d, -s) search is assumed",
|
2017-09-20 22:12:17 +00:00
|
|
|
"",
|
|
|
|
"Valid arguments are:",
|
|
|
|
"",
|
|
|
|
}
|
|
|
|
for _, ln := range help {
|
|
|
|
fmt.Println(ln)
|
|
|
|
}
|
|
|
|
for _, v := range validFlags {
|
2017-09-20 23:04:39 +00:00
|
|
|
fmt.Println("\t", v.Flag, "\t", v.Name)
|
2017-09-20 22:12:17 +00:00
|
|
|
for _, hv := range v.Description {
|
2017-09-20 23:04:39 +00:00
|
|
|
fmt.Println("\t\t\t", hv)
|
2017-09-20 22:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 14:46:59 +00:00
|
|
|
|
|
|
|
func assertError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|