remove unnecessary comments and do a bit of house cleaning

master
Sina Siadat 8 years ago
parent 141dcdd17f
commit bf0ff020f7

@ -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 L%s:", strings.Repeat(" ", len(attr.getIdentifier())), strconv.Itoa(linenumber+1))
prefix := fmt.Sprintf("%s", strings.Repeat(" ", 3+len(attr.getIdentifier()))) prefix := fmt.Sprintf("%s", strings.Repeat(" ", 3+len(attr.getIdentifier())))
matchinglines = append(matchinglines, color(prefix, "black")+line) matchinglines = append(matchinglines, color(prefix, "black")+line)
if maximumShownMatches != -1 && matchCounter >= maximumShownMatches { if maxShownMatches != -1 && matchCounter >= maxShownMatches {
break break
} }
} }
@ -339,13 +339,11 @@ func (attr attrStruct) setAlias(db *sql.DB, alias string) {
stmt, err := db.Prepare("UPDATE attributes SET alias = ? WHERE id = ?") stmt, err := db.Prepare("UPDATE attributes SET alias = ? WHERE id = ?")
check(err) check(err)
//var result sql.Result
if !unset { if !unset {
_, err = stmt.Exec(alias, attr.getID()) _, err = stmt.Exec(alias, attr.getID())
} else { } else {
_, err = stmt.Exec(nil, attr.getID()) _, err = stmt.Exec(nil, attr.getID())
} }
//check(err)
if err == nil { if err == nil {
if unset { if unset {
fmt.Fprintf(out, "ID:%d unaliased\n", attr.getID()) fmt.Fprintf(out, "ID:%d unaliased\n", attr.getID())
@ -355,7 +353,6 @@ func (attr attrStruct) setAlias(db *sql.DB, alias string) {
} else { } else {
log.Fatalf("error while setting alias \"%s\" for ID:%d -- alias must be unique\n", alias, attr.getID()) // , err) 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) { 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 { if openEditor(filepath) == false {
return return rowsAffected
} }
valueText := readFile(filepath) valueText := readFile(filepath)
@ -459,11 +456,9 @@ func (attr attrStruct) edit(db *sql.DB) (rowsAffected int64) {
if valueText != attr.getValue() { if valueText != attr.getValue() {
rowsAffected = attr.updateDb(db, valueText) rowsAffected = attr.updateDb(db, valueText)
} }
return return rowsAffected
} }
/******************************************************************************/
func writeToFile(filepath string, content string) { func writeToFile(filepath string, content string) {
err := ioutil.WriteFile(filepath, []byte(content), 0644) err := ioutil.WriteFile(filepath, []byte(content), 0644)
check(err) check(err)
@ -582,11 +577,11 @@ func findAttributeByAlias(db *sql.DB, alias string, exactMatchOnly bool) (attr a
check(err) 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) 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 { if err == nil {
return return attr
} }
if exactMatchOnly { if exactMatchOnly {
return return attr
} }
stmt, err = db.Prepare("SELECT " + sqlSelect + " FROM attributes WHERE alias LIKE ? ORDER BY " + orderby + " LIMIT 1") 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 // 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) 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 { if err == nil {
return return attr
} }
// Postfix match // 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) 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 { if err == nil {
return return attr
} }
prunes := strings.Split(alias, "") prunes := strings.Split(alias, "")
@ -609,10 +604,10 @@ func findAttributeByAlias(db *sql.DB, alias string, exactMatchOnly bool) (attr a
// Fuzzy match // 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) 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 { if err == nil {
return return attr
} }
return return attr
} }
func findAttributeByAliasOrID(db *sql.DB, indentifier string) (attr attrStruct) { func findAttributeByAliasOrID(db *sql.DB, indentifier string) (attr attrStruct) {
@ -687,8 +682,6 @@ func listWithFilters(db *sql.DB, opts options) (attrs []attrStruct) {
sqlLimit = "LIMIT ?, ?" sqlLimit = "LIMIT ?, ?"
} }
// ===========================================================================
tx, err := db.Begin() tx, err := db.Begin()
check(err) check(err)
stmt, err = tx.Prepare("SELECT " + sqlSelect + " FROM attributes WHERE " + sqlConditions + " ORDER BY " + orderby + " " + sqlLimit) 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 = opts
optsNew.RootID = attr.getID() optsNew.RootID = attr.getID()
optsNew.Indent += 2 optsNew.Indent += 2
//cmdLs(db, w, optsNew)
} }
tx.Commit() tx.Commit()
@ -739,7 +731,7 @@ func saveString(db *sql.DB, valueText string) (lastInsertID int64) {
lastInsertID, err = result.LastInsertId() lastInsertID, err = result.LastInsertId()
check(err) check(err)
return return lastInsertID
} }
func initializeDatabase(db *sql.DB) bool { func initializeDatabase(db *sql.DB) bool {
@ -753,7 +745,6 @@ func initializeDatabase(db *sql.DB) bool {
parent_id INTEGER, parent_id INTEGER,
frequency INTEGER DEFAULT 0, frequency INTEGER DEFAULT 0,
mark INTEGER DEFAULT 0, mark INTEGER DEFAULT 0,
-- pwd TEXT,
value_text TEXT, value_text TEXT,
value_blob BLOB, value_blob BLOB,

@ -17,12 +17,11 @@ import (
_ "github.com/mattn/go-sqlite3" _ "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 = "-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 (
const defaultEditor = "vi" 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 { func cmdShow(db *sql.DB, opts options) bool {
if len(opts.IDs) == 0 && len(opts.Aliases) == 0 { 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 { for _, id := range opts.IDs {
attr := findAttributeByID(db, id) attr := findAttributeByID(db, id)
//fmt.Printf(attr.getValue())
printToLess(attr.getValue()) printToLess(attr.getValue())
} }
for _, alias := range opts.Aliases { for _, alias := range opts.Aliases {
attr := findAttributeByAlias(db, alias, false) attr := findAttributeByAlias(db, alias, false)
//fmt.Printf(attr.getValue())
printToLess(attr.getValue()) printToLess(attr.getValue())
} }
return true return true
@ -75,7 +72,6 @@ func cmdMount(db *sql.DB, opts options) bool {
func cmdAddFiles(db *sql.DB, files []string) bool { func cmdAddFiles(db *sql.DB, files []string) bool {
tx, err := db.Begin() 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 (?, ?, ?)") stmt, err := tx.Prepare("INSERT INTO attributes (name, value_text, value_blob) VALUES (?, ?, ?)")
if err != nil { if err != nil {
@ -94,13 +90,11 @@ func cmdAddFiles(db *sql.DB, files []string) bool {
} }
fileAbsPath, err := filepath.Abs(file) fileAbsPath, err := filepath.Abs(file)
// fileRelPath, err := filepath.Rel(pwd, fileAbsPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
//_, err = stmt.Exec("file", pwd, fileRelPath, content)
_, err = stmt.Exec("file", fileAbsPath, content) _, err = stmt.Exec("file", fileAbsPath, content)
if err != nil { if err != nil {
log.Fatal(err) 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 { func cmdNew(db *sql.DB, opts options) bool {
var valueText string var valueText string
if opts.FromStdin { if opts.FromStdin {
@ -374,8 +367,6 @@ func cmdUnmark(db *sql.DB, opts options) bool {
return true return true
} }
/******************************************************************************/
func openEditor(filepath string) bool { func openEditor(filepath string) bool {
var cmd *exec.Cmd var cmd *exec.Cmd
@ -410,17 +401,16 @@ func readFile(filepath string) string {
func check(e error) { func check(e error) {
if e != nil { if e != nil {
// log.Fatal(e)
panic(e) panic(e)
} }
} }
func printToLess(text string) { func printToLess(text string) {
// declare your pager // declare pager
cmd := exec.Command("/usr/bin/env", "less") cmd := exec.Command("/usr/bin/env", "less")
// create a pipe (blocking) // create a pipe (blocking)
r, stdin := io.Pipe() r, stdin := io.Pipe()
// Set your i/o's // set IOs
cmd.Stdin = r cmd.Stdin = r
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr

@ -16,7 +16,6 @@ import (
) )
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var pwd string
const dbfilename string = ".etondb" const dbfilename string = ".etondb"
@ -65,9 +64,7 @@ func main() {
opts := optionsFromArgs(args) opts := optionsFromArgs(args)
//pwd, _ = os.Getwd() dbfile := filepath.Join(homeDir(), dbfilename)
dbfile := filepath.Join(homeDirectory(), dbfilename)
var db *sql.DB var db *sql.DB
dbfileExists := false dbfileExists := false

@ -6,10 +6,12 @@ import (
"strconv" "strconv"
) )
const novalue string = "nil" const (
const datelayout string = "06/01/02 03:04pm" novalue = "nil"
const ellipsis = "…" datelayout = "06/01/02 03:04pm"
const maximumShownMatches = -1 ellipsis = "…"
maxShownMatches = -1
)
type options struct { type options struct {
ID int64 ID int64
@ -40,16 +42,15 @@ func optionsFromArgs(args map[string]interface{}) (opts options) {
opts.RootID = -1 opts.RootID = -1
opts.Indent = 0 opts.Indent = 0
opts.Offset, err = strconv.Atoi(args["--offset"].(string))
check(err)
opts.ListFilepaths = args["--list-files"].(bool) opts.ListFilepaths = args["--list-files"].(bool)
if args["<note>"] != nil { if args["<note>"] != nil {
opts.Note = args["<note>"].(string) opts.Note = args["<note>"].(string)
} }
opts.Offset, err = strconv.Atoi(args["--offset"].(string))
check(err)
opts.AfterLinesCount, err = strconv.Atoi(args["--after"].(string)) opts.AfterLinesCount, err = strconv.Atoi(args["--after"].(string))
check(err) check(err)
@ -72,7 +73,7 @@ func optionsFromArgs(args map[string]interface{}) (opts options) {
if args["<mountpoint>"] != nil { if args["<mountpoint>"] != nil {
opts.MountPoint = args["<mountpoint>"].(string) opts.MountPoint = args["<mountpoint>"].(string)
} else { } else {
opts.MountPoint = filepath.Join(homeDirectory(), "eton-default-mount-point") opts.MountPoint = filepath.Join(homeDir(), "eton-default-mount-point")
} }
if args["<id2>"] != nil { if args["<id2>"] != nil {
@ -110,10 +111,9 @@ func optionsFromArgs(args map[string]interface{}) (opts options) {
opts.FromStdin = args["-"].(bool) opts.FromStdin = args["-"].(bool)
opts.Recursive = false // args["--recursive"].(bool) opts.Recursive = false // args["--recursive"].(bool)
opts.IncludeRemoved = args["--removed"].(bool) opts.IncludeRemoved = args["--removed"].(bool)
opts.ShortMode = args["--short"].(bool) opts.ShortMode = args["--short"].(bool)
opts.Verbose = args["--verbose"].(bool) opts.Verbose = args["--verbose"].(bool)
return return opts
} }
func (opts options) getIDsArrayOfInterface() []interface{} { func (opts options) getIDsArrayOfInterface() []interface{} {
@ -124,7 +124,7 @@ func (opts options) getIDsArrayOfInterface() []interface{} {
return interfaceIds return interfaceIds
} }
func homeDirectory() string { func homeDir() string {
usr, err := user.Current() usr, err := user.Current()
check(err) check(err)
return usr.HomeDir return usr.HomeDir

Loading…
Cancel
Save