forked from TrueCloudLab/frostfs-node
[#165] cli: Refactor put command
Resolve funlen linter for putObject method Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
c78e9cc857
commit
c2cf708e0e
1 changed files with 65 additions and 48 deletions
|
@ -58,7 +58,6 @@ func initObjectPutCmd() {
|
|||
flags.Bool(binaryFlag, false, "Deserialize object structure from given file.")
|
||||
}
|
||||
|
||||
// nolint: funlen
|
||||
func putObject(cmd *cobra.Command, _ []string) {
|
||||
binary, _ := cmd.Flags().GetBool(binaryFlag)
|
||||
cidVal, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
||||
|
@ -80,42 +79,13 @@ func putObject(cmd *cobra.Command, _ []string) {
|
|||
obj := object.New()
|
||||
|
||||
if binary {
|
||||
buf, err := os.ReadFile(filename)
|
||||
commonCmd.ExitOnErr(cmd, "unable to read given file: %w", err)
|
||||
objTemp := object.New()
|
||||
// TODO(@acid-ant): #1932 Use streams to marshal/unmarshal payload
|
||||
commonCmd.ExitOnErr(cmd, "can't unmarshal object from given file: %w", objTemp.Unmarshal(buf))
|
||||
payloadReader = bytes.NewReader(objTemp.Payload())
|
||||
cnr, _ = objTemp.ContainerID()
|
||||
ownerID = *objTemp.OwnerID()
|
||||
payloadReader, cnr, ownerID = readFilePayload(filename, cmd)
|
||||
} else {
|
||||
readCID(cmd, &cnr)
|
||||
user.IDFromKey(&ownerID, pk.PublicKey)
|
||||
}
|
||||
|
||||
attrs, err := parseObjectAttrs(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "can't parse object attributes: %w", err)
|
||||
|
||||
expiresOn, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
|
||||
if expiresOn > 0 {
|
||||
var expAttrFound bool
|
||||
expAttrValue := strconv.FormatUint(expiresOn, 10)
|
||||
|
||||
for i := range attrs {
|
||||
if attrs[i].Key() == objectV2.SysAttributeExpEpoch {
|
||||
attrs[i].SetValue(expAttrValue)
|
||||
expAttrFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !expAttrFound {
|
||||
index := len(attrs)
|
||||
attrs = append(attrs, object.Attribute{})
|
||||
attrs[index].SetKey(objectV2.SysAttributeExpEpoch)
|
||||
attrs[index].SetValue(expAttrValue)
|
||||
}
|
||||
}
|
||||
attrs := getAllObjectAttributes(cmd)
|
||||
|
||||
obj.SetContainerID(cnr)
|
||||
obj.SetOwnerID(&ownerID)
|
||||
|
@ -140,23 +110,9 @@ func putObject(cmd *cobra.Command, _ []string) {
|
|||
prm.SetPayloadReader(payloadReader)
|
||||
} else {
|
||||
if binary {
|
||||
p = pb.New(len(obj.Payload()))
|
||||
p.Output = cmd.OutOrStdout()
|
||||
prm.SetPayloadReader(p.NewProxyReader(payloadReader))
|
||||
prm.SetHeaderCallback(func(o *object.Object) { p.Start() })
|
||||
p = setBinaryPayloadReader(cmd, obj, &prm, payloadReader)
|
||||
} else {
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
cmd.PrintErrf("Failed to get file size, progress bar is disabled: %v\n", err)
|
||||
prm.SetPayloadReader(f)
|
||||
} else {
|
||||
p = pb.New64(fi.Size())
|
||||
p.Output = cmd.OutOrStdout()
|
||||
prm.SetPayloadReader(p.NewProxyReader(f))
|
||||
prm.SetHeaderCallback(func(o *object.Object) {
|
||||
p.Start()
|
||||
})
|
||||
}
|
||||
p = setFilePayloadReader(cmd, f, &prm)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,6 +126,67 @@ func putObject(cmd *cobra.Command, _ []string) {
|
|||
cmd.Printf(" OID: %s\n CID: %s\n", res.ID(), cnr)
|
||||
}
|
||||
|
||||
func readFilePayload(filename string, cmd *cobra.Command) (io.Reader, cid.ID, user.ID) {
|
||||
buf, err := os.ReadFile(filename)
|
||||
commonCmd.ExitOnErr(cmd, "unable to read given file: %w", err)
|
||||
objTemp := object.New()
|
||||
// TODO(@acid-ant): #1932 Use streams to marshal/unmarshal payload
|
||||
commonCmd.ExitOnErr(cmd, "can't unmarshal object from given file: %w", objTemp.Unmarshal(buf))
|
||||
payloadReader := bytes.NewReader(objTemp.Payload())
|
||||
cnr, _ := objTemp.ContainerID()
|
||||
ownerID := *objTemp.OwnerID()
|
||||
return payloadReader, cnr, ownerID
|
||||
}
|
||||
|
||||
func setFilePayloadReader(cmd *cobra.Command, f *os.File, prm *internalclient.PutObjectPrm) *pb.ProgressBar {
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
cmd.PrintErrf("Failed to get file size, progress bar is disabled: %v\n", err)
|
||||
prm.SetPayloadReader(f)
|
||||
return nil
|
||||
}
|
||||
p := pb.New64(fi.Size())
|
||||
p.Output = cmd.OutOrStdout()
|
||||
prm.SetPayloadReader(p.NewProxyReader(f))
|
||||
prm.SetHeaderCallback(func(o *object.Object) { p.Start() })
|
||||
return p
|
||||
}
|
||||
|
||||
func setBinaryPayloadReader(cmd *cobra.Command, obj *object.Object, prm *internalclient.PutObjectPrm, payloadReader io.Reader) *pb.ProgressBar {
|
||||
p := pb.New(len(obj.Payload()))
|
||||
p.Output = cmd.OutOrStdout()
|
||||
prm.SetPayloadReader(p.NewProxyReader(payloadReader))
|
||||
prm.SetHeaderCallback(func(o *object.Object) { p.Start() })
|
||||
return p
|
||||
}
|
||||
|
||||
func getAllObjectAttributes(cmd *cobra.Command) []object.Attribute {
|
||||
attrs, err := parseObjectAttrs(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "can't parse object attributes: %w", err)
|
||||
|
||||
expiresOn, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
|
||||
if expiresOn > 0 {
|
||||
var expAttrFound bool
|
||||
expAttrValue := strconv.FormatUint(expiresOn, 10)
|
||||
|
||||
for i := range attrs {
|
||||
if attrs[i].Key() == objectV2.SysAttributeExpEpoch {
|
||||
attrs[i].SetValue(expAttrValue)
|
||||
expAttrFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !expAttrFound {
|
||||
index := len(attrs)
|
||||
attrs = append(attrs, object.Attribute{})
|
||||
attrs[index].SetKey(objectV2.SysAttributeExpEpoch)
|
||||
attrs[index].SetValue(expAttrValue)
|
||||
}
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
func parseObjectAttrs(cmd *cobra.Command) ([]object.Attribute, error) {
|
||||
var rawAttrs []string
|
||||
|
||||
|
|
Loading…
Reference in a new issue