diff --git a/attr.go b/attr.go index 732ae5a..0ffa8df 100644 --- a/attr.go +++ b/attr.go @@ -272,7 +272,7 @@ func (attr attrStruct) prettyMatches(highlighteds []string, after int) string { //prefix := fmt.Sprintf("%s L%s:", strings.Repeat(" ", len(attr.getIdentifier())), strconv.Itoa(linenumber+1)) prefix := fmt.Sprintf("%s", strings.Repeat(" ", 3+len(attr.getIdentifier()))) matchinglines = append(matchinglines, color(prefix, "black")+line) - if maximumShownMatches != -1 && matchCounter >= maximumShownMatches { + if maxShownMatches != -1 && matchCounter >= maxShownMatches { break } } @@ -339,13 +339,11 @@ func (attr attrStruct) setAlias(db *sql.DB, alias string) { stmt, err := db.Prepare("UPDATE attributes SET alias = ? WHERE id = ?") check(err) - //var result sql.Result if !unset { _, err = stmt.Exec(alias, attr.getID()) } else { _, err = stmt.Exec(nil, attr.getID()) } - //check(err) if err == nil { if unset { fmt.Fprintf(out, "ID:%d unaliased\n", attr.getID()) @@ -355,7 +353,6 @@ func (attr attrStruct) setAlias(db *sql.DB, alias string) { } else { log.Fatalf("error while setting alias \"%s\" for ID:%d -- alias must be unique\n", alias, attr.getID()) // , err) } - //rowsAffected, err := result.RowsAffected() } func (attr attrStruct) setMark(db *sql.DB, mark int) (rowsAffected int64) { @@ -451,7 +448,7 @@ func (attr attrStruct) edit(db *sql.DB) (rowsAffected int64) { }() if openEditor(filepath) == false { - return + return rowsAffected } valueText := readFile(filepath) @@ -459,11 +456,9 @@ func (attr attrStruct) edit(db *sql.DB) (rowsAffected int64) { if valueText != attr.getValue() { rowsAffected = attr.updateDb(db, valueText) } - return + return rowsAffected } -/******************************************************************************/ - func writeToFile(filepath string, content string) { err := ioutil.WriteFile(filepath, []byte(content), 0644) check(err) @@ -582,11 +577,11 @@ func findAttributeByAlias(db *sql.DB, alias string, exactMatchOnly bool) (attr a check(err) err = stmt.QueryRow(alias).Scan(&attr.ID, &attr.ValueText, &attr.Name, &attr.ParentID, &attr.Alias, &attr.Mark, &attr.ValueBlob, &attr.CreatedAt, &attr.UpdatedAt) if err == nil { - return + return attr } if exactMatchOnly { - return + return attr } stmt, err = db.Prepare("SELECT " + sqlSelect + " FROM attributes WHERE alias LIKE ? ORDER BY " + orderby + " LIMIT 1") @@ -595,13 +590,13 @@ func findAttributeByAlias(db *sql.DB, alias string, exactMatchOnly bool) (attr a // Prefix match err = stmt.QueryRow(alias+"%").Scan(&attr.ID, &attr.ValueText, &attr.Name, &attr.ParentID, &attr.Alias, &attr.Mark, &attr.ValueBlob, &attr.CreatedAt, &attr.UpdatedAt) if err == nil { - return + return attr } // Postfix match err = stmt.QueryRow("%"+alias).Scan(&attr.ID, &attr.ValueText, &attr.Name, &attr.ParentID, &attr.Alias, &attr.Mark, &attr.ValueBlob, &attr.CreatedAt, &attr.UpdatedAt) if err == nil { - return + return attr } prunes := strings.Split(alias, "") @@ -609,10 +604,10 @@ func findAttributeByAlias(db *sql.DB, alias string, exactMatchOnly bool) (attr a // Fuzzy match err = stmt.QueryRow("%"+strings.Join(prunes, "%")+"%").Scan(&attr.ID, &attr.ValueText, &attr.Name, &attr.ParentID, &attr.Alias, &attr.Mark, &attr.ValueBlob, &attr.CreatedAt, &attr.UpdatedAt) if err == nil { - return + return attr } - return + return attr } func findAttributeByAliasOrID(db *sql.DB, indentifier string) (attr attrStruct) { @@ -687,8 +682,6 @@ func listWithFilters(db *sql.DB, opts options) (attrs []attrStruct) { sqlLimit = "LIMIT ?, ?" } - // =========================================================================== - tx, err := db.Begin() check(err) stmt, err = tx.Prepare("SELECT " + sqlSelect + " FROM attributes WHERE " + sqlConditions + " ORDER BY " + orderby + " " + sqlLimit) @@ -710,7 +703,6 @@ func listWithFilters(db *sql.DB, opts options) (attrs []attrStruct) { optsNew = opts optsNew.RootID = attr.getID() optsNew.Indent += 2 - //cmdLs(db, w, optsNew) } tx.Commit() @@ -739,7 +731,7 @@ func saveString(db *sql.DB, valueText string) (lastInsertID int64) { lastInsertID, err = result.LastInsertId() check(err) - return + return lastInsertID } func initializeDatabase(db *sql.DB) bool { @@ -753,7 +745,6 @@ func initializeDatabase(db *sql.DB) bool { parent_id INTEGER, frequency INTEGER DEFAULT 0, mark INTEGER DEFAULT 0, - -- pwd TEXT, value_text TEXT, value_blob BLOB, diff --git a/commands.go b/commands.go index 0dd13a4..b294873 100644 --- a/commands.go +++ b/commands.go @@ -17,12 +17,11 @@ import ( _ "github.com/mattn/go-sqlite3" ) -// var globalDB *sql.DB -// var globalOpts options - // const orderby = "-frequency, -mark, CASE WHEN updated_at IS NULL THEN created_at ELSE updated_at END DESC" -const orderby = "CASE WHEN updated_at IS NULL THEN created_at ELSE updated_at END DESC" -const defaultEditor = "vi" +const ( + orderby = "CASE WHEN updated_at IS NULL THEN created_at ELSE updated_at END DESC" + defaultEditor = "vi" +) func cmdShow(db *sql.DB, opts options) bool { if len(opts.IDs) == 0 && len(opts.Aliases) == 0 { @@ -31,13 +30,11 @@ func cmdShow(db *sql.DB, opts options) bool { for _, id := range opts.IDs { attr := findAttributeByID(db, id) - //fmt.Printf(attr.getValue()) printToLess(attr.getValue()) } for _, alias := range opts.Aliases { attr := findAttributeByAlias(db, alias, false) - //fmt.Printf(attr.getValue()) printToLess(attr.getValue()) } return true @@ -75,7 +72,6 @@ func cmdMount(db *sql.DB, opts options) bool { func cmdAddFiles(db *sql.DB, files []string) bool { tx, err := db.Begin() - // stmt, err := tx.Prepare("INSERT INTO attributes (name, pwd, value_text, value_blob) VALUES (?, ?, ?, ?)") stmt, err := tx.Prepare("INSERT INTO attributes (name, value_text, value_blob) VALUES (?, ?, ?)") if err != nil { @@ -94,13 +90,11 @@ func cmdAddFiles(db *sql.DB, files []string) bool { } fileAbsPath, err := filepath.Abs(file) - // fileRelPath, err := filepath.Rel(pwd, fileAbsPath) if err != nil { log.Fatal(err) } - //_, err = stmt.Exec("file", pwd, fileRelPath, content) _, err = stmt.Exec("file", fileAbsPath, content) if err != nil { log.Fatal(err) @@ -124,7 +118,6 @@ func cmdLs(db *sql.DB, w *tabwriter.Writer, opts options) bool { } func cmdNew(db *sql.DB, opts options) bool { - var valueText string if opts.FromStdin { @@ -374,8 +367,6 @@ func cmdUnmark(db *sql.DB, opts options) bool { return true } -/******************************************************************************/ - func openEditor(filepath string) bool { var cmd *exec.Cmd @@ -410,17 +401,16 @@ func readFile(filepath string) string { func check(e error) { if e != nil { - // log.Fatal(e) panic(e) } } func printToLess(text string) { - // declare your pager + // declare pager cmd := exec.Command("/usr/bin/env", "less") // create a pipe (blocking) r, stdin := io.Pipe() - // Set your i/o's + // set IOs cmd.Stdin = r cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/main.go b/main.go index a7eb40d..8b632a9 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ import ( ) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") -var pwd string const dbfilename string = ".etondb" @@ -65,9 +64,7 @@ func main() { opts := optionsFromArgs(args) - //pwd, _ = os.Getwd() - - dbfile := filepath.Join(homeDirectory(), dbfilename) + dbfile := filepath.Join(homeDir(), dbfilename) var db *sql.DB dbfileExists := false diff --git a/options.go b/options.go index 53774ee..cac4ee9 100644 --- a/options.go +++ b/options.go @@ -6,10 +6,12 @@ import ( "strconv" ) -const novalue string = "nil" -const datelayout string = "06/01/02 03:04pm" -const ellipsis = "…" -const maximumShownMatches = -1 +const ( + novalue = "nil" + datelayout = "06/01/02 03:04pm" + ellipsis = "…" + maxShownMatches = -1 +) type options struct { ID int64 @@ -40,16 +42,15 @@ func optionsFromArgs(args map[string]interface{}) (opts options) { opts.RootID = -1 opts.Indent = 0 - - opts.Offset, err = strconv.Atoi(args["--offset"].(string)) - check(err) - opts.ListFilepaths = args["--list-files"].(bool) if args[""] != nil { opts.Note = args[""].(string) } + opts.Offset, err = strconv.Atoi(args["--offset"].(string)) + check(err) + opts.AfterLinesCount, err = strconv.Atoi(args["--after"].(string)) check(err) @@ -72,7 +73,7 @@ func optionsFromArgs(args map[string]interface{}) (opts options) { if args[""] != nil { opts.MountPoint = args[""].(string) } else { - opts.MountPoint = filepath.Join(homeDirectory(), "eton-default-mount-point") + opts.MountPoint = filepath.Join(homeDir(), "eton-default-mount-point") } if args[""] != nil { @@ -110,10 +111,9 @@ func optionsFromArgs(args map[string]interface{}) (opts options) { opts.FromStdin = args["-"].(bool) opts.Recursive = false // args["--recursive"].(bool) opts.IncludeRemoved = args["--removed"].(bool) - opts.ShortMode = args["--short"].(bool) opts.Verbose = args["--verbose"].(bool) - return + return opts } func (opts options) getIDsArrayOfInterface() []interface{} { @@ -124,7 +124,7 @@ func (opts options) getIDsArrayOfInterface() []interface{} { return interfaceIds } -func homeDirectory() string { +func homeDir() string { usr, err := user.Current() check(err) return usr.HomeDir