From 8bd79f3751b34d01ba4e3186f4de8a36d87ab4d1 Mon Sep 17 00:00:00 2001 From: darmiel <71837281+darmiel@users.noreply.github.com> Date: Sat, 27 Mar 2021 13:08:53 +0100 Subject: [PATCH] Added `paste` command --- cmd/root.go | 39 ++++++++++++++++++++++++++++++++++++++- cmd/serve.go | 27 +-------------------------- go.mod | 1 + go.sum | 2 ++ internal/api/type.go | 5 +++++ internal/common/pipe.go | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 internal/api/type.go create mode 100644 internal/common/pipe.go diff --git a/cmd/root.go b/cmd/root.go index 39424e1..ff9651a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,8 +17,10 @@ package cmd import ( "fmt" + "github.com/darmiel/yaxc/internal/api" "github.com/spf13/cobra" "os" + "time" "github.com/mitchellh/go-homedir" "github.com/spf13/viper" @@ -28,6 +30,11 @@ var ( cfgFile string ) +var ( + settingServer string + Api *api.API +) + // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "yaxc", @@ -37,13 +44,16 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { + Api = &api.API{ + ServerURL: settingServer, + } cobra.CheckErr(rootCmd.Execute()) } func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.yaxc.yaml)") - rootCmd.PersistentFlags().String("server", "", "URL of API-Server") + rootCmd.PersistentFlags().StringVar(&settingServer, "server", "", "URL of API-Server") cobra.CheckErr(viper.BindPFlag("server", rootCmd.PersistentFlags().Lookup("server"))) } @@ -69,3 +79,30 @@ func initConfig() { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } } + +// misc + +func regStrP(cmd *cobra.Command, name, shorthand, def, usage string) { + cmd.PersistentFlags().StringP(name, shorthand, def, usage) + cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) +} +func regStr(cmd *cobra.Command, name, def, usage string) { + cmd.PersistentFlags().String(name, def, usage) + cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) +} +func regDurP(cmd *cobra.Command, name, shorthand string, def time.Duration, usage string) { + cmd.PersistentFlags().DurationP(name, shorthand, def, usage) + cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) +} +func regIntP(cmd *cobra.Command, name, shorthand string, def int, usage string) { + cmd.PersistentFlags().IntP(name, shorthand, def, usage) + cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) +} +func regInt(cmd *cobra.Command, name string, def int, usage string) { + cmd.PersistentFlags().Int(name, def, usage) + cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) +} +func regBoolP(cmd *cobra.Command, name, shorthand string, def bool, usage string) { + cmd.PersistentFlags().BoolP(name, shorthand, def, usage) + cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) +} diff --git a/cmd/serve.go b/cmd/serve.go index 16057fc..718a542 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -105,35 +105,10 @@ func init() { // ttl regDurP(serveCmd, "default-ttl", "t", 60*time.Second, "Default TTL") regDurP(serveCmd, "min-ttl", "l", 5*time.Second, "Min TTL") - regDurP(serveCmd, "max-ttl", "s", 5*time.Minute, "Max TTL") + regDurP(serveCmd, "max-ttl", "s", 60*time.Minute, "Max TTL") // other regIntP(serveCmd, "max-body-length", "x", 1024, "Max Body Length") regBoolP(serveCmd, "enable-encryption", "e", true, "Enable Encryption") regStr(serveCmd, "proxy-header", "", "Proxy Header") } - -func regStrP(cmd *cobra.Command, name, shorthand, def, usage string) { - cmd.PersistentFlags().StringP(name, shorthand, def, usage) - cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) -} -func regStr(cmd *cobra.Command, name, def, usage string) { - cmd.PersistentFlags().String(name, def, usage) - cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) -} -func regDurP(cmd *cobra.Command, name, shorthand string, def time.Duration, usage string) { - cmd.PersistentFlags().DurationP(name, shorthand, def, usage) - cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) -} -func regIntP(cmd *cobra.Command, name, shorthand string, def int, usage string) { - cmd.PersistentFlags().IntP(name, shorthand, def, usage) - cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) -} -func regInt(cmd *cobra.Command, name string, def int, usage string) { - cmd.PersistentFlags().Int(name, def, usage) - cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) -} -func regBoolP(cmd *cobra.Command, name, shorthand string, def bool, usage string) { - cmd.PersistentFlags().BoolP(name, shorthand, def, usage) - cobra.CheckErr(viper.BindPFlag(name, cmd.PersistentFlags().Lookup(name))) -} diff --git a/go.mod b/go.mod index d70d6ff..eab6434 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/go-redis/redis/v8 v8.8.0 github.com/gofiber/fiber/v2 v2.6.0 + github.com/imroc/req v0.3.0 github.com/magiconair/properties v1.8.4 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.4.1 // indirect diff --git a/go.sum b/go.sum index 0082f73..28f51bb 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U= +github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= diff --git a/internal/api/type.go b/internal/api/type.go new file mode 100644 index 0000000..347f9f4 --- /dev/null +++ b/internal/api/type.go @@ -0,0 +1,5 @@ +package api + +type API struct { + ServerURL string +} diff --git a/internal/common/pipe.go b/internal/common/pipe.go new file mode 100644 index 0000000..84b7535 --- /dev/null +++ b/internal/common/pipe.go @@ -0,0 +1,36 @@ +package common + +import ( + "bufio" + "errors" + "io" + "os" +) + +var NotPiped = errors.New("not piped") + +func ReadPipe() (res string, err error) { + var info os.FileInfo + if info, err = os.Stdin.Stat(); err != nil { + return + } + + if info.Mode()&os.ModeCharDevice != 0 || info.Size() <= 0 { + err = NotPiped + return + } + + reader := bufio.NewReader(os.Stdin) + var output []rune + + for { + input, _, err := reader.ReadRune() + if err != nil && err == io.EOF { + break + } + output = append(output, input) + } + + res = string(output) + return +}