36 lines
972 B
Go
36 lines
972 B
Go
package cli
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
const generatedSessionIDPrefix = "notarius:v1:"
|
|
|
|
func runSessionID(explicit commandkit.Supplied[string], inputModule string, rawInput []byte) (string, error) {
|
|
if explicit.Set {
|
|
return strings.TrimSpace(explicit.Value), nil
|
|
}
|
|
return resolvePromptSessionID("", inputModule, rawInput)
|
|
}
|
|
|
|
func resolvePromptSessionID(explicitSessionID, inputModule string, rawInput []byte) (string, error) {
|
|
inputModule = strings.TrimSpace(inputModule)
|
|
if inputModule == "" {
|
|
return "", fmt.Errorf("resolve prompt session: input module key must not be empty")
|
|
}
|
|
if sessionID := strings.TrimSpace(explicitSessionID); sessionID != "" {
|
|
return sessionID, nil
|
|
}
|
|
|
|
hasher := sha256.New()
|
|
_, _ = hasher.Write([]byte(inputModule))
|
|
_, _ = hasher.Write([]byte{0})
|
|
_, _ = hasher.Write(rawInput)
|
|
return generatedSessionIDPrefix + hex.EncodeToString(hasher.Sum(nil)), nil
|
|
}
|