From afef86d72e50a2510d8c7d0fc833786d90b54411 Mon Sep 17 00:00:00 2001 From: nazunalika Date: Mon, 20 Dec 2021 18:06:08 -0700 Subject: [PATCH] rpaste rpaste --- go.mod | 7 +++ go.sum | 14 +++++ modules/paste/ixio.go | 2 + modules/paste/method.go | 9 +++ modules/paste/rpaste.go | 2 + modules/setting/setting.go | 31 +++++++++ modules/setting/usage.go | 21 +++++++ modules/utility/file.go | 38 +++++++++++ rpaste.go | 125 +++++++++++++++++++++++++++++++++++++ 9 files changed, 249 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 modules/paste/ixio.go create mode 100644 modules/paste/method.go create mode 100644 modules/paste/rpaste.go create mode 100644 modules/setting/setting.go create mode 100644 modules/setting/usage.go create mode 100644 modules/utility/file.go create mode 100644 rpaste.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2e4c03f --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module github.com/rocky-linux/rpaste +go 1.16 + +require ( + github.com/urfave/cli/v2 v2.3.0 + gopkg.in/ini.v1 v1.66.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..539131f --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= +gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/modules/paste/ixio.go b/modules/paste/ixio.go new file mode 100644 index 0000000..f439bbb --- /dev/null +++ b/modules/paste/ixio.go @@ -0,0 +1,2 @@ +// support for ix.io +package paste diff --git a/modules/paste/method.go b/modules/paste/method.go new file mode 100644 index 0000000..d17d415 --- /dev/null +++ b/modules/paste/method.go @@ -0,0 +1,9 @@ +package paste + +import ( + "github.com/urfave/cli/v2" +) + +var PasteMethod = []*cli.Command{ + +} diff --git a/modules/paste/rpaste.go b/modules/paste/rpaste.go new file mode 100644 index 0000000..22b11c2 --- /dev/null +++ b/modules/paste/rpaste.go @@ -0,0 +1,2 @@ +// support for rpaste (bpaste) +package paste diff --git a/modules/setting/setting.go b/modules/setting/setting.go new file mode 100644 index 0000000..2eca4db --- /dev/null +++ b/modules/setting/setting.go @@ -0,0 +1,31 @@ +// rpaste settings +// +// This part of the setting package sets default var types and will set other +// important var information, or even load configuration files. Majority of the +// needed defaults are not set here. They are set in the main package. +package setting + +import ( + // ini "gopkg.in/ini.v1" + _ "github.com/rocky-linux/rpaste/modules/utility" +) + +var ( + AppName string + AppBuiltWith string + AppConf string + AppVer string + LexerType string + LifeTime string + PasteBinService string + SysInfo bool +) + +// todo: put in support to find conf file in order: +// -> user's home ~/.rpaste +// -> /etc/rpaste/rpaste.conf +// -> or ignore everything and use custom provided + +//func init() { +// var err error +//} diff --git a/modules/setting/usage.go b/modules/setting/usage.go new file mode 100644 index 0000000..b6341cd --- /dev/null +++ b/modules/setting/usage.go @@ -0,0 +1,21 @@ +package setting + +import ( + "fmt" + "github.com/urfave/cli/v2" +) + +var ( + colorGreen = "\033[01;32m" + colorReset = "\033[0m" + HelpTemplate = "\033[01;32m{{.Name}}\033[0m: {{.Usage}}\n\n" + + "{{.Name}} [options] [filepath]\n" + + "command | {{.Name}} [options]\n\n" + + "Options:\n" + + "{{range .VisibleFlags}}{{.}}\n" + + "{{end}}" +) + +func VersionTemplate(c *cli.Context) { + fmt.Printf("%s%s%s version %s\n\n%s\n", colorGreen, c.App.Name, colorReset, c.App.Version, c.App.UsageText) +} diff --git a/modules/utility/file.go b/modules/utility/file.go new file mode 100644 index 0000000..56d5675 --- /dev/null +++ b/modules/utility/file.go @@ -0,0 +1,38 @@ +// file utility area +// +// This part of the utility package is primarily for file or path checking. +package utility + +import ( + "os" + _ "path" + _ "path/filepath" + _ "strings" + _ "errors" +) + +// Check if this is a file +func IsFile(filePath string) (bool, error) { + f, err := os.Stat(filePath) + if err == nil { + return !f.IsDir(), nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +// Check if this even exists +func IsExist(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil || os.IsExist(err) { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +// todo: add string checking, we can't paste binary diff --git a/rpaste.go b/rpaste.go new file mode 100644 index 0000000..b255d53 --- /dev/null +++ b/rpaste.go @@ -0,0 +1,125 @@ +// Copyright 2022 The rpaste Authors. All rights reserved. +// Use of this source code is governed by an MIT-style license that can be +// found in the LICENSE file of this git repository or release archive. +package main + +// All Imports +import ( + "fmt" + "log" + "os" + "time" + "github.com/urfave/cli/v2" + "github.com/rocky-linux/rpaste/modules/setting" + _ "github.com/rocky-linux/rpaste/modules/paste" +) + +// Sane defaults and basic info +var ( + AppName = "rpaste" + Version = "0.2.0" + DefaultConf = "/etc/rpaste/rpaste.conf" + DefaultLexer = "text" + DefaultLifeTime = "1hour" + DefaultPasteBin = "rpaste" + DefaultSysInfo = false + ShortOption = true +) + +// Initialize default settings +func init() { + setting.AppName = AppName + setting.AppVer = Version + setting.AppConf = DefaultConf + setting.LexerType = DefaultLexer + setting.LifeTime = DefaultLifeTime + setting.PasteBinService = DefaultPasteBin + setting.SysInfo = DefaultSysInfo +} + +func main() { + app := &cli.App{ + Name: AppName, + Usage: "Paste utility originally made for the Rocky paste service", + Version: Version, + // short options should be on + UseShortOptionHandling: ShortOption, + Compiled: time.Now(), + // Explicitly setting these + Reader: os.Stdin, + Writer: os.Stdout, + ErrWriter: os.Stderr, + Copyright: "2022 (c) Louis Abel ", + // Usage text at the bottom to keep the editor from putting me on the wrong + // column. Additional options should be above these comments. + UsageText: `Paste utility used primarily for the Rocky Linux pastebin service. +It can collect system information and forward it to a pastebin or simply +send regular text files. This utility is primarily used for asking for +assistance in the Rocky Linux support venue.`, + } + + // Shorten the help/usage area + cli.AppHelpTemplate = setting.HelpTemplate + cli.VersionPrinter = setting.VersionTemplate + + defaultFlags := []cli.Flag{ + &cli.StringFlag{ + Name: "life", + Aliases: []string{"x"}, + Value: setting.LifeTime, + Usage: "Sets the syntax highlighting", + DefaultText: "1hour", + }, + &cli.StringFlag{ + Name: "type", + Aliases: []string{"t"}, + Value: setting.LexerType, + Usage: "Sets the syntax highlighting", + DefaultText: "text", + }, + &cli.BoolFlag{ + Name: "sysinfo", + Aliases: []string{"s"}, + Value: setting.SysInfo, + Usage: "Collects general system information (disables stdin and file input)", + Required: false, + }, +// &cli.StringFlag{ +// Name: "pastebin", +// Aliases: []string{"p"}, +// Value: setting.PasteBinService, +// Usage: "Decides what pastebin to use", +// }, + } + + // append the default flags to both the main and paste method + app.Flags = append(app.Flags, paste.PasteMethod.Flags...) + app.Flags = append(app.Flags, defaultFlags...) + + // Actions + //app.Before = establishConfExistence + app.Action = paste.PasteMethod.Action + + app.Commands = []*cli.Command { + paste.PasteMethod, + } + + // below is an example to make sure this works + //app.Commands = []*cli.Command { + // { + // Name: "test", + // Aliases: []string{"t"}, + // Usage: "testing", + // Action: func(c *cli.Context) error { + // fmt.Println("testing: ", c.String("type")) + // return nil + // }, + // }, + //} + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} +