From 92c6d1b7bc1c3735c55f6c78143bdd95b6ec236c Mon Sep 17 00:00:00 2001 From: Kilian SMITI Date: Sat, 11 Mar 2023 01:30:03 +0100 Subject: [PATCH] refact(cmd): run function to struct for code scoping --- cmd/add.go | 2 +- cmd/list.go | 2 +- cmd/open.go | 2 +- cmd/rm.go | 2 +- cmd/root.go | 2 +- internal/commands/add.go | 22 ++++++++++++---------- internal/commands/init.go | 26 ++++++++++++++------------ internal/commands/list.go | 4 +++- internal/commands/open.go | 4 +++- internal/commands/rm.go | 4 +++- 10 files changed, 40 insertions(+), 30 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 04cd639..2c7aaed 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -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() { diff --git a/cmd/list.go b/cmd/list.go index 3eaebfd..93f1d52 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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() { diff --git a/cmd/open.go b/cmd/open.go index fd1eeb0..727d290 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -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() { diff --git a/cmd/rm.go b/cmd/rm.go index 452d315..6ff9e99 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -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() { diff --git a/cmd/root.go b/cmd/root.go index cfd95d9..3438c31 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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{}) } } diff --git a/internal/commands/add.go b/internal/commands/add.go index d26e0c3..e783ca0 100644 --- a/internal/commands/add.go +++ b/internal/commands/add.go @@ -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", @@ -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", - ) -} diff --git a/internal/commands/init.go b/internal/commands/init.go index 5bbcaaa..4cf7e6c 100644 --- a/internal/commands/init.go +++ b/internal/commands/init.go @@ -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) @@ -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) @@ -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", - ) -} diff --git a/internal/commands/list.go b/internal/commands/list.go index 6c69510..5b5aa24 100644 --- a/internal/commands/list.go +++ b/internal/commands/list.go @@ -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{ diff --git a/internal/commands/open.go b/internal/commands/open.go index d26ec2f..1202cd1 100644 --- a/internal/commands/open.go +++ b/internal/commands/open.go @@ -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: " + diff --git a/internal/commands/rm.go b/internal/commands/rm.go index 6ddd8c6..c73b6e0 100644 --- a/internal/commands/rm.go +++ b/internal/commands/rm.go @@ -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: " +