upd: helper utility updated
This commit is contained in:
parent
9de05ac027
commit
0a887fe53c
@ -391,12 +391,16 @@ func obrimCodecEncodeSqids(configuration *obrimCodecConfiguration) (string, erro
|
|||||||
configuration.Salt,
|
configuration.Salt,
|
||||||
)
|
)
|
||||||
|
|
||||||
numbers := make([]uint64, 0)
|
numbers := make([]uint64, 0, len(input))
|
||||||
|
|
||||||
for _, value := range []byte(input) {
|
for _, value := range []byte(input) {
|
||||||
numbers = append(numbers, uint64(value))
|
numbers = append(numbers, uint64(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(numbers) == 0 {
|
||||||
|
return "", errors.New("empty sqids input")
|
||||||
|
}
|
||||||
|
|
||||||
sqidsInstance, err := sqids.New(sqids.Options{
|
sqidsInstance, err := sqids.New(sqids.Options{
|
||||||
Alphabet: configuration.Charset,
|
Alphabet: configuration.Charset,
|
||||||
MinLength: uint8(configuration.Length),
|
MinLength: uint8(configuration.Length),
|
||||||
@ -418,16 +422,27 @@ func obrimCodecDecodeSqids(configuration *obrimCodecConfiguration) (string, erro
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
numbers, err := sqidsInstance.Decode(obrimCodecNormalizeInput(configuration.Data))
|
numbers := sqidsInstance.Decode(
|
||||||
if err != nil {
|
obrimCodecNormalizeInput(configuration.Data),
|
||||||
return "", err
|
)
|
||||||
|
|
||||||
|
if len(numbers) == 0 {
|
||||||
|
return "", errors.New("invalid or empty sqids data")
|
||||||
}
|
}
|
||||||
|
|
||||||
output := make([]byte, 0)
|
output := make([]byte, 0, len(numbers))
|
||||||
|
|
||||||
for _, value := range numbers {
|
for _, value := range numbers {
|
||||||
|
if value > 255 {
|
||||||
|
return "", errors.New("decoded sqids value exceeds byte range")
|
||||||
|
}
|
||||||
|
|
||||||
output = append(output, byte(value))
|
output = append(output, byte(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
return obrimCodecReverseTransform(string(output), configuration.Charset, configuration.Salt), nil
|
return obrimCodecReverseTransform(
|
||||||
|
string(output),
|
||||||
|
configuration.Charset,
|
||||||
|
configuration.Salt,
|
||||||
|
), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,8 +47,17 @@ func obrimLogTimestampGenerate() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogEntryBuild constructs formatted log entries.
|
// obrimLogEntryBuild constructs formatted log entries.
|
||||||
func obrimLogEntryBuild(timestamp string, label string, message string) string {
|
func obrimLogEntryBuild(
|
||||||
return fmt.Sprintf("[%s] [%s] %s\n", timestamp, strings.TrimSpace(label), strings.TrimSpace(message))
|
timestamp string,
|
||||||
|
label string,
|
||||||
|
message string,
|
||||||
|
) string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"[%s] [%s] %s\n",
|
||||||
|
timestamp,
|
||||||
|
strings.TrimSpace(label),
|
||||||
|
strings.TrimSpace(message),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogDirectoryResolve resolves platform-specific log directory paths.
|
// obrimLogDirectoryResolve resolves platform-specific log directory paths.
|
||||||
@ -69,17 +78,30 @@ func obrimLogDirectoryResolve(softwareName string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogFileResolve resolves platform-specific log file paths.
|
// obrimLogFileResolve resolves platform-specific log file paths.
|
||||||
func obrimLogFileResolve(directoryPath string, softwareName string) string {
|
func obrimLogFileResolve(
|
||||||
return filepath.Join(directoryPath, softwareName+obrimLogFileExtension)
|
directoryPath string,
|
||||||
|
softwareName string,
|
||||||
|
) string {
|
||||||
|
return filepath.Join(
|
||||||
|
directoryPath,
|
||||||
|
softwareName+obrimLogFileExtension,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogFileEnsure creates and verifies required log directories and files.
|
// obrimLogFileEnsure creates and verifies required log directories and files.
|
||||||
func obrimLogFileEnsure(directoryPath string, filePath string) error {
|
func obrimLogFileEnsure(
|
||||||
|
directoryPath string,
|
||||||
|
filePath string,
|
||||||
|
) error {
|
||||||
if err := os.MkdirAll(directoryPath, 0o755); err != nil {
|
if err := os.MkdirAll(directoryPath, 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.OpenFile(filePath, os.O_CREATE, 0o644)
|
file, err := os.OpenFile(
|
||||||
|
filePath,
|
||||||
|
os.O_CREATE,
|
||||||
|
0o644,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -88,7 +110,10 @@ func obrimLogFileEnsure(directoryPath string, filePath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogWrite persists log entries to the filesystem.
|
// obrimLogWrite persists log entries to the filesystem.
|
||||||
func obrimLogWrite(filePath string, entry string) error {
|
func obrimLogWrite(
|
||||||
|
filePath string,
|
||||||
|
entry string,
|
||||||
|
) error {
|
||||||
file, err := os.OpenFile(
|
file, err := os.OpenFile(
|
||||||
filePath,
|
filePath,
|
||||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
||||||
@ -107,7 +132,7 @@ func obrimLogWrite(filePath string, entry string) error {
|
|||||||
|
|
||||||
// obrimLogSoftwareNameResolve retrieves the software name from metadata resources.
|
// obrimLogSoftwareNameResolve retrieves the software name from metadata resources.
|
||||||
func obrimLogSoftwareNameResolve() string {
|
func obrimLogSoftwareNameResolve() string {
|
||||||
value := retriever.ObrimRetriever(
|
response := retriever.ObrimRetriever(
|
||||||
"json",
|
"json",
|
||||||
map[string]any{
|
map[string]any{
|
||||||
"resource": obrimLogMetadataResource,
|
"resource": obrimLogMetadataResource,
|
||||||
@ -115,19 +140,32 @@ func obrimLogSoftwareNameResolve() string {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
switch softwareName := value.(type) {
|
status, ok := response["status"].(bool)
|
||||||
case string:
|
if !ok || !status {
|
||||||
return strings.TrimSpace(softwareName)
|
|
||||||
|
|
||||||
default:
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payload, ok := response["payload"].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
softwareName, ok := payload["data"].(string)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(softwareName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogDirectoryResolveLinux resolves Linux log directories.
|
// obrimLogDirectoryResolveLinux resolves Linux log directories.
|
||||||
func obrimLogDirectoryResolveLinux(softwareName string) string {
|
func obrimLogDirectoryResolveLinux(
|
||||||
// Mutable XDG state directory environment value.
|
softwareName string,
|
||||||
xdgStateHome := strings.TrimSpace(os.Getenv("XDG_STATE_HOME"))
|
) string {
|
||||||
|
// Mutable XDG state home environment variable.
|
||||||
|
xdgStateHome := strings.TrimSpace(
|
||||||
|
os.Getenv("XDG_STATE_HOME"),
|
||||||
|
)
|
||||||
|
|
||||||
if xdgStateHome != "" {
|
if xdgStateHome != "" {
|
||||||
return filepath.Join(
|
return filepath.Join(
|
||||||
@ -152,9 +190,13 @@ func obrimLogDirectoryResolveLinux(softwareName string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogDirectoryResolveWindows resolves Windows log directories.
|
// obrimLogDirectoryResolveWindows resolves Windows log directories.
|
||||||
func obrimLogDirectoryResolveWindows(softwareName string) string {
|
func obrimLogDirectoryResolveWindows(
|
||||||
// Mutable local application data directory environment value.
|
softwareName string,
|
||||||
localAppData := strings.TrimSpace(os.Getenv("LOCALAPPDATA"))
|
) string {
|
||||||
|
// Mutable LOCALAPPDATA environment variable.
|
||||||
|
localAppData := strings.TrimSpace(
|
||||||
|
os.Getenv("LOCALAPPDATA"),
|
||||||
|
)
|
||||||
|
|
||||||
if localAppData == "" {
|
if localAppData == "" {
|
||||||
return ""
|
return ""
|
||||||
@ -168,7 +210,9 @@ func obrimLogDirectoryResolveWindows(softwareName string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obrimLogDirectoryResolveMacOS resolves macOS log directories.
|
// obrimLogDirectoryResolveMacOS resolves macOS log directories.
|
||||||
func obrimLogDirectoryResolveMacOS(softwareName string) string {
|
func obrimLogDirectoryResolveMacOS(
|
||||||
|
softwareName string,
|
||||||
|
) string {
|
||||||
homeDirectory, err := os.UserHomeDir()
|
homeDirectory, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user