Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use urfave/cli to handle CLI #150

Merged
merged 3 commits into from Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .drone.yml
Expand Up @@ -39,6 +39,7 @@ steps:
- GO111MODULE=auto go get -u github.com/gobuffalo/packr/v2/packr2
- go mod download
- packr2 build -o shiori
- ./shiori
- ./shiori add https://example.com
- ./shiori print
- ./shiori delete 1
Expand Down
147 changes: 147 additions & 0 deletions cmd/account.go
@@ -0,0 +1,147 @@
package cmd

import (
"errors"
"fmt"
"syscall"

"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
)

var (
CmdAccount = cli.Command{
Name: "account",
Description: "Manage account for accessing web interface",
Subcommands: []cli.Command{
subcmdAddAccount,
subcmdPrintAccounts,
subcmdDeleteAccounts,
},
}

subcmdAddAccount = cli.Command{
Name: "add",
Description: "Create new account",
Action: runAddAccount,
}

subcmdPrintAccounts = cli.Command{
Name: "print",
Description: "List all accounts",
Aliases: []string{"list", "ls"},
Flags: []cli.Flag{
cli.StringFlag{
Name: "search, s",
Usage: "Search accounts by username",
},
},
Action: runPrintAccount,
}

subcmdDeleteAccounts = cli.Command{
Name: "delete",
Description: "Delete accounts. " +
"Accepts space-separated list of usernames. " +
"If no arguments, all records will be deleted.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "yes, y",
Usage: "Skip confirmation prompt and delete ALL accounts",
},
},
Action: runDeleteAccount,
}
)

func runAddAccount(c *cli.Context) error {
// TODO: check for duplicate account already
args := c.Args()
db, err := getDbConnection(c)

if err != nil {
return errors.New(cErrorSprint(err))
}

username := args[0]
if username == "" {
return errors.New(cErrorSprint("Username must not be empty"))
}

fmt.Println("Username: " + username)

// Read and validate password
fmt.Print("Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return errors.New(cErrorSprint(err))
}

fmt.Println()
strPassword := string(bytePassword)
if len(strPassword) < 8 {
return errors.New(cErrorSprint("Password must be at least 8 characters"))
}

// Save account to database
err = db.CreateAccount(username, strPassword)
if err != nil {
return errors.New(cErrorSprint(err))
}
return nil
}

func runPrintAccount(c *cli.Context) error {
// Parse flags
db, err := getDbConnection(c)

if err != nil {
return errors.New(cErrorSprint(err))
}
keyword := c.String("search")

// Fetch list accounts in database
accounts, err := db.GetAccounts(keyword)
if err != nil {
return errors.New(cErrorSprint(err))
}

// Show list accounts
for _, account := range accounts {
cIndex.Print("- ")
fmt.Println(account.Username)
}
return nil
}

func runDeleteAccount(c *cli.Context) error {
args := c.Args()
skipConfirm := c.Bool("yes")
db, err := getDbConnection(c)

if err != nil {
return errors.New(cErrorSprint(err))
}

// If no arguments (i.e all accounts going to be deleted),
// confirm to user
if len(args) == 0 && !skipConfirm {
confirmDelete := ""
fmt.Print("Remove ALL accounts? (y/n): ")
fmt.Scanln(&confirmDelete)

if confirmDelete != "y" {
fmt.Println("No accounts deleted")
return nil
}
}

// Delete accounts in database
err = db.DeleteAccounts(args...)
if err != nil {
return errors.New(cErrorSprint(err))
}

fmt.Println("Account(s) have been deleted")
return nil
}
97 changes: 0 additions & 97 deletions cmd/account/cmd-handler.go

This file was deleted.

56 changes: 0 additions & 56 deletions cmd/account/root.go

This file was deleted.