1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

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
5 changed files with 13 additions and 10 deletions

View File

@ -0,0 +1,27 @@
package consolecommands
import (
"context"
"fmt"
"time"
)
func (router *Router) Cleanup(args []string) {
if len(args) == 0 {
fmt.Println("Expected 1 argument.")
return
}
lifetime, err := time.ParseDuration(args[0])
if err != nil {
fmt.Printf("Could not parse duration: %s.\n", err.Error())
return
}
amount, err := router.Storage.Pastes().DeleteOlderThan(context.Background(), lifetime)
if err != nil {
if err != nil {
fmt.Printf("Could not delete pastes: %s.\n", err.Error())
return
}
}
fmt.Printf("Deleted %d pastes older than %s.\n", amount, lifetime)
}

View File

@ -0,0 +1,28 @@
package consolecommands
import (
"context"
"fmt"
)
func (router *Router) Delete(args []string) {
if len(args) == 0 {
fmt.Println("Expected 1 argument.")
return
}
pasteID := args[0]
paste, err := router.Storage.Pastes().FindByID(context.Background(), pasteID)
if err != nil {
fmt.Printf("Could not look up paste: %s.\n", err.Error())
return
}
if paste == nil {
fmt.Printf("Invalid paste ID: %s.\n", pasteID)
return
}
if err := router.Storage.Pastes().DeleteByID(context.Background(), pasteID); err != nil {
fmt.Printf("Could not delete paste: %s.\n", err.Error())
return
}
fmt.Printf("Deleted paste %s.\n", pasteID)
}

View File

@ -0,0 +1,34 @@
package consolecommands
import (
"context"
"fmt"
)
func (router *Router) SetModificationToken(args []string) {
if len(args) < 2 {
fmt.Println("Expected 2 arguments.")
return
}
pasteID := args[0]
newToken := args[1]
paste, err := router.Storage.Pastes().FindByID(context.Background(), pasteID)
if err != nil {
fmt.Printf("Could not look up paste: %s.\n", err.Error())
return
}
if paste == nil {
fmt.Printf("Invalid paste ID: %s.\n", pasteID)
return
}
paste.ModificationToken = newToken
if err := paste.HashModificationToken(); err != nil {
fmt.Printf("Could not hash modification token: %s.\n", err.Error())
return
}
if err := router.Storage.Pastes().Upsert(context.Background(), paste); err != nil {
fmt.Printf("Could not update paste: %s.\n", err.Error())
return
}
fmt.Printf("Changed modification token of paste %s to %s.\n", pasteID, newToken)
}

View File

@ -0,0 +1,73 @@
package consolecommands
import (
"bufio"
"fmt"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/storage"
"github.com/rs/zerolog/log"
"os"
"regexp"
"strings"
"syscall"
)
var whitespaceRegex = regexp.MustCompile("\\s+")
type Router struct {
Config *config.Config
Storage storage.Driver
}
func (router *Router) Listen() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
input, err := reader.ReadString('\n')
if err != nil {
log.Err(err).Msg("Could not read console input.")
continue
}
commandData := strings.Split(whitespaceRegex.ReplaceAllString(strings.TrimSpace(input), " "), " ")
if len(commandData) == 0 {
fmt.Println("Invalid command.")
continue
}
handle := strings.ToLower(commandData[0])
var args []string
if len(commandData) > 1 {
args = commandData[1:]
}
switch handle {
case "help":
fmt.Println("Available commands:")
fmt.Println(" help : Shows this overview")
fmt.Println(" stop : Stops the application")
fmt.Println(" setmodtoken <id> <token> : Changes the modification token of the paste with ID <id> to <token>")
fmt.Println(" delete <id> : Deletes the paste with ID <id>")
fmt.Println(" cleanup <duration> : Deletes all pastes that are older than <duration>")
break
case "stop":
if err := syscall.Kill(syscall.Getpid(), syscall.SIGINT); err != nil {
fmt.Printf("Could not send interrupt signal: %s.\nUse Ctrl+C instead.\n", err.Error())
break
}
return
case "setmodtoken":
router.SetModificationToken(args)
break
case "delete":
router.Delete(args)
break
case "cleanup":
router.Cleanup(args)
break
default:
fmt.Println("Invalid command.")
break
}
}
}