init
This commit is contained in:
53
db/alias.go
Normal file
53
db/alias.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package db
|
||||
|
||||
import "errors"
|
||||
|
||||
type Alias struct {
|
||||
Name string
|
||||
Command string
|
||||
}
|
||||
|
||||
func (db *DB) GetAlias(name string) (*Alias, error) {
|
||||
a := &Alias{}
|
||||
err := db.QueryRow("SELECT name, command FROM aliases WHERE name = ?", name).Scan(&a.Name, &a.Command)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (db *DB) SetAlias(name, command string) error {
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO aliases (name, command) VALUES (?, ?) ON CONFLICT(name) DO UPDATE SET command = excluded.command",
|
||||
name, command,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) DeleteAlias(name string) error {
|
||||
res, err := db.Exec("DELETE FROM aliases WHERE name = ?", name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n == 0 {
|
||||
return errors.New("alias not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) ListAliases() ([]*Alias, error) {
|
||||
rows, err := db.Query("SELECT name, command FROM aliases ORDER BY name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var aliases []*Alias
|
||||
for rows.Next() {
|
||||
a := &Alias{}
|
||||
rows.Scan(&a.Name, &a.Command)
|
||||
aliases = append(aliases, a)
|
||||
}
|
||||
return aliases, nil
|
||||
}
|
||||
Reference in New Issue
Block a user