rpaste/modules/paste/method.go

117 lines
3.2 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"
2024-06-10 03:27:38 +00:00
"io"
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-29 07:40:36 +00:00
pasteBinGoTo := ctx.String("pastebin")
2022-01-01 23:41:59 +00:00
dryMode := ctx.Bool("dry")
2022-01-02 17:03:04 +00:00
//timeInput := ctx.String("life")
//lexerInput := ctx.String("type")
//verifyValues, err := utility.VerifyInputs(timeInput, lexerInput)
//if err != nil {
// fmt.Printf("%s", err)
//}
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
2022-01-02 17:03:04 +00:00
// Paste Data is empty, checking if input is from stdin
if stdMethod && PasteData == "" {
fio, err := os.Stdin.Stat()
if (fio.Mode() & os.ModeCharDevice) == 0 {
2024-06-10 03:27:38 +00:00
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 == "" {
2022-01-02 17:03:04 +00:00
// 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]
2021-12-28 04:27:10 +00:00
if lastArg != os.Args[0] {
2022-01-02 17:03:04 +00:00
// 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 {
2024-06-10 03:27:38 +00:00
fileBytes, err := os.ReadFile(lastArg)
2022-01-02 17:03:04 +00:00
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)
}
2021-12-28 04:27:10 +00:00
}
}
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.")
2022-01-01 23:41:59 +00:00
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)
2022-01-02 17:03:04 +00:00
default:
fmt.Printf("You have provided an unknown paste bin.\n\n")
cli.ShowAppHelp(ctx)
2022-01-01 23:41:59 +00:00
}
2021-12-29 07:40:36 +00:00
}
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
}