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