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"
"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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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("> ")