bluesky viewer in the terminal
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "runtime"
7)
8
9const appName = "skycli"
10
11// GetConfigDir returns the platform-specific configuration directory path.
12// On Unix-like systems: ~/.skycli or Windows: %APPDATA%/skycli
13func GetConfigDir() (string, error) {
14 var baseDir string
15
16 if runtime.GOOS == "windows" {
17 baseDir = os.Getenv("APPDATA")
18 if baseDir == "" {
19 return "", &PathError{Op: "GetConfigDir", Err: "APPDATA environment variable not set"}
20 }
21 } else {
22 homeDir, err := os.UserHomeDir()
23 if err != nil {
24 return "", &PathError{Op: "GetConfigDir", Err: err.Error()}
25 }
26 baseDir = homeDir
27 }
28
29 if runtime.GOOS == "windows" {
30 return filepath.Join(baseDir, appName), nil
31 }
32 return filepath.Join(baseDir, "."+appName), nil
33}
34
35// GetConfigFile returns the full path to the configuration file.
36// Returns: ~/.skycli/.config.json (Unix) or %APPDATA%/skycli/.config.json (Windows)
37func GetConfigFile() (string, error) {
38 configDir, err := GetConfigDir()
39 if err != nil {
40 return "", err
41 }
42 return filepath.Join(configDir, ".config.json"), nil
43}
44
45// GetCacheDB returns the full path to the SQLite cache database.
46// Returns: ~/.skycli/cache.db (Unix) or %APPDATA%/skycli/cache.db (Windows)
47func GetCacheDB() (string, error) {
48 configDir, err := GetConfigDir()
49 if err != nil {
50 return "", err
51 }
52 return filepath.Join(configDir, "cache.db"), nil
53}
54
55// EnsureConfigDir creates the configuration directory if it doesn't exist.
56// Sets permissions to 0700 (owner read/write/execute only) for security.
57func EnsureConfigDir() error {
58 configDir, err := GetConfigDir()
59 if err != nil {
60 return err
61 }
62
63 if err := os.MkdirAll(configDir, 0700); err != nil {
64 return &PathError{Op: "EnsureConfigDir", Err: err.Error()}
65 }
66
67 return nil
68}
69
70// PathError represents an error that occurred during path operations
71type PathError struct {
72 Op string
73 Err string
74}
75
76func (e *PathError) Error() string {
77 return e.Op + ": " + e.Err
78}
79
80var _ error = &PathError{}