this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: parse hosts config

Khue Doan 73188ee8 529247e3

+53 -15
+23 -15
cmd/nixie/main.go
··· 3 3 import ( 4 4 "flag" 5 5 6 + "code.khuedoan.com/nixie/internal/hosts" 7 + 6 8 "github.com/charmbracelet/log" 7 9 ) 8 10 9 - type Config struct { 11 + type Flags struct { 10 12 Address string 11 13 Debug bool 12 14 Flake string ··· 15 17 SSHKey string 16 18 } 17 19 18 - func parseFlags() Config { 19 - var config Config 20 + func parseFlags() Flags { 21 + var flags Flags 20 22 21 - flag.BoolVar(&config.Debug, "debug", false, "Enable debug logging") 22 - flag.StringVar(&config.Address, "address", "0.0.0.0", "Address to listen on") 23 - flag.StringVar(&config.Flake, "flake", "", "NixOS configuration flake (for example, .)") 24 - flag.StringVar(&config.HostsFile, "hosts", "", "Path to hosts.json file (for example, ./hosts.json)") 25 - flag.StringVar(&config.Installer, "installer", "", "NixOS installer flake output (for example, .#installer)") 26 - flag.StringVar(&config.SSHKey, "ssh-key", "", "Path to the SSH private key (for example, ~/.ssh/id_ed25519)") 23 + flag.BoolVar(&flags.Debug, "debug", false, "Enable debug logging") 24 + flag.StringVar(&flags.Address, "address", "0.0.0.0", "Address to listen on") 25 + flag.StringVar(&flags.Flake, "flake", "", "NixOS configuration flake (for example, .)") 26 + flag.StringVar(&flags.HostsFile, "hosts", "", "Path to hosts.json file (for example, ./hosts.json)") 27 + flag.StringVar(&flags.Installer, "installer", "", "NixOS installer flake output (for example, .#installer)") 28 + flag.StringVar(&flags.SSHKey, "ssh-key", "", "Path to the SSH private key (for example, ~/.ssh/id_ed25519)") 27 29 28 30 flag.Parse() 29 31 30 - if config.HostsFile == "" || config.Flake == "" || config.Installer == "" { 31 - log.Fatal("Usage: nixie --hosts <hosts.json> --flake <flake> --installer <installer-output>") 32 + if flags.HostsFile == "" || flags.Flake == "" || flags.Installer == "" { 33 + log.Fatal("usage: nixie --hosts <hosts.json> --flake <flake> --installer <installer-output>") 32 34 } 33 35 34 - return config 36 + return flags 35 37 } 36 38 37 39 func setupLogging(debug bool) { ··· 41 43 } 42 44 43 45 func main() { 44 - config := parseFlags() 45 - setupLogging(config.Debug) 46 - log.Debug("Parsed config", "config", config) 46 + flags := parseFlags() 47 + setupLogging(flags.Debug) 48 + log.Debug("parsed command line flags", "flags", flags) 49 + 50 + hostsConfig, err := hosts.LoadHostsConfig(flags.HostsFile) 51 + if err != nil { 52 + log.Fatal("failed to load hosts config", "error", err) 53 + } 54 + log.Debug("parsed hosts config", "hosts", hostsConfig) 47 55 }
+3
examples/hosts.json
··· 1 1 { 2 2 "machine1": { 3 3 "mac_address": "bc:24:11:d0:28:34" 4 + }, 5 + "machine2": { 6 + "mac_address": "bc:24:11:0d:2f:20" 4 7 } 5 8 }
+27
internal/hosts/config.go
··· 1 + package hosts 2 + 3 + import ( 4 + "encoding/json" 5 + "fmt" 6 + "os" 7 + ) 8 + 9 + type Host struct { 10 + MACAddress string `json:"mac_address"` 11 + } 12 + 13 + type HostsConfig map[string]Host 14 + 15 + func LoadHostsConfig(filename string) (HostsConfig, error) { 16 + data, err := os.ReadFile(filename) 17 + if err != nil { 18 + return nil, fmt.Errorf("failed to read hosts file: %w", err) 19 + } 20 + 21 + var hostsConfig HostsConfig 22 + if err := json.Unmarshal(data, &hostsConfig); err != nil { 23 + return nil, fmt.Errorf("failed to parse hosts file: %w", err) 24 + } 25 + 26 + return hostsConfig, nil 27 + }