73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
// method runner for pasting
|
|
package paste
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/rocky-linux/rpaste/modules/utility"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var (
|
|
PasteMethod = &cli.Command{
|
|
Name: "Paste",
|
|
Usage: "Pastes content to pastebin",
|
|
Description: `This is the default action for the rpaste utility.`,
|
|
Action: runPaste,
|
|
}
|
|
PasteData string
|
|
)
|
|
|
|
func runPaste(ctx *cli.Context) error {
|
|
// check
|
|
stdMethod := utility.StdInChecker()
|
|
sysInfoMethod := ctx.Bool("sysinfo")
|
|
pasteBinGoTo := ctx.String("pastebin")
|
|
|
|
// Check sysinfo is enabled and run through all the required stuff
|
|
if sysInfoMethod {
|
|
PasteData = SysInfoGather()
|
|
}
|
|
|
|
if stdMethod && PasteData == "" {
|
|
fio, err := os.Stdin.Stat()
|
|
if (fio.Mode() & os.ModeCharDevice) == 0 {
|
|
bytes, _ := ioutil.ReadAll(os.Stdin)
|
|
PasteData = string(bytes)
|
|
} else if err != nil {
|
|
fmt.Printf("Could not read from stdin: (%s)\n", err)
|
|
os.Exit(1)
|
|
}
|
|
} else if !stdMethod && PasteData == "" {
|
|
argList := os.Args
|
|
lastArg := argList[len(argList)-1]
|
|
if lastArg != os.Args[0] {
|
|
fileBytes, err := ioutil.ReadFile(lastArg)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
PasteData = string(fileBytes)
|
|
}
|
|
}
|
|
|
|
// Final check to see if there's paste data
|
|
if PasteData == "" {
|
|
cli.ShowAppHelp(ctx)
|
|
os.Exit(0)
|
|
}
|
|
|
|
// Check that PasteData is text, and not binary
|
|
// fmt.Println("You cannot upload binary data.")
|
|
|
|
switch pasteBinGoTo {
|
|
case "rpaste":
|
|
Rpaste(ctx.String("life"), ctx.String("type"), PasteData)
|
|
case "fpaste":
|
|
Fpaste(ctx.String("life"), ctx.String("type"), PasteData)
|
|
}
|
|
|
|
return nil
|
|
}
|