Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect configured DeployHostAttr #6

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/pelletier/go-toml/v2"
Expand Down Expand Up @@ -80,7 +81,7 @@ func Load(path string) (*Config, error) {
b, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Use defaults when no config file present.
slog.Warn("Config file not found, using defaults", "path", path)
return &conf, nil
}
return nil, err
Expand All @@ -90,5 +91,6 @@ func Load(path string) (*Config, error) {
return nil, err
}

slog.Debug("Loaded config", "path", path)
return &conf, nil
}
7 changes: 6 additions & 1 deletion internal/nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"os/exec"
"text/template"

"github.com/jhillyerd/labcoat/internal/config"
)

const namesScript = `
Expand Down Expand Up @@ -43,7 +46,7 @@ const targetInfoScript = `
target = flake.nixosConfigurations.${key};
in
{
deployHost = target.config.networking.fqdnOrHostName;
deployHost = {{ .Config.Hosts.DeployHostAttr }};
}
`

Expand All @@ -52,6 +55,7 @@ var targetInfoTmpl = template.Must(template.New("targetInfo").Parse(targetInfoSc
type TargetInfoRequest struct {
FlakePath string
HostName string
Config config.Config
}

// TargetInfo contains host information queried from nix. It is cached.
Expand Down Expand Up @@ -96,6 +100,7 @@ func runScript(tmpl *template.Template, data any) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("nix template read: %w", err)
}
slog.Debug("Running nix script", "script", script)

// Pass script to nix cmd.
cmd := exec.Command("nix", "eval", "--file", "-", "--json")
Expand Down
1 change: 1 addition & 0 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ func (m *Model) hostTargetInfoCmd(host *hostModel) tea.Cmd {
targetInfo, nerr := nix.GetTargetInfo(nix.TargetInfoRequest{
FlakePath: m.flakePath,
HostName: host.name,
Config: m.config,
})
if nerr != nil {
slog.Error("Failed to fetch target info from nix",
Expand Down
32 changes: 16 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ func main() {
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.
configRoot := os.Getenv("XDG_CONFIG_HOME")
if configRoot == "" {
Expand All @@ -51,22 +67,6 @@ func main() {
os.Exit(1)
}

// 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 host list from flake.
flakePath := flag.Arg(0)
if flakePath == "" {
Expand Down