From 3af880042d4fbbecdc67b404acb77812febd9352 Mon Sep 17 00:00:00 2001 From: Lukas Schulte Pelkum Date: Sat, 17 Jun 2023 21:58:05 +0200 Subject: [PATCH] move console commands to separate package --- cmd/pasty/main.go | 5 ++++- .../consolecommands/cmd_cleanup.go | 4 ++-- .../consolecommands/cmd_delete.go | 4 ++-- .../consolecommands/cmd_set_modification_token.go | 4 ++-- .../command_router.go => internal/consolecommands/router.go | 6 +++--- 5 files changed, 13 insertions(+), 10 deletions(-) rename cmd/pasty/command_cleanup.go => internal/consolecommands/cmd_cleanup.go (87%) rename cmd/pasty/command_delete.go => internal/consolecommands/cmd_delete.go (88%) rename cmd/pasty/command_set_modification_token.go => internal/consolecommands/cmd_set_modification_token.go (90%) rename cmd/pasty/command_router.go => internal/consolecommands/router.go (94%) diff --git a/cmd/pasty/main.go b/cmd/pasty/main.go index 4ee6028..95939b2 100644 --- a/cmd/pasty/main.go +++ b/cmd/pasty/main.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/lus/pasty/internal/cleanup" "github.com/lus/pasty/internal/config" + "github.com/lus/pasty/internal/consolecommands" "github.com/lus/pasty/internal/meta" "github.com/lus/pasty/internal/reports" "github.com/lus/pasty/internal/storage" @@ -132,16 +133,18 @@ func main() { } }() + // Listen to console commands if enabled if !cfg.ConsoleCommandsEnabled { log.Info().Msg("The application has been started. Use Ctrl+C to shut it down.") } else { log.Info().Msg("The application has been started and listens to console commands. Use Ctrl+C or 'stop' to shut it down.") - go (&consoleCommandRouter{ + go (&consolecommands.Router{ Config: cfg, Storage: driver, }).Listen() } + // Wait for an interrupt signal shutdownChan := make(chan os.Signal, 1) signal.Notify(shutdownChan, os.Interrupt) <-shutdownChan diff --git a/cmd/pasty/command_cleanup.go b/internal/consolecommands/cmd_cleanup.go similarity index 87% rename from cmd/pasty/command_cleanup.go rename to internal/consolecommands/cmd_cleanup.go index 16abc57..59029d0 100644 --- a/cmd/pasty/command_cleanup.go +++ b/internal/consolecommands/cmd_cleanup.go @@ -1,4 +1,4 @@ -package main +package consolecommands import ( "context" @@ -6,7 +6,7 @@ import ( "time" ) -func (router *consoleCommandRouter) Cleanup(args []string) { +func (router *Router) Cleanup(args []string) { if len(args) == 0 { fmt.Println("Expected 1 argument.") return diff --git a/cmd/pasty/command_delete.go b/internal/consolecommands/cmd_delete.go similarity index 88% rename from cmd/pasty/command_delete.go rename to internal/consolecommands/cmd_delete.go index 2b92be4..081d353 100644 --- a/cmd/pasty/command_delete.go +++ b/internal/consolecommands/cmd_delete.go @@ -1,11 +1,11 @@ -package main +package consolecommands import ( "context" "fmt" ) -func (router *consoleCommandRouter) Delete(args []string) { +func (router *Router) Delete(args []string) { if len(args) == 0 { fmt.Println("Expected 1 argument.") return diff --git a/cmd/pasty/command_set_modification_token.go b/internal/consolecommands/cmd_set_modification_token.go similarity index 90% rename from cmd/pasty/command_set_modification_token.go rename to internal/consolecommands/cmd_set_modification_token.go index c99c2b0..a6ac2b4 100644 --- a/cmd/pasty/command_set_modification_token.go +++ b/internal/consolecommands/cmd_set_modification_token.go @@ -1,11 +1,11 @@ -package main +package consolecommands import ( "context" "fmt" ) -func (router *consoleCommandRouter) SetModificationToken(args []string) { +func (router *Router) SetModificationToken(args []string) { if len(args) < 2 { fmt.Println("Expected 2 arguments.") return diff --git a/cmd/pasty/command_router.go b/internal/consolecommands/router.go similarity index 94% rename from cmd/pasty/command_router.go rename to internal/consolecommands/router.go index 4f6bdc9..1c9ec90 100644 --- a/cmd/pasty/command_router.go +++ b/internal/consolecommands/router.go @@ -1,4 +1,4 @@ -package main +package consolecommands import ( "bufio" @@ -14,12 +14,12 @@ import ( var whitespaceRegex = regexp.MustCompile("\\s+") -type consoleCommandRouter struct { +type Router struct { Config *config.Config Storage storage.Driver } -func (router *consoleCommandRouter) Listen() { +func (router *Router) Listen() { reader := bufio.NewReader(os.Stdin) for { fmt.Print("> ")