117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
// method runner for pasting
|
|
package paste
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"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")
|
|
dryMode := ctx.Bool("dry")
|
|
//timeInput := ctx.String("life")
|
|
//lexerInput := ctx.String("type")
|
|
|
|
//verifyValues, err := utility.VerifyInputs(timeInput, lexerInput)
|
|
//if err != nil {
|
|
// fmt.Printf("%s", err)
|
|
//}
|
|
|
|
// Check sysinfo is enabled and run through all the required stuff
|
|
if sysInfoMethod {
|
|
PasteData = SysInfoGather()
|
|
}
|
|
|
|
// Paste Data is empty, checking if input is from stdin
|
|
if stdMethod && PasteData == "" {
|
|
fio, err := os.Stdin.Stat()
|
|
if (fio.Mode() & os.ModeCharDevice) == 0 {
|
|
bytes, _ := io.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 == "" {
|
|
// In the event that input is not from stdin, let's check if a file name
|
|
// was given to us. golang doesn't let us do the same thing that python,
|
|
// bash, perl, and others do, which is call the last index item with -1.
|
|
// Below is basically a hack where we get the length of the arguments,
|
|
// subtract by 1, and use that to get the last argument index number.
|
|
argList := os.Args
|
|
lastArg := argList[len(argList)-1]
|
|
if lastArg != os.Args[0] {
|
|
// Check if input exists
|
|
checkarg, _ := utility.IsExist(lastArg)
|
|
// if it's true, check if it's a file
|
|
if checkarg {
|
|
checkfile, _ := utility.IsFile(lastArg)
|
|
// if it's a file, send it to the PasteData var
|
|
if checkfile {
|
|
fileBytes, err := os.ReadFile(lastArg)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
PasteData = string(fileBytes)
|
|
} else {
|
|
fmt.Printf("%s is not a file\n", lastArg)
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
// It doesn't exist, so we need to report if the reported location
|
|
// does not exist. Else, if it starts with a "-", just skip everything
|
|
// entirely.
|
|
if len(lastArg) > 1 && lastArg[:1] == "-" {
|
|
PasteData = ""
|
|
// This section is a bug for now - If a value to an arg is provided
|
|
// and it's not supposed to be a file, we still checked it above.
|
|
} else if !(len(lastArg) > 1 && lastArg[:1] == "-") {
|
|
fmt.Printf("File not found or is an argument value: %s\n", lastArg)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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.")
|
|
|
|
if dryMode {
|
|
fmt.Printf(PasteData)
|
|
} else {
|
|
switch pasteBinGoTo {
|
|
case "rpaste":
|
|
Rpaste(ctx.String("life"), ctx.String("type"), PasteData)
|
|
case "fpaste":
|
|
Fpaste(ctx.String("life"), ctx.String("type"), PasteData)
|
|
default:
|
|
fmt.Printf("You have provided an unknown paste bin.\n\n")
|
|
cli.ShowAppHelp(ctx)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|