From 9571ccc4d87907067df98edeaa78f0c167fcff43 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 13 May 2023 22:32:24 -0500 Subject: begin total refactor --- conf/create.go | 83 --------------------------------- conf/delete.go | 54 ---------------------- conf/endpoint.go.bak | 98 --------------------------------------- conf/go.mod | 16 ------- conf/go.sum | 8 ---- conf/list.go | 69 ---------------------------- conf/main.go | 96 -------------------------------------- conf/user.go.bak | 127 --------------------------------------------------- 8 files changed, 551 deletions(-) delete mode 100644 conf/create.go delete mode 100644 conf/delete.go delete mode 100644 conf/endpoint.go.bak delete mode 100644 conf/go.mod delete mode 100644 conf/go.sum delete mode 100644 conf/list.go delete mode 100644 conf/main.go delete mode 100644 conf/user.go.bak (limited to 'conf') diff --git a/conf/create.go b/conf/create.go deleted file mode 100644 index 201149a..0000000 --- a/conf/create.go +++ /dev/null @@ -1,83 +0,0 @@ -package main - - -import ( - "fmt" - "log" - "strings" - "syscall" - "golang.org/x/term" -) - - -func hiddenPrompt(prompt string) (string, error) { - fmt.Print(prompt) - bytes, err := term.ReadPassword(int(syscall.Stdin)) - if err != nil { - return "", err - } - fmt.Println() - - return strings.TrimSpace(string(bytes)), nil -} - - -var createUser = &Command{ - Name: "user", - Execute: func(args []string) { - if len(args) < 1 { - fmt.Println("error: no username provided") - return; - } - username := args[0] - var password string - var err error - if len(args) == 1 { - password, err = hiddenPrompt("Enter password: ") - if err != nil { log.Fatal(err) } - confirm, err := hiddenPrompt("Confirm password: ") - if err != nil { log.Fatal(err) } - if password != confirm { - fmt.Println("passwords do not match!") - return - } - } else { - password = args[1] - } - - _, err = p.CreateUser(username, password) - if err != nil { log.Fatal(err) } - fmt.Printf("created user %v\n", username) - }, -} - - -var createEndpoint = &Command{ - Name: "endpoint", - Execute: func(args []string) { - if len(args) < 3 { - fmt.Println("error: name, URI, and address must be specified!") - return - } - - name := args[0] - path := args[1] - addr := args[2] - - _, err := p.CreateEndpoint(name, path, addr) - if err != nil { log.Fatal(err) } - fmt.Printf("created endpoint %v\n", name) - }, -} - - -func CreateInit(parser *Command) { - create := &Command{ - Name: "create", - Branch: true, - } - - create.AddCommand(createUser) - create.AddCommand(createEndpoint) - parser.AddCommand(create) -} diff --git a/conf/delete.go b/conf/delete.go deleted file mode 100644 index 384e392..0000000 --- a/conf/delete.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" - "log" -) - - -var rmUser = &Command{ - Name: "user", - Execute: func(args []string) { - if len(args) < 1 { - fmt.Println("error: username not provided") - return - } - username := args[0] - user, err := p.GetByUsername(username) - if err != nil { log.Fatal(err) } - err = p.DeleteUser(user) - if err != nil { log.Fatal(err) } - fmt.Printf("deleted user %v\n", username) - }, -} - - -var rmEndpoint = &Command{ - Name: "endpoint", - Execute: func(args []string) { - if len(args) < 1 { - fmt.Println("error: endpointname not provided") - return - } - name := args[0] - endpoint, err := p.GetEndpointByName(name) - if err != nil { log.Fatal(err) } - err = p.DeleteEndpoint(endpoint) - if err != nil { log.Fatal(err) } - fmt.Printf("deleted endpoint %v\n", name) - }, -} - - - - -func RmInit(parser *Command) { - rm := &Command{ - Name: "rm", - Branch: true, - } - - rm.AddCommand(rmUser) - rm.AddCommand(rmEndpoint) - parser.AddCommand(rm) -} diff --git a/conf/endpoint.go.bak b/conf/endpoint.go.bak deleted file mode 100644 index 1f8fa11..0000000 --- a/conf/endpoint.go.bak +++ /dev/null @@ -1,98 +0,0 @@ -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/go.mod b/conf/go.mod deleted file mode 100644 index 7ec1d45..0000000 --- a/conf/go.mod +++ /dev/null @@ -1,16 +0,0 @@ -module sanine.net/git/phlox/phloxconf - -go 1.19 - -replace sanine.net/git/phlox/db => ../db - -require ( - golang.org/x/term v0.7.0 - sanine.net/git/phlox/db v0.0.0-00010101000000-000000000000 -) - -require ( - github.com/mattn/go-sqlite3 v1.14.16 // indirect - golang.org/x/crypto v0.8.0 // indirect - golang.org/x/sys v0.7.0 // indirect -) diff --git a/conf/go.sum b/conf/go.sum deleted file mode 100644 index bd19da9..0000000 --- a/conf/go.sum +++ /dev/null @@ -1,8 +0,0 @@ -github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= -github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= -golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ= -golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= diff --git a/conf/list.go b/conf/list.go deleted file mode 100644 index 4fa3829..0000000 --- a/conf/list.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -import( - "fmt" - "log" - "time" -) - - -var listUsers = Command{ - Name: "users", - Execute: func([]string) { - users, err := p.AllUsers() - if err != nil { - log.Fatal(err) - } - fmt.Printf("id\tname\n========================================\n") - for _, user := range users { - fmt.Printf("%v\t%v\n", user.Id, user.Name) - } - }, -} - - -var listSessions = Command{ - Name: "sessions", - Execute: func([]string) { - sessions, err := p.AllSessions() - if err != nil { - log.Fatal(err) - } - // padding on created and modified to match RFC3339 format - fmt.Printf("user\t\tcreated \tmodified \tid\n================================================================================\n") - for _, session := range sessions { - user, err := p.GetById(session.UserId) - if err != nil { - log.Fatal(err) - } - fmt.Printf("%v\t\t%v\t%v\t%v\n", user.Name, session.Created.Format(time.RFC3339), session.Modified.Format(time.RFC3339), session.Id) - } - }, -} - - -var listEndpoints = Command{ - Name: "endpoints", - Execute: func([]string) { - endpoints, err := p.AllEndpoints() - if err != nil { log.Fatal(err) } - - fmt.Printf("name\tpath\taddress\n========================================\n") - for _, endpoint := range endpoints { - fmt.Printf("%v\t%v\t%v\n", endpoint.Name, endpoint.Path, endpoint.Address) - } - }, -} - - -func ListInit(parser *Command) { - list := &Command{ - Name: "list", - Branch: true, - } - - list.AddCommand(&listUsers) - list.AddCommand(&listSessions) - list.AddCommand(&listEndpoints) - parser.AddCommand(list) -} diff --git a/conf/main.go b/conf/main.go deleted file mode 100644 index 5dc922c..0000000 --- a/conf/main.go +++ /dev/null @@ -1,96 +0,0 @@ -package main - -import ( - "os" - "fmt" - "log" - "flag" -// "strings" -// "bufio" - db "sanine.net/git/phlox/db" -) - - -var Params struct { - InitDb string - Db string -} - - -var p *db.Phlox - -type Command struct { - Name string - Help string - subcommands map[string]*Command - Branch bool - Execute func([]string) -} - -func (c *Command) errNoSubCommand() { - fmt.Println("error: a subcommand was required but not provided.") - fmt.Println("available options are:\n") - for name := range c.subcommands { - fmt.Printf("\t* %v\n", name) - } - fmt.Println() -} - -func (c *Command) Parse(args []string) { - if !c.Branch { - c.Execute(args) - } else { - if len(args) > 0 { - cn, ok := c.subcommands[args[0]] - if ok { - cn.Parse(args[1:]) - } else { - c.errNoSubCommand() - } - } else { - c.errNoSubCommand() - } - } -} - - -func (c *Command) AddCommand(cn *Command) { - if c.subcommands == nil { - c.subcommands = make(map[string]*Command) - } - c.subcommands[cn.Name] = cn -} - - -var parser *Command - - -func main() { - p = &db.Phlox{} - - flag.StringVar(&Params.InitDb, "init", "", "initialize a new database at the given path") - flag.StringVar(&Params.Db, "db", "/etc/phlox/phlox.conf", "open the database at the given path for processing") - flag.Parse() - - if Params.InitDb != "" { - // initialize a new db - err := p.Create(Params.InitDb) - if err != nil { log.Fatal(err) } - fmt.Printf("created new database at %v\n", Params.InitDb) - p.Close() - os.Exit(0) - } - - err := p.Open(Params.Db) - if err != nil { - log.Fatal(err) - } - defer p.Close() - - parser = &Command{Branch: true} - ListInit(parser) - CreateInit(parser) - RmInit(parser) - - parser.Parse(flag.Args()) -} diff --git a/conf/user.go.bak b/conf/user.go.bak deleted file mode 100644 index 5ceaefb..0000000 --- a/conf/user.go.bak +++ /dev/null @@ -1,127 +0,0 @@ -package main - -import ( - "os" - "fmt" - "log" - "strings" - "bufio" - "syscall" - "golang.org/x/term" - db "sanine.net/git/phlox/db" -) - - -func GetUsername(prompt string) (string, error) { - reader := bufio.NewReader(os.Stdin) - - fmt.Print(prompt) - username, err := reader.ReadString('\n') - if err != nil { - return "", err - } - - return strings.TrimSpace(username), nil -} - - -func GetPassword(prompt string) (string, error) { - fmt.Print(prompt) - bytes, err := term.ReadPassword(int(syscall.Stdin)) - if err != nil { - return "", err - } - - password := strings.TrimSpace(string(bytes)) - return password, nil -} - - -func ConfirmPassword() string { - password, err := GetPassword("Enter Password: ") - if err != nil { log.Fatal(err) } - fmt.Println() - password1, err := GetPassword("Confirm Password: ") - if err != nil { log.Fatal(err) } - fmt.Println() - if password != password1 { log.Fatal("passwords do not match!") } - return password -} - - -func GetUser() db.User { - OpenDb() - username, err := GetUsername("Username: ") - if err != nil { log.Fatal(err) } - user, err := p.GetByUsername(username) - if err != nil { log.Fatal(err) } - return user -} - - -func CreateUser() *Command { - return &Command{ - Flag: "create-user", - Usage: "create a new user", - Invoke: func() { - OpenDb() - - username, err := GetUsername("Enter Username: ") - if err != nil { log.Fatal(err) } - password := ConfirmPassword() - - _, err = p.CreateUser(username, password) - if err != nil { log.Fatal(err) } - fmt.Printf("created user %v\n", username) - }, - } -} - - -func DeleteUser() *Command { - return &Command{ - Flag: "delete-user", - Usage: "delete an existing user", - Invoke: func() { - user := GetUser() - err := p.DeleteUser(user) - if err != nil { log.Fatal(err) } - fmt.Printf("deleted user %v\n", user.Name) - }, - } -} - - -func UpdatePassword() *Command { - return &Command{ - Flag: "update-password", - Usage: "update a user's password", - Invoke: func() { - user := GetUser() - password := ConfirmPassword() - err := p.SetPassword(user, password) - if err != nil { log.Fatal(err) } - fmt.Printf("updated password for user %v\n", user.Name) - }, - } -} - - -func ListUsers() *Command { - return &Command{ - Flag: "list-users", - Usage: "list all users", - Invoke: func() { - OpenDb() - users, err := p.AllUsers() - if err != nil { log.Fatal(err) } - fmt.Println("id\t\tname") - fmt.Println("========================================") - for _, user := range users { - fmt.Printf( - "%v\t\t%v\n", user.Id, user.Name, - ) - } - }, - } -} -- cgit v1.2.1