config

package
v0.0.0-...-58581c8 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultConfigPath

func DefaultConfigPath() string

func EnsureConfigFile

func EnsureConfigFile(path string) error

EnsureConfigFile checks that file on path exists and if not, writes default config

func SaveConfig

func SaveConfig(path string, uc *UserConfig) error

Types

type Anki

type Anki struct {
	// Addr is host:port of AnkiConnect address.
	// Protocol must not be specified, anyway https seems to be redudant.
	Addr string `yaml:"addr" koanf:"addr"`
	// APIKey is secret that can be enabled on AnkiConnect.
	// Can be any string. Empty string means that no secret will be used.
	APIKey string `yaml:"api-key" koanf:"api-key"`

	// Deck is the name of deck in Anki where new cards will be added.
	// Deck can be any non-empty string, that doesn't contain `"` (Anki removes it),
	// that doesn't start or end with spaces.
	// I have found that Anki can convert some names with `::`, for example name
	// `hello::::world` will have alias `hello::blank::world`, but this should not
	// break anything.
	Deck string `yaml:"deck" koanf:"deck"`
	// NoteType is the name of note type that will be used for creation new notes in Anki.
	// NoteType can be any non-empty string, that doesn't contain `"` (Anki removes it) and
	// doesn't start or end with spaces.
	NoteType string `yaml:"note-type" koanf:"note-type"`

	// FieldMapping specifies how note fields will be filled in new notes.
	//
	// Key is the name of field in Anki. Can be any non-empty string that doesn't contain
	// symbols `:`, `"`, `{` or `}` and doesn't start or end with spaces.
	//
	// Value should be valid go text/template. For more details see pkg/anki/template.go.
	FieldMapping map[string]string `yaml:"fields" koanf:"fields"`

	// Audio specifies how audio should be mapped to anki notes.
	Audio AnkiAudio `yaml:"audio" koanf:"audio"`
}

type AnkiAudio

type AnkiAudio struct {
	// Field is name of field where audio should be added. If empty, audio will not be added.
	Field string
	// PreferredType is substring of audio type that will be selected if found.
	PreferredType string
}

type Consumer

type Consumer interface {
	// Config extracts parts of config that will be used.
	// Also its recommended that you implement validation in this function.
	Config(*UserConfig) (Part, error)
}

type ConsumerFunc

type ConsumerFunc func(*UserConfig) (Part, error)

ConsumerFunc is helper implementation for Consumer interface

func (ConsumerFunc) Config

func (f ConsumerFunc) Config(uc *UserConfig) (Part, error)

type Dictionary

type Dictionary struct {
	Workers   int               `yaml:"workers" koanf:"workers"`
	UserAgent string            `yaml:"user-agent" koanf:"user-agent"`
	Headers   map[string]string `yaml:"headers" koanf:"headers"`
	Jisho     Jisho             `yaml:"jisho" koanf:"jisho"`
	Wadoku    Wadoku            `yaml:"wadoku" koanf:"wadoku"`
}

type Jisho

type Jisho struct {
	URL string `yaml:"url" koanf:"url"`
}

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func New

func New(configPath string) (*Manager, error)

func (*Manager) Current

func (m *Manager) Current() *UserConfig

func (*Manager) Register

func (m *Manager) Register(consumer Consumer) (Part, UpdateConfigFunc, error)

Register registers config consumer. It's not concurrent safe. If consumer implement Reloader interface then it will be registered as consumer that can reload it's self on config change. Also Reloader gets it's initial reload with acquired part.

func (*Manager) UpdateConfig

func (m *Manager) UpdateConfig(updateFn func(*UserConfig) error) error

UpdateConfig guarded by simple mutex. This function will rewrite config specified while construction Manager. Saving can be failed despite reloading all affected parts, this indicated by SaveFailedError.

type Part

type Part interface {
	Equal(any) bool
}

type Reloader

type Reloader interface {
	Consumer
	Reload(Part) error
}

type SaveFailedError

type SaveFailedError struct {
	Reason error
}

func (*SaveFailedError) Error

func (e *SaveFailedError) Error() string

func (*SaveFailedError) Unwrap

func (e *SaveFailedError) Unwrap() error

type UpdateConfigFunc

type UpdateConfigFunc func(updateFn func(*UserConfig) error) error

type UserConfig

type UserConfig struct {
	Addr       string     `yaml:"addr" koanf:"addr"`
	Anki       Anki       `yaml:"anki" koanf:"anki"`
	Dictionary Dictionary `yaml:"dictionary" koanf:"dictionary"`
}

func DefaultUserConfig

func DefaultUserConfig() *UserConfig

func LoadConfig

func LoadConfig(path string) (*UserConfig, error)

func (*UserConfig) Clone

func (uc *UserConfig) Clone() *UserConfig

type Wadoku

type Wadoku struct {
	URL string `yaml:"url" koanf:"url"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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