lots o progress
- added 'command' subcommand - removed viper - setup commands added - still a WIP - readme TODO update
This commit is contained in:
@@ -10,6 +10,10 @@ import (
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
/*
|
||||
all sqlx methods for CRUD functionalities of commands and servers
|
||||
*/
|
||||
|
||||
type database struct {
|
||||
*sqlx.DB
|
||||
}
|
||||
@@ -32,6 +36,8 @@ type Database interface {
|
||||
DeleteServer(name string) error
|
||||
}
|
||||
|
||||
// creates a new sqlite connection to the mctl database. Database files are
|
||||
// kept in the the user home directory
|
||||
func New() (Database, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
@@ -47,29 +53,32 @@ func New() (Database, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// intitial database setup, creates commands and servers tables
|
||||
func (d *database) Init() error {
|
||||
query := `
|
||||
CREATE TABLE IF NOT EXISTS commands(
|
||||
name TEXT PRIMARY KEY,
|
||||
command TEXT
|
||||
)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS servers(
|
||||
name TEXT PRIMARY KEY,
|
||||
server TEXT,
|
||||
password TEXT,
|
||||
port NUMBER
|
||||
)
|
||||
port NUMBER,
|
||||
--active TEXT? bit? or something
|
||||
);
|
||||
`
|
||||
|
||||
_, err := d.Exec(query)
|
||||
return err
|
||||
}
|
||||
|
||||
// drops commands and servers tables
|
||||
func (d *database) Destroy() error {
|
||||
query := `
|
||||
DROP TABLE commands
|
||||
DROP TABLE servers
|
||||
DROP TABLE commands;
|
||||
DROP TABLE servers;
|
||||
`
|
||||
|
||||
_, err := d.Exec(query)
|
||||
@@ -77,9 +86,10 @@ func (d *database) Destroy() error {
|
||||
}
|
||||
|
||||
func (d *database) Close() error {
|
||||
return d.Close()
|
||||
return d.DB.Close()
|
||||
}
|
||||
|
||||
// gets command using name
|
||||
func (d *database) GetCmd(name string) (string, error) {
|
||||
query := `
|
||||
SELECT
|
||||
@@ -99,6 +109,7 @@ func (d *database) GetCmd(name string) (string, error) {
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// gets all saved commands
|
||||
func (d *database) GetAllCmds() ([]model.Command, error) {
|
||||
query := `
|
||||
SELECT
|
||||
@@ -128,6 +139,7 @@ func (d *database) GetAllCmds() ([]model.Command, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// save a new command
|
||||
func (d *database) SaveCmd(name, cmd string) error {
|
||||
query := `
|
||||
INSERT INTO commands(name, command)
|
||||
@@ -138,6 +150,7 @@ func (d *database) SaveCmd(name, cmd string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// DO WE NEED THIS?
|
||||
func (d *database) UpdateCmd(name, cmd string) error {
|
||||
query := `
|
||||
UPDATE
|
Reference in New Issue
Block a user