diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-02 14:47:02 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-02 14:47:02 -0500 |
commit | 9ab2e0044c8976b5669a069e61479354ca52980c (patch) | |
tree | a62f31483c0ee055fe83376f71971e01ddc02320 | |
parent | ad6a617c12d9b341be1357dcbb6ab58febe32ee8 (diff) |
add endpoint.go
-rw-r--r-- | conf/endpoint.go | 98 | ||||
-rw-r--r-- | conf/main.go | 21 | ||||
-rw-r--r-- | db/endpoint.go | 2 | ||||
-rw-r--r-- | db/session.go | 8 |
4 files changed, 120 insertions, 9 deletions
diff --git a/conf/endpoint.go b/conf/endpoint.go new file mode 100644 index 0000000..1f8fa11 --- /dev/null +++ b/conf/endpoint.go @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + "log" + db "sanine.net/git/phlox/db" +) + + +func CreateEndpoint() *Command { + return &Command{ + Flag: "create-endpoint", + Usage: "create a new endpoint", + Invoke: func() { + OpenDb() + + name := ReadLine("Endpoint Name: ") + path := ReadLine("Endpoint Path: ") + address := ReadLine("Endpoint Address: ") + _, err := p.CreateEndpoint(name, path, address) + if err != nil { log.Fatal(err) } + fmt.Printf("created endpoint %v\n", name) + }, + } +} + + +func GetEndpoint() db.Endpoint { + OpenDb() + name := ReadLine("Endpoint Name: ") + endpoint, err := p.GetEndpointByName(name) + if err != nil { log.Fatal(err) } + return endpoint +} + + +func DeleteEndpoint() *Command { + return &Command{ + Flag: "delete-endpoint", + Usage: "delete an existing endpoint", + Invoke: func() { + endpoint := GetEndpoint() + err := p.DeleteEndpoint(endpoint) + if err != nil { log.Fatal(err) } + fmt.Printf("deleted endpoint %v\n", endpoint.Name) + }, + } +} + + +func SetPath() *Command { + return &Command{ + Flag: "set-endpoint-path", + Usage: "set the path of an existing endpoint", + Invoke: func() { + endpoint := GetEndpoint() + fmt.Printf("Current Path: %v\n", endpoint.Path) + path := ReadLine("New Path: ") + err := p.SetEndpointPath(endpoint, path) + if err != nil { log.Fatal(err) } + fmt.Printf("updated path for endpoint %v\n", endpoint.Name) + }, + } +} + + +func SetAddress() *Command { + return &Command{ + Flag: "set-endpoint-address", + Usage: "set the address of an existing endpoint", + Invoke: func() { + endpoint := GetEndpoint() + fmt.Printf("Current Address: %v\n", endpoint.Address) + address := ReadLine("New Address: ") + err := p.SetEndpointAddress(endpoint, address) + if err != nil { log.Fatal(err) } + fmt.Printf("updated address for endpoint %v\n", endpoint.Name) + }, + } +} + + +func ListEndpoints() *Command { + return &Command{ + Flag: "list-endpoints", + Usage: "list all endpoints", + Invoke: func() { + OpenDb() + endpoints, err := p.AllEndpoints() + if err != nil { log.Fatal(err) } + fmt.Println("id\t\tname\t\tpath\t\taddress") + fmt.Println("================================================================================") + for _, endpoint := range endpoints { + fmt.Printf("%v\t\t%v\t\t%v\t\t%v\n", endpoint.Id, endpoint.Name, endpoint.Path, endpoint.Address) + } + }, + } +} diff --git a/conf/main.go b/conf/main.go index dcd9737..182d97f 100644 --- a/conf/main.go +++ b/conf/main.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "flag" + "strings" + "bufio" db "sanine.net/git/phlox/db" ) @@ -39,6 +41,19 @@ func OpenDb() { } +func ReadLine(prompt string) string { + reader := bufio.NewReader(os.Stdin) + + fmt.Print(prompt) + line, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + + return strings.TrimSpace(line) +} + + func main() { p = &db.Phlox{} @@ -50,6 +65,12 @@ func main() { 4: DeleteUser(), 5: UpdatePassword(), 6: ListUsers(), + + 7: CreateEndpoint(), + 8: DeleteEndpoint(), + 9: SetPath(), + 10: SetAddress(), + 11: ListEndpoints(), } for val, cmd := range cmds { diff --git a/db/endpoint.go b/db/endpoint.go index cb59de5..d880510 100644 --- a/db/endpoint.go +++ b/db/endpoint.go @@ -2,7 +2,7 @@ package db func (p *Phlox) CreateEndpoint(name, path, address string) (Endpoint, error) { var id int - row := p.db.QueryRow("select max(endpointid) from endpoints;") + row := p.db.QueryRow("select coalesce(max(endpointid), 0) from endpoints;") err := row.Scan(&id) if err != nil { return Endpoint{}, err diff --git a/db/session.go b/db/session.go index b8365ff..8590d2f 100644 --- a/db/session.go +++ b/db/session.go @@ -73,14 +73,6 @@ func extractSession(s Scanner) (Session, error) { } -func (p *Phlox) TouchSession(session Session) error { - now := time.Now().UTC() - nowStr := now.Format(time.RFC3339) - _, err := p.db.Exec("update sessions set modified=? where sessionid=?;", nowStr, session.Id) - return err -} - - func (p *Phlox) CheckSession(session Session) (bool, error) { row := p.db.QueryRow("select * from sessions where sessionid = ?", session.Id) session, err := extractSession(row) |