diff --git a/internal/nats/client.go b/internal/nats/client.go index 3d53f06..c616476 100644 --- a/internal/nats/client.go +++ b/internal/nats/client.go @@ -9,7 +9,6 @@ import ( "code.jhot.me/jhot/hats/pkg/config" n "code.jhot.me/jhot/hats/pkg/nats" - "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" ) @@ -31,11 +30,7 @@ func JetstreamConnect(parentContext context.Context, parentLogger *slog.Logger, cfg = parentConfig var err error - client = n.DefaultNatsConnection().WithHostName(cfg.NatsHost).WithPort(cfg.NatsPort).WithConnectionOption(nats.Name(cfg.NatsClientName)) - - if cfg.NatsToken != "" { - client.WithConnectionOption(nats.Token(cfg.NatsToken)) - } + client = n.New(n.WithHostName(cfg.NatsHost), n.WithPort(cfg.NatsPort), n.WithClientName(cfg.NatsClientName), n.WithToken(cfg.NatsToken)) logger.Debug("Connecting to nats") err = client.Connect() diff --git a/pkg/config/config.go b/pkg/config/config.go index 8d3927d..595e027 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -67,14 +67,9 @@ func New() (*HatsConfig, error) { return cfg, fmt.Errorf("error logging in to Infisical: %w", err) } - cfg.infisicalRetrievalOpts = &infisical.RetrieveSecretOptions{ - WorkspaceID: cfg.InfisicalProjectID, - Environment: cfg.InfisicalEnvironment, - SecretPath: "/", - IncludeImports: false, - } - - secrets, err := cfg.infisicalClient.ListSecrets(cfg.infisicalRetrievalOpts) + secrets, err := cfg.infisicalClient.ListSecrets( + infisical.WithWorkspaceID(cfg.InfisicalProjectID), + infisical.WithEnvironment(cfg.InfisicalEnvironment)) if err != nil { return cfg, fmt.Errorf("error getting Infisical secrets: %w", err) } diff --git a/pkg/homeassistant/structs.go b/pkg/homeassistant/structs.go index a371798..9bd72ae 100644 --- a/pkg/homeassistant/structs.go +++ b/pkg/homeassistant/structs.go @@ -48,39 +48,43 @@ var Domains = struct { // Home Assistant services var Services = struct { - TurnOn string - TurnOff string - Toggle string - Reload string - Lock string - Unlock string - OpenCover string - CloseCover string - SelectOption string - SetHvacMode string - SetFanMode string - SetTemperature string - SetValue string - Start string - Change string - Cancel string + TurnOn string + TurnOff string + Toggle string + Reload string + Lock string + Unlock string + OpenCover string + CloseCover string + StopCover string + SelectOption string + SetHvacMode string + SetFanMode string + SetTemperature string + SetValue string + Start string + Change string + Cancel string + SetCoverPosition string }{ - TurnOn: "turn_on", - TurnOff: "turn_off", - Toggle: "toggle", - Reload: "reload", - Lock: "lock", - Unlock: "unlock", - OpenCover: "open_cover", - CloseCover: "close_cover", - SelectOption: "select_option", - SetHvacMode: "set_hvac_mode", - SetFanMode: "set_fan_mode", - SetTemperature: "set_temperature", - SetValue: "set_value", - Start: "start", - Change: "change", - Cancel: "cancel", + TurnOn: "turn_on", + TurnOff: "turn_off", + Toggle: "toggle", + Reload: "reload", + Lock: "lock", + Unlock: "unlock", + OpenCover: "open_cover", + CloseCover: "close_cover", + StopCover: "stop_cover", + SelectOption: "select_option", + SetHvacMode: "set_hvac_mode", + SetFanMode: "set_fan_mode", + SetTemperature: "set_temperature", + SetValue: "set_value", + Start: "start", + Change: "change", + Cancel: "cancel", + SetCoverPosition: "set_cover_position", } // Extra props that can be sent when calling a Home Assistant service @@ -96,6 +100,7 @@ var ExtraProps = struct { TargetTempLow string Duration string Value string + Position string }{ Transition: "transition", Brightness: "brightness", @@ -108,6 +113,7 @@ var ExtraProps = struct { TargetTempLow: "target_temp_low", Duration: "duration", Value: "value", + Position: "position", } type ResultContext struct { diff --git a/pkg/infisical/api.go b/pkg/infisical/api.go index a81d9f0..c9ff9e2 100644 --- a/pkg/infisical/api.go +++ b/pkg/infisical/api.go @@ -70,7 +70,12 @@ func (c *InfisicalClient) CheckToken() error { return nil } -func (c *InfisicalClient) ListSecrets(opts *RetrieveSecretOptions) ([]Secret, error) { +func (c *InfisicalClient) ListSecrets(optFuncs ...RetrieveSecretOptionFunc) ([]Secret, error) { + opts := DefaultRetrieveSecretOptions() + for _, f := range optFuncs { + f(opts) + } + err := c.CheckToken() if err != nil { return []Secret{}, err diff --git a/pkg/infisical/structs.go b/pkg/infisical/structs.go index 2a105e7..0fa051d 100644 --- a/pkg/infisical/structs.go +++ b/pkg/infisical/structs.go @@ -23,24 +23,28 @@ func DefaultRetrieveSecretOptions() *RetrieveSecretOptions { } } -func (o *RetrieveSecretOptions) WithWorkspaceID(workspaceId string) *RetrieveSecretOptions { - o.WorkspaceID = workspaceId - return o +type RetrieveSecretOptionFunc func(*RetrieveSecretOptions) + +func WithWorkspaceID(workspaceId string) RetrieveSecretOptionFunc { + return func(rso *RetrieveSecretOptions) { + rso.WorkspaceID = workspaceId + } } -func (o *RetrieveSecretOptions) WithEnvironment(environment string) *RetrieveSecretOptions { - o.Environment = environment - return o +func WithEnvironment(environment string) RetrieveSecretOptionFunc { + return func(rso *RetrieveSecretOptions) { + rso.Environment = environment + } } -func (o *RetrieveSecretOptions) WithSecretPath(secretPath string) *RetrieveSecretOptions { - o.SecretPath = secretPath - return o +func WithSecretPath(secretPath string) RetrieveSecretOptionFunc { + return func(rso *RetrieveSecretOptions) { + rso.SecretPath = secretPath + } } -func (o *RetrieveSecretOptions) WithImports(includeImports bool) *RetrieveSecretOptions { - o.IncludeImports = includeImports - return o +func WithImports(rso *RetrieveSecretOptions) { + rso.IncludeImports = true } type Secret struct { diff --git a/pkg/nats/client.go b/pkg/nats/client.go index c9c6faa..2d94611 100644 --- a/pkg/nats/client.go +++ b/pkg/nats/client.go @@ -30,28 +30,55 @@ func DefaultNatsConnection() *NatsConnection { } } -func (n *NatsConnection) WithHostName(hostname string) *NatsConnection { - n.HostName = hostname - return n +type NatsConnectionOption func(*NatsConnection) + +func WithHostName(hostname string) NatsConnectionOption { + return func(nc *NatsConnection) { + nc.HostName = hostname + } } -func (n *NatsConnection) WithPort(port string) *NatsConnection { - n.Port = port - return n +func WithPort(port string) NatsConnectionOption { + return func(nc *NatsConnection) { + nc.Port = port + } } -func (n *NatsConnection) WithJetstream(jetstream bool) *NatsConnection { - n.UseJetstream = jetstream - return n +func WithoutJetstream(nc *NatsConnection) { + nc.UseJetstream = false } -func (n *NatsConnection) WithConnectionOption(opt nats.Option) *NatsConnection { - n.connOpts = append(n.connOpts, opt) - return n +func WithClientName(name string) NatsConnectionOption { + return func(nc *NatsConnection) { + nc.connOpts = append(nc.connOpts, nats.Name(name)) + } } -func (n *NatsConnection) WithJetstreamOption(opt jetstream.JetStreamOpt) *NatsConnection { - n.jetOpts = append(n.jetOpts, opt) +func WithToken(token string) NatsConnectionOption { + return func(nc *NatsConnection) { + if token != "" { + nc.connOpts = append(nc.connOpts, nats.Token(token)) + } + } +} + +func WithConnectionOption(opt nats.Option) NatsConnectionOption { + return func(nc *NatsConnection) { + nc.connOpts = append(nc.connOpts, opt) + } +} + +func WithJetstreamnOption(opt jetstream.JetStreamOpt) NatsConnectionOption { + return func(nc *NatsConnection) { + nc.jetOpts = append(nc.jetOpts, opt) + } +} + +func New(optFuncs ...NatsConnectionOption) *NatsConnection { + n := DefaultNatsConnection() + for _, optFn := range optFuncs { + optFn(n) + } return n }