mirror of
https://github.com/quitesimpleorg/hs9001.git
synced 2025-01-10 09:13:44 +01:00
Compare commits
No commits in common. "305e4300ccb95bd207b54cf6217ee939f4fa96ba" and "da945dce2d967e05a2e1b7c2303e4f9e4eab05ce" have entirely different histories.
305e4300cc
...
da945dce2d
@ -39,7 +39,7 @@ Add this to .bashrc
|
|||||||
|
|
||||||
```
|
```
|
||||||
if [ -n "$PS1" ] ; then
|
if [ -n "$PS1" ] ; then
|
||||||
PROMPT_COMMAND='hs9001 add -ret $? "$(history 1)"'
|
PROMPT_COMMAND='hs9001 -ret $? add "$(history 1)"'
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
By default, every system user gets his own database. You can override this by setting the environment variable
|
By default, every system user gets his own database. You can override this by setting the environment variable
|
||||||
|
72
main.go
72
main.go
@ -77,21 +77,6 @@ func search(conn *sql.DB, q string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func delete(conn *sql.DB, q string) {
|
|
||||||
queryStmt := "DELETE FROM history WHERE command LIKE ?"
|
|
||||||
|
|
||||||
_, err := conn.Exec(queryStmt, "%"+q+"%")
|
|
||||||
if err != nil {
|
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Exec("VACUUM")
|
|
||||||
if err != nil {
|
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func add(conn *sql.DB, cmd string) {
|
func add(conn *sql.DB, cmd string) {
|
||||||
user := os.Getenv("USER")
|
user := os.Getenv("USER")
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
@ -139,76 +124,53 @@ func printUsage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
|
var ret int
|
||||||
searchCmd := flag.NewFlagSet("search", flag.ExitOnError)
|
flag.IntVar(&ret, "ret", 0, "Return value of the command to add")
|
||||||
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
|
flag.Parse()
|
||||||
|
args := flag.Args()
|
||||||
|
argslen := len(args)
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if argslen < 1 {
|
||||||
printUsage()
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := os.Args[1]
|
cmd := args[0]
|
||||||
globalargs := os.Args[2:]
|
|
||||||
|
|
||||||
conn := createConnection()
|
conn := createConnection()
|
||||||
|
|
||||||
switch cmd {
|
if cmd == "add" {
|
||||||
case "add":
|
|
||||||
var ret int
|
|
||||||
addCmd.IntVar(&ret, "ret", 0, "Return value of the command to add")
|
|
||||||
addCmd.Parse(globalargs)
|
|
||||||
args := addCmd.Args()
|
|
||||||
|
|
||||||
if ret == 23 { // 23 is our secret do not log status code
|
if ret == 23 { // 23 is our secret do not log status code
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(args) < 1 {
|
if argslen < 2 {
|
||||||
fmt.Fprint(os.Stderr, "Error: You need to provide the command to be added")
|
fmt.Fprint(os.Stderr, "Error: You need to provide the command to be added")
|
||||||
|
|
||||||
}
|
}
|
||||||
historycmd := args[0]
|
historycmd := args[1]
|
||||||
var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)")
|
var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)")
|
||||||
rs := rgx.FindStringSubmatch(historycmd)
|
rs := rgx.FindStringSubmatch(historycmd)
|
||||||
if len(rs) == 2 {
|
if len(rs) == 2 {
|
||||||
add(conn, rs[1])
|
add(conn, rs[1])
|
||||||
}
|
}
|
||||||
case "search":
|
} else if cmd == "search" {
|
||||||
searchCmd.Parse(globalargs)
|
if argslen < 2 {
|
||||||
args := searchCmd.Args()
|
|
||||||
|
|
||||||
if len(args) < 1 {
|
|
||||||
fmt.Fprint(os.Stderr, "Please provide the search query\n")
|
fmt.Fprint(os.Stderr, "Please provide the search query\n")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
q := strings.Join(args[1:], " ")
|
||||||
q := strings.Join(args, " ")
|
|
||||||
search(conn, q)
|
search(conn, q)
|
||||||
os.Exit(23)
|
os.Exit(23)
|
||||||
case "delete":
|
} else if cmd == "init" {
|
||||||
deleteCmd.Parse(globalargs)
|
|
||||||
args := deleteCmd.Args()
|
|
||||||
if len(args) < 1 {
|
|
||||||
fmt.Fprint(os.Stderr, "Error: You need to provide a search query for records to delete")
|
|
||||||
|
|
||||||
}
|
|
||||||
q := strings.Join(args, " ")
|
|
||||||
delete(conn, q)
|
|
||||||
|
|
||||||
//we do not want to leak what we just deleted :^)
|
|
||||||
os.Exit(23)
|
|
||||||
case "init":
|
|
||||||
err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755)
|
err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
initDatabase(conn)
|
initDatabase(conn)
|
||||||
case "import":
|
} else if cmd == "import" {
|
||||||
importFromStdin(conn)
|
importFromStdin(conn)
|
||||||
default:
|
} else {
|
||||||
fmt.Fprintf(os.Stderr, "Error: Unknown subcommand '%s' supplied\n\n", cmd)
|
fmt.Fprint(os.Stderr, "Error: Unknown command supplied\n\n")
|
||||||
printUsage()
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user