drop cli and bump deps

This commit is contained in:
Hayden 2022-08-29 18:34:27 -08:00
parent 29f583e936
commit 43eba5437a
8 changed files with 64 additions and 308 deletions

View file

@ -1,9 +0,0 @@
package main
import (
"github.com/hay-kot/git-web-template/backend/internal/repo"
)
type app struct {
repos *repo.AllRepos
}

View file

@ -1,105 +0,0 @@
package main
import (
"context"
"fmt"
"os"
"text/tabwriter"
"github.com/google/uuid"
"github.com/hay-kot/git-web-template/backend/app/cli/reader"
"github.com/hay-kot/git-web-template/backend/internal/types"
"github.com/hay-kot/git-web-template/backend/pkgs/hasher"
"github.com/urfave/cli/v2"
)
func (a *app) UserCreate(c *cli.Context) error {
var defaultValidators = []reader.StringValidator{
reader.StringRequired,
reader.StringNoLeadingOrTrailingWhitespace,
}
// Get Flags
name := reader.ReadString("Name: ",
defaultValidators...,
)
password := reader.ReadString("Password: ",
defaultValidators...,
)
email := reader.ReadString("Email: ",
reader.StringRequired,
reader.StringNoLeadingOrTrailingWhitespace,
reader.StringContainsAt,
)
isSuper := reader.ReadBool("Is Superuser?")
pwHash, err := hasher.HashPassword(password)
if err != nil {
return err
}
usr := types.UserCreate{
Name: name,
Email: email,
Password: pwHash,
IsSuperuser: isSuper,
}
_, err = a.repos.Users.Create(context.Background(), usr)
if err == nil {
fmt.Println("Super user created")
}
return err
}
func (a *app) UserDelete(c *cli.Context) error {
// Get Flags
id := c.String("id")
uid := uuid.MustParse(id)
fmt.Printf("Deleting user with id: %s\n", id)
// Confirm Action
fmt.Printf("Are you sure you want to delete this user? (y/n) ")
var answer string
_, err := fmt.Scanln(&answer)
if answer != "y" || err != nil {
fmt.Println("Aborting")
return nil
}
err = a.repos.Users.Delete(context.Background(), uid)
if err == nil {
fmt.Printf("%v User(s) deleted (id=%v)\n", 1, id)
}
return err
}
func (a *app) UserList(c *cli.Context) error {
fmt.Println("Superuser List")
users, err := a.repos.Users.GetAll(context.Background())
if err != nil {
return err
}
tabWriter := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
defer func(tabWriter *tabwriter.Writer) {
_ = tabWriter.Flush()
}(tabWriter)
_, err = fmt.Fprintln(tabWriter, "Id\tName\tEmail\tIsSuper")
if err != nil {
return err
}
for _, u := range users {
_, _ = fmt.Fprintf(tabWriter, "%v\t%s\t%s\t%v\n", u.ID, u.Name, u.Email, u.IsSuperuser)
}
return nil
}

View file

@ -1,82 +0,0 @@
package main
import (
"context"
"log"
"os"
"github.com/hay-kot/git-web-template/backend/ent"
"github.com/hay-kot/git-web-template/backend/internal/config"
"github.com/hay-kot/git-web-template/backend/internal/repo"
_ "github.com/mattn/go-sqlite3"
"github.com/urfave/cli/v2"
)
func main() {
cfg, err := config.NewConfig("config.yml")
if err != nil {
panic(err)
}
if err := run(cfg); err != nil {
log.Fatal(err)
}
}
func run(cfg *config.Config) error {
// =========================================================================
// Initialize Database
c, err := ent.Open(cfg.Database.GetDriver(), cfg.Database.GetUrl())
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer func(c *ent.Client) {
_ = c.Close()
}(c)
if err := c.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
// Create App
a := &app{
repos: repo.EntAllRepos(c),
}
app := &cli.App{
Commands: []*cli.Command{
{
Name: "users",
Aliases: []string{"u"},
Usage: "options to manage users",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "list users in database",
Action: a.UserList,
},
{
Name: "add",
Usage: "add a new user",
Action: a.UserCreate,
},
{
Name: "delete",
Usage: "delete user in database",
Action: a.UserDelete,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "id",
Usage: "name of the user to add",
Required: true,
},
},
},
},
},
},
}
return app.Run(os.Args)
}

View file

@ -1,65 +0,0 @@
package reader
import "fmt"
type StringValidator func(s string) bool
func StringRequired(s string) bool {
return s != ""
}
func StringNoLeadingOrTrailingWhitespace(s string) bool {
return s != "" && len(s) > 0 && s[0] != ' ' && s[len(s)-1] != ' '
}
func StringContainsAt(s string) bool {
for _, c := range s {
if c == '@' {
return true
}
}
return false
}
func ReadString(message string, sv ...StringValidator) string {
for {
fmt.Print(message)
var input string
fmt.Scanln(&input)
if len(sv) == 0 {
return input
}
isValid := true
for _, validator := range sv {
if !validator(input) {
isValid = false
fmt.Println("Invalid input")
continue
}
}
if isValid {
return input
}
}
}
func ReadBool(message string) bool {
for {
fmt.Print(message + " (y/n) ")
var input string
fmt.Scanln(&input)
if input == "y" {
return true
} else if input == "n" {
return false
} else {
fmt.Println("Invalid input")
}
}
}