2024-07-11 16:39:54 +00:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2024-10-07 15:32:26 +00:00
|
|
|
"time"
|
2024-07-11 16:39:54 +00:00
|
|
|
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/mr-tron/base58"
|
2024-10-07 15:32:26 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2024-07-11 16:39:54 +00:00
|
|
|
)
|
|
|
|
|
2024-10-07 15:32:26 +00:00
|
|
|
func OpenDB(path string, writable bool) (*bbolt.DB, error) {
|
|
|
|
db, err := bbolt.Open(path, 0o600, &bbolt.Options{
|
|
|
|
ReadOnly: !writable,
|
|
|
|
Timeout: 100 * time.Millisecond,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2024-07-11 16:39:54 +00:00
|
|
|
func CIDParser(s string) (any, error) {
|
|
|
|
data, err := base58.Decode(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var id cid.ID
|
|
|
|
if err = id.Decode(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func OIDParser(s string) (any, error) {
|
|
|
|
data, err := base58.Decode(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var id oid.ID
|
|
|
|
if err = id.Decode(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddressParser(s string) (map[string]any, error) {
|
|
|
|
m := make(map[string]any)
|
|
|
|
|
|
|
|
parts := strings.Split(s, "/")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, errors.New("expected <cid>/<oid>")
|
|
|
|
}
|
|
|
|
cnr, err := CIDParser(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
obj, err := OIDParser(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m["cid"] = cnr
|
|
|
|
m["oid"] = obj
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyParser(s string) (any, error) {
|
|
|
|
if s == "" {
|
|
|
|
return nil, errors.New("empty attribute key")
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func valueParser(s string) (any, error) {
|
|
|
|
if s == "" {
|
|
|
|
return nil, errors.New("empty attribute value")
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AttributeParser(s string) (map[string]any, error) {
|
|
|
|
m := make(map[string]any)
|
|
|
|
|
|
|
|
parts := strings.Split(s, "/")
|
|
|
|
if len(parts) != 1 && len(parts) != 2 {
|
|
|
|
return nil, errors.New("expected <key> or <key>/<value>")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := keyParser(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["key"] = key
|
|
|
|
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := valueParser(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["value"] = value
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|