109 lines
2.5 KiB
Go
109 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"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
|
|
|
|
func main() {
|
|
err := initialize()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// If no command line options are given, go into gui mode
|
|
if len(os.Args) > 1 {
|
|
fmt.Println(os.Args[1])
|
|
if os.Args[1] == "-a" {
|
|
// Adding a new bookmark
|
|
}
|
|
} else {
|
|
// Enter cui mode
|
|
fmt.Println("Entering CUI Mode")
|
|
}
|
|
}
|
|
|
|
// initialize sets up the application for general use
|
|
func initialize() error {
|
|
var err error
|
|
// Build the list of valid command line options
|
|
addValidFlag("-a", "add", []string{"Add a new bookmark"})
|
|
addValidFlag("-h", "help", []string{"Print the usage (this message)"})
|
|
addValidFlag("-n", "name", []string{
|
|
"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.",
|
|
})
|
|
addValidFlag("-u", "update", []string{"Update an existing bookmark"})
|
|
|
|
// 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: ",
|
|
"\tmark",
|
|
"",
|
|
"If no arguments are given, we enter gui mode",
|
|
"",
|
|
"Valid arguments are:",
|
|
"",
|
|
}
|
|
for _, ln := range help {
|
|
fmt.Println(ln)
|
|
}
|
|
for _, v := range validFlags {
|
|
fmt.Println(v.Flag, "\t", v.Name)
|
|
for _, hv := range v.Description {
|
|
fmt.Println("\t", hv)
|
|
}
|
|
}
|
|
}
|