upd: helper utility updated
This commit is contained in:
parent
57c1acacdc
commit
bf183d076c
@ -391,12 +391,16 @@ func obrimCodecEncodeSqids(configuration *obrimCodecConfiguration) (string, erro
|
||||
configuration.Salt,
|
||||
)
|
||||
|
||||
numbers := make([]uint64, 0)
|
||||
numbers := make([]uint64, 0, len(input))
|
||||
|
||||
for _, value := range []byte(input) {
|
||||
numbers = append(numbers, uint64(value))
|
||||
}
|
||||
|
||||
if len(numbers) == 0 {
|
||||
return "", errors.New("empty sqids input")
|
||||
}
|
||||
|
||||
sqidsInstance, err := sqids.New(sqids.Options{
|
||||
Alphabet: configuration.Charset,
|
||||
MinLength: uint8(configuration.Length),
|
||||
@ -418,16 +422,27 @@ func obrimCodecDecodeSqids(configuration *obrimCodecConfiguration) (string, erro
|
||||
return "", err
|
||||
}
|
||||
|
||||
numbers, err := sqidsInstance.Decode(obrimCodecNormalizeInput(configuration.Data))
|
||||
if err != nil {
|
||||
return "", err
|
||||
numbers := sqidsInstance.Decode(
|
||||
obrimCodecNormalizeInput(configuration.Data),
|
||||
)
|
||||
|
||||
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 {
|
||||
if value > 255 {
|
||||
return "", errors.New("decoded sqids value exceeds byte range")
|
||||
}
|
||||
|
||||
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.
|
||||
func obrimLogEntryBuild(timestamp string, label string, message string) string {
|
||||
return fmt.Sprintf("[%s] [%s] %s\n", timestamp, strings.TrimSpace(label), strings.TrimSpace(message))
|
||||
func obrimLogEntryBuild(
|
||||
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.
|
||||
@ -69,17 +78,30 @@ func obrimLogDirectoryResolve(softwareName string) string {
|
||||
}
|
||||
|
||||
// obrimLogFileResolve resolves platform-specific log file paths.
|
||||
func obrimLogFileResolve(directoryPath string, softwareName string) string {
|
||||
return filepath.Join(directoryPath, softwareName+obrimLogFileExtension)
|
||||
func obrimLogFileResolve(
|
||||
directoryPath string,
|
||||
softwareName string,
|
||||
) string {
|
||||
return filepath.Join(
|
||||
directoryPath,
|
||||
softwareName+obrimLogFileExtension,
|
||||
)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filePath, os.O_CREATE, 0o644)
|
||||
file, err := os.OpenFile(
|
||||
filePath,
|
||||
os.O_CREATE,
|
||||
0o644,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -88,7 +110,10 @@ func obrimLogFileEnsure(directoryPath string, filePath string) error {
|
||||
}
|
||||
|
||||
// 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(
|
||||
filePath,
|
||||
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.
|
||||
func obrimLogSoftwareNameResolve() string {
|
||||
value := retriever.ObrimRetriever(
|
||||
response := retriever.ObrimRetriever(
|
||||
"json",
|
||||
map[string]any{
|
||||
"resource": obrimLogMetadataResource,
|
||||
@ -115,19 +140,32 @@ func obrimLogSoftwareNameResolve() string {
|
||||
},
|
||||
)
|
||||
|
||||
switch softwareName := value.(type) {
|
||||
case string:
|
||||
return strings.TrimSpace(softwareName)
|
||||
|
||||
default:
|
||||
status, ok := response["status"].(bool)
|
||||
if !ok || !status {
|
||||
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.
|
||||
func obrimLogDirectoryResolveLinux(softwareName string) string {
|
||||
// Mutable XDG state directory environment value.
|
||||
xdgStateHome := strings.TrimSpace(os.Getenv("XDG_STATE_HOME"))
|
||||
func obrimLogDirectoryResolveLinux(
|
||||
softwareName string,
|
||||
) string {
|
||||
// Mutable XDG state home environment variable.
|
||||
xdgStateHome := strings.TrimSpace(
|
||||
os.Getenv("XDG_STATE_HOME"),
|
||||
)
|
||||
|
||||
if xdgStateHome != "" {
|
||||
return filepath.Join(
|
||||
@ -152,9 +190,13 @@ func obrimLogDirectoryResolveLinux(softwareName string) string {
|
||||
}
|
||||
|
||||
// obrimLogDirectoryResolveWindows resolves Windows log directories.
|
||||
func obrimLogDirectoryResolveWindows(softwareName string) string {
|
||||
// Mutable local application data directory environment value.
|
||||
localAppData := strings.TrimSpace(os.Getenv("LOCALAPPDATA"))
|
||||
func obrimLogDirectoryResolveWindows(
|
||||
softwareName string,
|
||||
) string {
|
||||
// Mutable LOCALAPPDATA environment variable.
|
||||
localAppData := strings.TrimSpace(
|
||||
os.Getenv("LOCALAPPDATA"),
|
||||
)
|
||||
|
||||
if localAppData == "" {
|
||||
return ""
|
||||
@ -168,7 +210,9 @@ func obrimLogDirectoryResolveWindows(softwareName string) string {
|
||||
}
|
||||
|
||||
// obrimLogDirectoryResolveMacOS resolves macOS log directories.
|
||||
func obrimLogDirectoryResolveMacOS(softwareName string) string {
|
||||
func obrimLogDirectoryResolveMacOS(
|
||||
softwareName string,
|
||||
) string {
|
||||
homeDirectory, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user