From fc134ce0b97fbc870ecebe652ade39165baf6cb2 Mon Sep 17 00:00:00 2001 From: Victor Date: Sun, 4 Aug 2024 08:42:03 +0000 Subject: [PATCH] ArgCheck --- cmd/client/main.go | 23 +++++++++++++---------- pkg/utils/argcheckers.go | 26 ++++++++++++++++++++++++++ yproxy.yaml | 10 ---------- 3 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 pkg/utils/argcheckers.go delete mode 100644 yproxy.yaml diff --git a/cmd/client/main.go b/cmd/client/main.go index a462a71..8a84c1a 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -7,6 +7,7 @@ import ( "os" "github.com/yezzey-gp/yproxy/pkg/storage" + "github.com/yezzey-gp/yproxy/pkg/utils" "github.com/spf13/cobra" "github.com/yezzey-gp/yproxy/config" @@ -24,9 +25,14 @@ var encrypt bool var offset uint64 // TODOV -func Prepare(f func(net.Conn, *config.Instance, []string) error) func(*cobra.Command, []string) error { +func Runner(f func(net.Conn, *config.Instance, []string) error, argChecker func([]string) error) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { - err := config.LoadInstanceConfig(cfgPath) + err := argChecker(args) + if err != nil { + return err + } + err = config.LoadInstanceConfig(cfgPath) if err != nil { return err } @@ -141,14 +147,11 @@ func putFunc(con net.Conn, instanceCnf *config.Instance, args []string) error { } if tp == message.MessageTypeReadyForQuery { - // ok - ylogger.Zero.Debug().Msg("got rfq") + return nil } else { return fmt.Errorf("failed to get rfq") } - return nil - } func listFunc(con net.Conn, instanceCnf *config.Instance, args []string) error { @@ -203,25 +206,25 @@ var rootCmd = &cobra.Command{ var catCmd = &cobra.Command{ Use: "cat", Short: "cat", - RunE: Prepare(catFunc), + RunE: Runner(catFunc, utils.OneArg), } var copyCmd = &cobra.Command{ Use: "copy", Short: "copy", - RunE: Prepare(copyFunc), + RunE: Runner(copyFunc, utils.OneArg), } var putCmd = &cobra.Command{ Use: "put", Short: "put", - RunE: Prepare(putFunc), + RunE: Runner(putFunc, utils.OneArg), } var listCmd = &cobra.Command{ Use: "list", Short: "list", - RunE: Prepare(listFunc), + RunE: Runner(listFunc, utils.OneArg), } func init() { diff --git a/pkg/utils/argcheckers.go b/pkg/utils/argcheckers.go new file mode 100644 index 0000000..fb0444e --- /dev/null +++ b/pkg/utils/argcheckers.go @@ -0,0 +1,26 @@ +package utils + +import "fmt" + +func countCheck(args []string, count int64) error { + if len(args) != int(count) { + return fmt.Errorf("invalid number of arguments, %d expected, %d received", count, len(args)) + } else { + return nil + } +} +func lessThan(args []string, count int64) error { + if len(args) > int(count) { + return fmt.Errorf("invalid number of arguments, %d<= expected, %d received", count, len(args)) + } else { + return nil + } +} + +func ZeroArg(args []string) error { + return countCheck(args, 0) +} + +func OneArg(args []string) error { + return countCheck(args, 1) +} diff --git a/yproxy.yaml b/yproxy.yaml deleted file mode 100644 index 1760234..0000000 --- a/yproxy.yaml +++ /dev/null @@ -1,10 +0,0 @@ -socket_path: "/tmp/yproxy.sock" - -storage: - # S3 data for Yandex Cloud - access_key_id: "YCBpaste_your_key" - secret_access_key: "YCP-paste_your_key" - storage_endpoint: "https://storage.yandexcloud.net" - storage_prefix: "wal-e/6/" - storage_region: "us-west-2" - storage_bucket: "your_s3_bucket"