Resolve all deferred items from pass 1 and deeper violations found in this pass: rename abbreviated variables (a, e, f, s, ts, inp, inputStr, firstTS/lastTS, argv, id) to full descriptive names; add usage-example comments to all unexported helpers (parseFromReader, extractToolInput, extractResultContent, truncate, shortID, formatDuration, generateTape, extractCommand, lookupExecutable, isExecutablePath, runCommand, resultError, repeatString, containsAny, indexOf, trimQuotes); rename single-letter helper parameters to match AX principle 1. Co-Authored-By: Virgil <virgil@lethean.io>
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
// SPDX-Licence-Identifier: EUPL-1.2
|
|
package session
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
core "dappco.re/go/core"
|
|
)
|
|
|
|
var hostFS = (&core.Fs{}).NewUnrestricted()
|
|
|
|
type rawJSON []byte
|
|
|
|
func (m *rawJSON) UnmarshalJSON(data []byte) error {
|
|
if m == nil {
|
|
return core.E("rawJSON.UnmarshalJSON", "nil receiver", nil)
|
|
}
|
|
*m = append((*m)[:0], data...)
|
|
return nil
|
|
}
|
|
|
|
func (m rawJSON) MarshalJSON() ([]byte, error) {
|
|
if m == nil {
|
|
return []byte("null"), nil
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// resultError extracts the error from a core.Result, or constructs one if absent.
|
|
//
|
|
// err := resultError(hostFS.Write(path, content))
|
|
func resultError(result core.Result) error {
|
|
if result.OK {
|
|
return nil
|
|
}
|
|
if err, ok := result.Value.(error); ok && err != nil {
|
|
return err
|
|
}
|
|
return core.E("resultError", "unexpected core result failure", nil)
|
|
}
|
|
|
|
// repeatString returns text repeated count times.
|
|
//
|
|
// repeatString("=", 50) // "=================================================="
|
|
func repeatString(text string, count int) string {
|
|
if text == "" || count <= 0 {
|
|
return ""
|
|
}
|
|
return string(bytes.Repeat([]byte(text), count))
|
|
}
|
|
|
|
// containsAny reports whether text contains any rune in chars.
|
|
//
|
|
// containsAny("/tmp/file", `/\`) // true
|
|
func containsAny(text, chars string) bool {
|
|
for _, character := range chars {
|
|
if bytes.IndexRune([]byte(text), character) >= 0 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// indexOf returns the byte offset of substr in text, or -1 if not found.
|
|
//
|
|
// indexOf("hello world", "world") // 6
|
|
func indexOf(text, substr string) int {
|
|
return bytes.Index([]byte(text), []byte(substr))
|
|
}
|
|
|
|
// trimQuotes strips a single layer of matching double-quotes or back-ticks.
|
|
//
|
|
// trimQuotes(`"hello"`) // "hello"
|
|
func trimQuotes(text string) string {
|
|
if len(text) < 2 {
|
|
return text
|
|
}
|
|
if (text[0] == '"' && text[len(text)-1] == '"') || (text[0] == '`' && text[len(text)-1] == '`') {
|
|
return text[1 : len(text)-1]
|
|
}
|
|
return text
|
|
}
|