rpaste/modules/paste/method.go
2021-12-27 21:27:10 -07:00

69 lines
1.4 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")
// 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.")
//fmt.Println(PasteData)
Rpaste(ctx.String("life"), ctx.String("type"), PasteData)
return nil
}