rpaste/modules/paste/method.go

69 lines
1.4 KiB
Go
Raw Normal View History

2021-12-24 09:01:22 +00:00
// method runner for pasting
2021-12-21 01:06:08 +00:00
package paste
import (
2021-12-24 09:01:22 +00:00
"fmt"
"io/ioutil"
2021-12-24 09:01:22 +00:00
"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
2021-12-21 01:06:08 +00:00
)
2021-12-24 09:01:22 +00:00
func runPaste(ctx *cli.Context) error {
// check
stdMethod := utility.StdInChecker()
sysInfoMethod := ctx.Bool("sysinfo")
2021-12-24 09:01:22 +00:00
// Check sysinfo is enabled and run through all the required stuff
if sysInfoMethod {
PasteData = SysInfoGather()
}
2021-12-24 09:01:22 +00:00
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]
2021-12-28 04:27:10 +00:00
if lastArg != os.Args[0] {
fileBytes, err := ioutil.ReadFile(lastArg)
if err != nil {
panic(err)
}
PasteData = string(fileBytes)
}
2021-12-28 04:27:10 +00:00
}
// Final check to see if there's paste data
if PasteData == "" {
cli.ShowAppHelp(ctx)
os.Exit(0)
}
2021-12-24 09:01:22 +00:00
// Check that PasteData is text, and not binary
2021-12-28 04:27:10 +00:00
// fmt.Println("You cannot upload binary data.")
//fmt.Println(PasteData)
2021-12-24 09:01:22 +00:00
2021-12-28 04:27:10 +00:00
Rpaste(ctx.String("life"), ctx.String("type"), PasteData)
2021-12-21 01:06:08 +00:00
2021-12-24 09:01:22 +00:00
return nil
2021-12-21 01:06:08 +00:00
}