move console commands to separate package

This commit is contained in:
Lukas Schulte Pelkum 2023-06-17 21:58:05 +02:00
parent b2ad618781
commit 3af880042d
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
5 changed files with 13 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"github.com/lus/pasty/internal/cleanup" "github.com/lus/pasty/internal/cleanup"
"github.com/lus/pasty/internal/config" "github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/consolecommands"
"github.com/lus/pasty/internal/meta" "github.com/lus/pasty/internal/meta"
"github.com/lus/pasty/internal/reports" "github.com/lus/pasty/internal/reports"
"github.com/lus/pasty/internal/storage" "github.com/lus/pasty/internal/storage"
@ -132,16 +133,18 @@ func main() {
} }
}() }()
// Listen to console commands if enabled
if !cfg.ConsoleCommandsEnabled { if !cfg.ConsoleCommandsEnabled {
log.Info().Msg("The application has been started. Use Ctrl+C to shut it down.") log.Info().Msg("The application has been started. Use Ctrl+C to shut it down.")
} else { } else {
log.Info().Msg("The application has been started and listens to console commands. Use Ctrl+C or 'stop' to shut it down.") 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, Config: cfg,
Storage: driver, Storage: driver,
}).Listen() }).Listen()
} }
// Wait for an interrupt signal
shutdownChan := make(chan os.Signal, 1) shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, os.Interrupt) signal.Notify(shutdownChan, os.Interrupt)
<-shutdownChan <-shutdownChan

View File

@ -1,4 +1,4 @@
package main package consolecommands
import ( import (
"context" "context"
@ -6,7 +6,7 @@ import (
"time" "time"
) )
func (router *consoleCommandRouter) Cleanup(args []string) { func (router *Router) Cleanup(args []string) {
if len(args) == 0 { if len(args) == 0 {
fmt.Println("Expected 1 argument.") fmt.Println("Expected 1 argument.")
return return

View File

@ -1,11 +1,11 @@
package main package consolecommands
import ( import (
"context" "context"
"fmt" "fmt"
) )
func (router *consoleCommandRouter) Delete(args []string) { func (router *Router) Delete(args []string) {
if len(args) == 0 { if len(args) == 0 {
fmt.Println("Expected 1 argument.") fmt.Println("Expected 1 argument.")
return return

View File

@ -1,11 +1,11 @@
package main package consolecommands
import ( import (
"context" "context"
"fmt" "fmt"
) )
func (router *consoleCommandRouter) SetModificationToken(args []string) { func (router *Router) SetModificationToken(args []string) {
if len(args) < 2 { if len(args) < 2 {
fmt.Println("Expected 2 arguments.") fmt.Println("Expected 2 arguments.")
return return

View File

@ -1,4 +1,4 @@
package main package consolecommands
import ( import (
"bufio" "bufio"
@ -14,12 +14,12 @@ import (
var whitespaceRegex = regexp.MustCompile("\\s+") var whitespaceRegex = regexp.MustCompile("\\s+")
type consoleCommandRouter struct { type Router struct {
Config *config.Config Config *config.Config
Storage storage.Driver Storage storage.Driver
} }
func (router *consoleCommandRouter) Listen() { func (router *Router) Listen() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
for { for {
fmt.Print("> ") fmt.Print("> ")