Skip to content

Commit

Permalink
Merge pull request #4 from ermos/refact-cmd
Browse files Browse the repository at this point in the history
refact(cmd): run function to struct for code scoping
  • Loading branch information
ermos authored Mar 11, 2023
2 parents b065022 + 92c6d1b commit c2280f1
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var addCmd = &cobra.Command{
Use: "add",
Short: "Store a new database",
Long: `Store a new database`,
Run: commands.RunAdd,
Run: commands.Add{}.Run,
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var listCmd = &cobra.Command{
Use: "list",
Short: "Show available databases",
Long: `Show available databases`,
Run: commands.RunList,
Run: commands.List{}.Run,
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var openCmd = &cobra.Command{
Use: "open",
Short: "Open a database connection",
Long: `Open a database connection`,
Run: commands.RunOpen,
Run: commands.Open{}.Run,
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var rmCmd = &cobra.Command{
Use: "rm",
Short: "Remove a database",
Long: `Remove a database`,
Run: commands.RunRm,
Run: commands.Rm{}.Run,
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func initConfig() {

// No master password ? Init set-up
if dbm.Get().EncryptChecker == "" {
commands.RunInit(rootCmd, []string{})
commands.Init{}.Run(rootCmd, []string{})
}
}
22 changes: 12 additions & 10 deletions internal/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ import (
"github.com/spf13/cobra"
)

func RunAdd(cmd *cobra.Command, args []string) {
type Add struct{}

func (Add) text() {
term.Clear()
term.TitlePrint("Adding a new database to dbm")
fmt.Print("This command is designed to guide you through\n" +
"the process of adding a new database to " + color.New(color.Bold).Sprint("dbm") + ".\n\n",
)
}

func (c Add) Run(cmd *cobra.Command, args []string) {
var dbConfig db.Config

auth.WithMasterPassword()

newText()
c.text()
err := survey.Ask([]*survey.Question{
{
Name: "alias",
Expand Down Expand Up @@ -82,11 +92,3 @@ func RunAdd(cmd *cobra.Command, args []string) {
panic(err)
}
}

func newText() {
term.Clear()
term.TitlePrint("Adding a new database to dbm")
fmt.Print("This command is designed to guide you through\n" +
"the process of adding a new database to " + color.New(color.Bold).Sprint("dbm") + ".\n\n",
)
}
26 changes: 14 additions & 12 deletions internal/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ import (
"os"
)

func RunInit(cmd *cobra.Command, args []string) {
initText()
type Init struct{}

func (Init) text() {
term.Clear()
fmt.Print("Welcome to ", color.New(color.Bold).Sprint("dbm"), "!\n",
"Please define your master password before continuing.\n",
"Use a strong and unique master password,\n",
"it will be asking before each command.\n\n",
)
}

func (c Init) Run(cmd *cobra.Command, args []string) {
c.text()
fmt.Println("Your master password :")
if err := auth.PromptMasterPassword(); err != nil {
log.Fatal(err)
Expand All @@ -28,7 +39,7 @@ func RunInit(cmd *cobra.Command, args []string) {
os.Exit(0)
}

initText()
c.text()
fmt.Println("Verify your master password :")
if err := auth.PromptMasterPassword(); err != nil {
log.Fatal(err)
Expand All @@ -45,12 +56,3 @@ func RunInit(cmd *cobra.Command, args []string) {
os.Exit(0)
}
}

func initText() {
term.Clear()
fmt.Print("Welcome to ", color.New(color.Bold).Sprint("dbm"), "!\n",
"Please define your master password before continuing.\n",
"Use a strong and unique master password,\n",
"it will be asking before each command.\n\n",
)
}
4 changes: 3 additions & 1 deletion internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/spf13/cobra"
)

func RunList(cmd *cobra.Command, args []string) {
type List struct{}

func (List) Run(cmd *cobra.Command, args []string) {
t := table.NewWriter()
t.SetStyle(table.Style{
Box: table.BoxStyle{
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/spf13/cobra"
)

func RunOpen(cmd *cobra.Command, args []string) {
type Open struct{}

func (Open) Run(cmd *cobra.Command, args []string) {
if len(args) < 1 {
panic(
"you must provide the alias name, for example: " +
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/spf13/cobra"
)

func RunRm(cmd *cobra.Command, args []string) {
type Rm struct{}

func (Rm) Run(cmd *cobra.Command, args []string) {
if len(args) < 1 {
panic(
"you must provide the alias name, for example: " +
Expand Down

0 comments on commit c2280f1

Please sign in to comment.