-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (98 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"flag"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
"github.com/jhillyerd/labcoat/internal/config"
"github.com/jhillyerd/labcoat/internal/nix"
"github.com/jhillyerd/labcoat/internal/ui"
)
func main() {
var (
help = flag.Bool("help", false, "Print argument help message")
configPathOpt = flag.String("config", "", "Path to TOML config file")
defaults = flag.Bool("defaults", false, "Prints default configuration to stdout and exits")
logPath = flag.String("log", "", "File to write debug logs to")
)
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
}
if *defaults {
config.PrintDefaults()
os.Exit(0)
}
// Init logging.
if *logPath != "" {
lf, err := tea.LogToFile(*logPath, "")
if err != nil {
fmt.Fprintln(os.Stderr, "logging error: ", err)
os.Exit(1)
}
defer lf.Close()
slog.SetLogLoggerLevel(slog.LevelDebug)
slog.Info("### STARTUP ###################################################################")
} else {
// Prevent log output corrupting UI.
log.SetOutput(io.Discard)
}
// Load config file if present.
var configPath string
var configMustExist bool
if *configPathOpt == "" {
configPath = defaultConfigPath()
} else {
configPath = *configPathOpt
configMustExist = true
}
conf, err := config.Load(configPath, configMustExist)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read config: %v\n", err)
os.Exit(1)
}
// Load host list from flake.
flakePath := flag.Arg(0)
if flakePath == "" {
flakePath, err = os.Getwd()
} else {
flakePath, err = filepath.Abs(flakePath)
}
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
hosts, nerr := nix.GetNames(nix.NamesRequest{FlakePath: flakePath})
if nerr != nil {
fmt.Fprintln(os.Stderr, nerr.Error())
os.Exit(1)
}
if len(hosts) == 0 {
fmt.Fprintln(os.Stderr, "No hosts (nixosConfigurations) found in flake")
os.Exit(1)
}
// Launch UI.
p := tea.NewProgram(ui.New(*conf, config.DefaultKeyMap, flakePath, hosts), tea.WithAltScreen())
go p.Send(p)
if _, err := p.Run(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func defaultConfigPath() string {
configRoot := os.Getenv("XDG_CONFIG_HOME")
if configRoot == "" {
home := os.Getenv("HOME")
if home == "" {
fmt.Fprintln(os.Stderr, "Neither $XDG_CONFIG_HOME or $HOME available")
os.Exit(1)
}
configRoot = filepath.Join(home, ".config")
}
return filepath.Join(configRoot, "labcoat", "config.toml")
}