50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
)
|
|
|
|
func resolveSessionConfigPath(flagValue string) (string, error) {
|
|
return resolveSessionConfigPathWithCandidates(flagValue, config.DefaultSessionConfigSearchPaths)
|
|
}
|
|
|
|
func resolveSessionConfigPathWithCandidates(flagValue string, candidates []string) (string, error) {
|
|
if explicit := strings.TrimSpace(flagValue); explicit != "" {
|
|
return explicit, nil
|
|
}
|
|
|
|
ordered := make([]string, 0, len(candidates))
|
|
for _, raw := range candidates {
|
|
path := strings.TrimSpace(raw)
|
|
if path == "" {
|
|
continue
|
|
}
|
|
ordered = append(ordered, path)
|
|
info, err := os.Stat(path)
|
|
if err == nil {
|
|
if info.IsDir() {
|
|
continue
|
|
}
|
|
return filepath.Clean(path), nil
|
|
}
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
continue
|
|
}
|
|
return "", fmt.Errorf("check default session config %q: %w", path, err)
|
|
}
|
|
|
|
if len(ordered) == 0 {
|
|
return "", fmt.Errorf("no session config path provided and no default locations configured")
|
|
}
|
|
return "", fmt.Errorf(
|
|
"no session config path provided and no default session config found; searched: %s; pass --session to use an explicit path",
|
|
strings.Join(ordered, ", "),
|
|
)
|
|
}
|