forked from TrueCloudLab/frostfs-node
[#278] cli: Support request X-Headers
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
e53bf574b5
commit
51b85b0a73
5 changed files with 105 additions and 45 deletions
|
@ -7,8 +7,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||
crypto "github.com/nspcc-dev/neofs-crypto"
|
||||
|
@ -25,6 +27,10 @@ const (
|
|||
ttlDefaultValue = 2
|
||||
)
|
||||
|
||||
const xHeadersFlag = "xhdr"
|
||||
|
||||
var xHeaders []string
|
||||
|
||||
// Global scope flags.
|
||||
var (
|
||||
cfgFile string
|
||||
|
@ -80,6 +86,10 @@ func init() {
|
|||
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
|
||||
|
||||
rootCmd.PersistentFlags().StringSliceVarP(&xHeaders, xHeadersFlag, "x", nil,
|
||||
"Request X-Headers in form of Key=Value")
|
||||
_ = viper.BindPFlag(xHeadersFlag, rootCmd.PersistentFlags().Lookup(xHeadersFlag))
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
|
@ -194,3 +204,35 @@ func printVerbose(format string, a ...interface{}) {
|
|||
fmt.Printf(format+"\n", a...)
|
||||
}
|
||||
}
|
||||
|
||||
func parseXHeaders() []*pkg.XHeader {
|
||||
xs := make([]*pkg.XHeader, 0, len(xHeaders))
|
||||
|
||||
for i := range xHeaders {
|
||||
kv := strings.SplitN(xHeaders[i], "=", 2)
|
||||
if len(kv) != 2 {
|
||||
panic(fmt.Errorf("invalid X-Header format: %s", xHeaders[i]))
|
||||
}
|
||||
|
||||
x := pkg.NewXHeader()
|
||||
x.SetKey(kv[0])
|
||||
x.SetValue(kv[1])
|
||||
|
||||
xs = append(xs, x)
|
||||
}
|
||||
|
||||
return xs
|
||||
}
|
||||
|
||||
func globalCallOptions() []client.CallOption {
|
||||
xHdrs := parseXHeaders()
|
||||
|
||||
opts := make([]client.CallOption, 0, len(xHdrs)+1) // + TTL
|
||||
opts = append(opts, client.WithTTL(getTTL()))
|
||||
|
||||
for i := range xHdrs {
|
||||
opts = append(opts, client.WithXHeader(xHdrs[i]))
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue