1
0
Fork 0

Option funcs

main v0.21.0
Jordan Hotmann 2024-01-04 09:38:38 -07:00
parent b8a82b0050
commit f281e82499
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
6 changed files with 105 additions and 73 deletions

View File

@ -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()

View File

@ -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)
}

View File

@ -56,6 +56,7 @@ var Services = struct {
Unlock string
OpenCover string
CloseCover string
StopCover string
SelectOption string
SetHvacMode string
SetFanMode string
@ -64,6 +65,7 @@ var Services = struct {
Start string
Change string
Cancel string
SetCoverPosition string
}{
TurnOn: "turn_on",
TurnOff: "turn_off",
@ -73,6 +75,7 @@ var Services = struct {
Unlock: "unlock",
OpenCover: "open_cover",
CloseCover: "close_cover",
StopCover: "stop_cover",
SelectOption: "select_option",
SetHvacMode: "set_hvac_mode",
SetFanMode: "set_fan_mode",
@ -81,6 +84,7 @@ var Services = struct {
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 {

View File

@ -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

View File

@ -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 {

View File

@ -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
}