Added paste command

This commit is contained in:
darmiel 2021-03-27 13:08:53 +01:00
parent c50c70f883
commit 8bd79f3751
6 changed files with 83 additions and 27 deletions

View file

@ -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)))
}

View file

@ -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)))
}

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

5
internal/api/type.go Normal file
View file

@ -0,0 +1,5 @@
package api
type API struct {
ServerURL string
}

36
internal/common/pipe.go Normal file
View file

@ -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
}