Skip to content

Commit

Permalink
[fhomed] fix crash when installing with Homebrew
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Dec 4, 2024
1 parent 99a815b commit ddea99c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
10 changes: 5 additions & 5 deletions cmd/fhomed/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/bartekpacia/fhome/highlevel"
)

func daemon(ctx context.Context, name, pin string) error {
func daemon(ctx context.Context, config *highlevel.Config, name, pin string) error {
client, err := highlevel.Connect(ctx, config, nil)
if err != nil {
return fmt.Errorf("failed to create api client: %v", err)
Expand All @@ -38,14 +38,14 @@ func daemon(ctx context.Context, name, pin string) error {
slog.String("source", systemConfig.Source),
)

config, err := api.MergeConfigs(userConfig, systemConfig)
apiConfig, err := api.MergeConfigs(userConfig, systemConfig)
if err != nil {
slog.Error("failed to merge configs", slog.Any("error", err))
return err
}

go serviceListener(ctx, client)
go websiteListener(ctx, config)
go websiteListener(ctx, config, apiConfig)

// Here we listen to HomeKit events and convert them to API calls to F&Home
// to keep the state in sync.
Expand Down Expand Up @@ -122,7 +122,7 @@ func daemon(ctx context.Context, name, pin string) error {
},
}

home, err := homekitClient.SetUp(config)
home, err := homekitClient.SetUp(apiConfig)
if err != nil {
slog.Error("failed to set up homekit", slog.Any("error", err))
return err
Expand Down Expand Up @@ -150,7 +150,7 @@ func daemon(ctx context.Context, name, pin string) error {
}

cellValue := resp.Response.CellValues[0]
printCellData(&cellValue, config)
printCellData(&cellValue, apiConfig)

// handle lightbulb
{
Expand Down
3 changes: 2 additions & 1 deletion cmd/fhomed/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"

"github.com/bartekpacia/fhome/api"
"github.com/bartekpacia/fhome/highlevel"
)

//go:embed assets/*
Expand Down Expand Up @@ -45,7 +46,7 @@ func serviceListener(ctx context.Context, client *api.Client) {
}

// A simple webserver to display some state about my smart devices.
func websiteListener(ctx context.Context, homeConfig *api.Config) {
func websiteListener(ctx context.Context, config *highlevel.Config, homeConfig *api.Config) {
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
slog.Info("got request", slog.String("method", r.Method), slog.String("path", r.URL.Path))

Expand Down
10 changes: 6 additions & 4 deletions cmd/fhomed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/urfave/cli/v3"
)

var config *highlevel.Config
// var config *highlevel.Config

// This is set by GoReleaser, see https://goreleaser.com/cookbooks/using-main.version
var version = "dev"
Expand Down Expand Up @@ -120,7 +120,9 @@ func main() {
name := cmd.String("name")
pin := cmd.String("pin")

return daemon(ctx, name, pin)
config := loadConfig()

return daemon(ctx, config, name, pin)
},
CommandNotFound: func(ctx context.Context, cmd *cli.Command, command string) {
log.Printf("invalid command '%s'. See 'fhomed --help'\n", command)
Expand All @@ -135,7 +137,7 @@ func main() {
}
}

func loadConfig() {
func loadConfig() *highlevel.Config {
k := koanf.New(".")

p := "/etc/fhomed/config.toml"
Expand All @@ -153,7 +155,7 @@ func loadConfig() {
slog.Debug("loaded config file", slog.String("path", p))
}

config = &highlevel.Config{
return &highlevel.Config{
Email: k.MustString("FHOME_EMAIL"),
Password: k.MustString("FHOME_CLOUD_PASSWORD"),
ResourcePassword: k.MustString("FHOME_RESOURCE_PASSWORD"),
Expand Down

0 comments on commit ddea99c

Please sign in to comment.