mirror of
https://github.com/quitesimpleorg/hs9001.git
synced 2025-07-01 18:53:49 +02:00
Compare commits
6 Commits
v0.1
...
305e4300cc
Author | SHA1 | Date | |
---|---|---|---|
305e4300cc | |||
b02911c9b4 | |||
8477ba5bfe | |||
54697be895 | |||
da945dce2d | |||
ee7a0868a8 |
6
LICENSE
Normal file
6
LICENSE
Normal file
@ -0,0 +1,6 @@
|
||||
Copyright 2021 lawl (github.com/lawl)
|
||||
Copyright 2021 Albert S. <hs9001 at quitesimple org>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
38
README.md
38
README.md
@ -1,24 +1,48 @@
|
||||
# hs9001
|
||||
hs90001 (history search 9001) is an easy, quite simple bash history enhancement. It simply writes all
|
||||
hs9001 (history search 9001) is an easy, quite simple bash history enhancement. It simply writes all
|
||||
your bash commands into an sqlite database. You can then search this database.
|
||||
|
||||
|
||||
## Setup
|
||||
## Install
|
||||
|
||||
### From source
|
||||
```
|
||||
go build
|
||||
#move hs9001 to a PATH location
|
||||
# Initialize database
|
||||
```
|
||||
|
||||
### Debian / Ubuntu
|
||||
Latest release can be installed using apt
|
||||
```
|
||||
curl -s https://repo.quitesimple.org/repo.quitesimple.org.asc | sudo apt-key add -
|
||||
echo "deb https://repo.quitesimple.org/debian/ default main" | sudo tee /etc/apt/sources.list.d/quitesimple.list
|
||||
sudo apt-get update
|
||||
sudo apt-get install hs9001
|
||||
```
|
||||
|
||||
### Alpine
|
||||
```
|
||||
wget https://repo.quitesimple.org/repo%40quitesimple.org-5f3d101.rsa.pub -O /etc/apk/repo@quitesimple.org-5f3d101.rsa.pub
|
||||
echo "https://repo.quitesimple.org/alpine/quitesimple/" >> /etc/apk/repositories
|
||||
apk update
|
||||
apk add hs9001
|
||||
```
|
||||
|
||||
|
||||
### Setup / Config
|
||||
|
||||
```
|
||||
hs9001 init
|
||||
````
|
||||
```
|
||||
|
||||
Add this to .bashrc
|
||||
|
||||
```
|
||||
if [ -n "$PS1" ] ; then
|
||||
PROMPT_COMMAND='hs9001 -ret $? add "$(history 1)"'
|
||||
PROMPT_COMMAND='hs9001 add -ret $? "$(history 1)"'
|
||||
fi
|
||||
```
|
||||
By default, every system user gets his own database. You can override this by overriding the environment variable
|
||||
By default, every system user gets his own database. You can override this by setting the environment variable
|
||||
for all users that should write to your unified database.
|
||||
|
||||
```
|
||||
@ -29,7 +53,7 @@ export HS9001_DB_PATH="/home/db/history.sqlite"
|
||||
### Search
|
||||
|
||||
```
|
||||
hs9001 search "term"
|
||||
hs9001 search [search terms]
|
||||
```
|
||||
|
||||
It is recommended to create an alias for search to make life easier, e. g.:
|
||||
|
72
main.go
72
main.go
@ -77,6 +77,21 @@ 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) {
|
||||
user := os.Getenv("USER")
|
||||
hostname, err := os.Hostname()
|
||||
@ -124,53 +139,76 @@ func printUsage() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var ret int
|
||||
flag.IntVar(&ret, "ret", 0, "Return value of the command to add")
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
argslen := len(args)
|
||||
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
|
||||
searchCmd := flag.NewFlagSet("search", flag.ExitOnError)
|
||||
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
|
||||
|
||||
if argslen < 1 {
|
||||
if len(os.Args) < 2 {
|
||||
printUsage()
|
||||
return
|
||||
}
|
||||
|
||||
cmd := args[0]
|
||||
cmd := os.Args[1]
|
||||
globalargs := os.Args[2:]
|
||||
|
||||
conn := createConnection()
|
||||
|
||||
if cmd == "add" {
|
||||
switch cmd {
|
||||
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
|
||||
return
|
||||
}
|
||||
if argslen < 2 {
|
||||
if len(args) < 1 {
|
||||
fmt.Fprint(os.Stderr, "Error: You need to provide the command to be added")
|
||||
|
||||
}
|
||||
historycmd := args[1]
|
||||
historycmd := args[0]
|
||||
var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)")
|
||||
rs := rgx.FindStringSubmatch(historycmd)
|
||||
if len(rs) == 2 {
|
||||
add(conn, rs[1])
|
||||
}
|
||||
} else if cmd == "search" {
|
||||
if argslen < 2 {
|
||||
case "search":
|
||||
searchCmd.Parse(globalargs)
|
||||
args := searchCmd.Args()
|
||||
|
||||
if len(args) < 1 {
|
||||
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)
|
||||
os.Exit(23)
|
||||
} else if cmd == "init" {
|
||||
case "delete":
|
||||
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)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
initDatabase(conn)
|
||||
} else if cmd == "import" {
|
||||
case "import":
|
||||
importFromStdin(conn)
|
||||
} else {
|
||||
fmt.Fprint(os.Stderr, "Error: Unknown command supplied\n\n")
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Error: Unknown subcommand '%s' supplied\n\n", cmd)
|
||||
printUsage()
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user