cli

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagLimit    = "limit"
	FlagFromDate = "from"
	FlagToDate   = "to"
	FlagJson     = "json"

	FromDateTime = "fromDateTime"
	ToDateTime   = "toDateTime"
)
View Source
const (
	PersistentFlagConfig = "config"
	PersistentFlagSender = "sender"
)
View Source
const (
	FlagServerInterval     = "interval"
	SmartlockCheckInterval = time.Hour * 2
)
View Source
const (
	AppName = "nuki-logger"
)

Variables

View Source
var (
	BuildDate          string
	ApplicationVersion string
)
View Source
var (
	QueryCmd = &cobra.Command{
		Use:   "query",
		Short: "Query logs from the Nuki API",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			lim := viper.GetInt(FlagLimit)
			if lim <= 0 || lim > 50 {
				return fmt.Errorf("limit is out of bound. Should be between 1 and 50")
			}

			if viper.GetString(FlagFromDate) != "" {
				fromDate, err := time.Parse(time.RFC3339, viper.GetString(FlagFromDate))
				if err != nil {
					return err
				}
				viper.Set(FromDateTime, fromDate)
			}
			if viper.GetString(FlagToDate) != "" {
				toDate, err := time.Parse(time.RFC3339, viper.GetString(FlagToDate))
				if err != nil {
					return err
				}
				viper.Set(ToDateTime, toDate)
			}

			return nil
		},
		RunE: QueryRun,
	}
)
View Source
var (
	RootCmd = &cobra.Command{
		Use:           "nuki-logger",
		Short:         "Query Nuki api for most recent logs",
		SilenceUsage:  true,
		SilenceErrors: true,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
			if strings.Contains(cmd.CommandPath(), "version") {
				return nil
			}

			var requiredFlagsMissing []string
			for _, v := range requiredStringFlags {
				if viper.GetString(v) == "" {
					requiredFlagsMissing = append(requiredFlagsMissing, v)
				}
			}
			for _, v := range requiredStringSliceFlags {
				if len(viper.GetStringSlice(v)) == 0 {
					requiredFlagsMissing = append(requiredFlagsMissing, v)
				}
			}

			if len(requiredFlagsMissing) > 0 {
				return fmt.Errorf("the following flag(s) are required: %s", strings.Join(requiredFlagsMissing, ", "))
			}

			if viper.GetString(PersistentFlagConfig) != "" {
				viper.SetConfigName(viper.GetString(PersistentFlagConfig))
			}

			if err := config.LoadConfig(viper.GetViper()); err != nil {
				return err
			}

			return initSenders()
		},
		RunE: run,
	}
)
View Source
var (
	ServerCmd = &cobra.Command{
		Use:           "server",
		Short:         "Run a server querying Nuki api for logs on a regular interval",
		SilenceUsage:  true,
		SilenceErrors: true,
		RunE:          RunServer,
	}
)
View Source
var (
	VersionCmd = &cobra.Command{
		Use:   "version",
		Short: "Print app version",
		Run: func(cmd *cobra.Command, args []string) {
			log.Info().
				Str("app_name", AppName).
				Str("version", ApplicationVersion).
				Str("build_date", BuildDate).
				Send()
		},
	}
)

Functions

func QueryRun

func QueryRun(_ *cobra.Command, _ []string) error

func RunServer

func RunServer(_ *cobra.Command, _ []string) error

func StringToTimeHourMinuteHookFunc added in v0.0.7

func StringToTimeHourMinuteHookFunc() mapstructure.DecodeHookFunc

Types

type Config

type Config struct {
	SmartlockID  int64          `mapstructure:"smartlock_id"`
	NukiAPIToken string         `mapstructure:"nuki_api_token"`
	AddressID    int64          `mapstructure:"address_id"`
	Senders      []SenderConfig `mapstructure:"senders"`
	TelegramBot  struct {
		Enabled           bool           `mapstructure:"enabled"`
		SenderName        string         `mapstructure:"sender_name"`
		DefaultCheckIn    TimeHourMinute `mapstructure:"default_check_in"`
		DefaultCheckOut   TimeHourMinute `mapstructure:"default_check_out"`
		RestrictToChatIDs []int64        `mapstructure:"restrict_private_chat_ids"`
	} `mapstructure:"telegram_bot"`
	LogsReader          nukiapi.LogsReader          `mapstructure:"-"`
	SmartlockReader     nukiapi.SmartlockReader     `mapstructure:"-"`
	SmartlockAuthReader nukiapi.SmartlockAuthReader `mapstructure:"-"`
	ReservationsReader  nukiapi.ReservationsReader  `mapstructure:"-"`
}

func (*Config) GetSender

func (c *Config) GetSender(name string) (messaging.Sender, error)

func (*Config) LoadConfig

func (c *Config) LoadConfig(vi *viper.Viper) error

type QueryConfig

type QueryConfig struct {
	Config
}

type SenderConfig

type SenderConfig struct {
	Name     string                    `mapstructure:"name"`
	Telegram *messaging.TelegramSender `mapstructure:"telegram"`
	Console  *messaging.ConsoleSender  `mapstructure:"console"`
}

func (*SenderConfig) GetSender

func (sc *SenderConfig) GetSender() (messaging.Sender, error)

type TimeHourMinute added in v0.0.7

type TimeHourMinute time.Time

TimeHourMinute is a time.Time representing only hours and minutes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL