cosmos-sdk/client/v2/autocli/flag/binary.go
Mark Rushakoff f8e2c41a48
style: gofumpt client/v2 module (#15725)
## Description

This was causing one set of linter failures.




---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed all author checklist items have been addressed
- [ ] confirmed that this PR does not change production code
2023-04-06 22:51:53 +00:00

64 lines
1.5 KiB
Go

package flag
import (
"context"
"encoding/base64"
"encoding/hex"
"os"
"github.com/cockroachdb/errors"
"google.golang.org/protobuf/reflect/protoreflect"
)
type binaryType struct{}
var _ Value = (*fileBinaryValue)(nil)
func (f binaryType) NewValue(_ context.Context, _ *Builder) Value {
return &fileBinaryValue{}
}
func (f binaryType) DefaultValue() string {
return ""
}
// fileBinaryValue is a Value that holds a binary file.
type fileBinaryValue struct {
value []byte
}
func (f *fileBinaryValue) Get(protoreflect.Value) (protoreflect.Value, error) {
return protoreflect.ValueOfBytes(f.value), nil
}
func (f *fileBinaryValue) String() string {
return string(f.value)
}
// Set implements the flag.Value interface for binary files, with exceptions.
// If the input string is a valid file path, the value will be the content of that file.
// If the input string is a valid hex or base64 string, the value will be the decoded form of that string.
// If the input string is not a valid file path, hex string, or base64 string, Set will return an error.
func (f *fileBinaryValue) Set(s string) error {
if data, err := os.ReadFile(s); err == nil {
f.value = data
return nil
}
if data, err := hex.DecodeString(s); err == nil {
f.value = data
return nil
}
if data, err := base64.StdEncoding.DecodeString(s); err == nil {
f.value = data
return nil
}
return errors.New("input string is neither a valid file path, hex, or base64 encoded")
}
func (f *fileBinaryValue) Type() string {
return "binary"
}