util

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const Days = time.Hour * 24

Days is the duration of a day.

Variables

This section is empty.

Functions

func AppendFile

func AppendFile(filePath string, str string) error

AppendFile writes the passed string content at the end of the textfile.

func EpochWeek

func EpochWeek(dt time.Time) int

EpochWeek returns the number of weeks passed since January 1, 1970 UTC. Note this does not mean it's aligned for weekdays, i.e. the Monday after Sunday does not necessary have a higher EpochWeek number.

func EqualsIgnoreTrailingNewlines added in v1.3.0

func EqualsIgnoreTrailingNewlines(s1 string, s2 string) bool

EqualsIgnoreTrailingNewlines checks if s1 equals s2, but without taking trailing newlines into account.

func Exists added in v1.3.0

func Exists(path string) bool

Exists returns true if the file exists.

func FetchJSONAsModel

func FetchJSONAsModel(client *http.Client, url string, user string, password string, target interface{}) error

FetchJSONAsModel sends a GET request to the url and maps the JSON response to the target model.

func GetLowerBound

func GetLowerBound(t1 *time.Time, t2 *time.Time) time.Time

GetLowerBound returns the earlier of two times. A nil time is considered infinitely late so won't be used if the other time is not nil.

func GetUpperBound

func GetUpperBound(t1 *time.Time, t2 *time.Time) time.Time

GetUpperBound returns the later of two times. A nil time is considered infinitely early so won't be used if the other time is not nil.

func Keys

func Keys(m map[string]bool) []string

Keys returns the map's keys as a list.

func ParseISODate

func ParseISODate(s string) (time.Time, error)

ParseISODate parses a string of format 2006-01-02.

func ReadFile

func ReadFile(filePath string) (string, error)

ReadFile reads a textfile's entire content into a string and returns it.

func RemoveExtension

func RemoveExtension(s string) string

RemoveExtension returns the filename without extension E.g. todo.txt -> todo

func SaveConfig

func SaveConfig(cfg *Config, cfgFile string) error

SaveConfig saves the config object to a YAML file.

func SaveConfigString

func SaveConfigString(cfg *Config) (string, error)

SaveConfigString saves the config object to a YAML string.

func SetTime

func SetTime(dt time.Time, tm time.Time) time.Time

SetTime changes the given datetime to the time of the day.

func SetToEndOfDay

func SetToEndOfDay(dt time.Time) time.Time

SetToEndOfDay changes the given datetime to the end of the day (23:59).

func SetToEndOfWeek

func SetToEndOfWeek(dt time.Time) time.Time

SetToEndOfWeek changes the given datetime to the end of the week (Sunday).

func SetToStartOfDay

func SetToStartOfDay(dt time.Time) time.Time

SetToStartOfDay changes the given datetime to the start of the day (0:00).

func SetToStartOfWeek

func SetToStartOfWeek(dt time.Time) time.Time

SetToStartOfWeek changes the given datetime to the start of the week (Monday).

func SetWeekday

func SetWeekday(dt time.Time, wd time.Weekday) time.Time

SetWeekday changes the given datetime to occur on the given week day.

func WriteFile

func WriteFile(filePath string, str string) error

WriteFile writes the passed string content to a textfile.

Types

type Config

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

Config holds configuration values and has convenience methods to access these.

func LoadConfig

func LoadConfig(cfgFile string) (*Config, error)

LoadConfig loads a Config object from a YAML file.

func LoadConfigString

func LoadConfigString(str string) (*Config, error)

LoadConfigString loads a Config object from a YAML string.

func (Config) GetBool

func (cfg Config) GetBool(key string, defaultVal bool) bool

GetBool returns the bool value for key, or defaultVal if the key is not found.

func (Config) GetByPath

func (cfg Config) GetByPath(path ConfigPath, defaultVal interface{}) interface{}

GetByPath returns the value for the config path, or defaultVal if the key is not found.

func (Config) GetInt

func (cfg Config) GetInt(key string, defaultVal int) int

GetInt returns the int value for key, or defaultVal if the key is not found.

func (Config) GetIntOrFail

func (cfg Config) GetIntOrFail(key string) int

GetIntOrFail returns the int value for key, or exits the program with error code 1 if it doesn't exist.

func (Config) GetString

func (cfg Config) GetString(key string, defaultVal string) string

GetString returns the string value for key, or defaultVal if the key is not found.

func (Config) GetStringList

func (cfg Config) GetStringList(key string, defaultVal []string) []string

GetStringList returns the value for key, or defaultVal if the key is not found.

func (Config) GetStringOrFail

func (cfg Config) GetStringOrFail(key string) string

GetStringOrFail returns the string value for key, or exits the program with error code 1 if it doesn't exist.

func (Config) GetSubConfig

func (cfg Config) GetSubConfig(key string) *Config

GetSubConfig returns the part of the config under key as a new Config object. If the key is not found an empty config is returned.

func (Config) HasKey

func (cfg Config) HasKey(key string) bool

HasKey returns true if the config contains the key.

func (Config) SetByPath

func (cfg Config) SetByPath(path ConfigPath, val interface{}) bool

SetByPath sets the value for the config path, if the path points to a correct sub-config. If the sub-config is not found, nothing is written - it does not create intermediate sub-configs. If the sub-config is found but doesn't have the final part of the path as a key yet, that key is created.

func (Config) SetString

func (cfg Config) SetString(key string, val string)

SetString sets the config key to string val.

type ConfigPath

type ConfigPath struct {
	Parts []string
}

ConfigPath defines a path to a specific config value, where each part is a key in the next sub-config.

func (*ConfigPath) FromString

func (p *ConfigPath) FromString(str string)

FromString sets the config path to the path denoted by the dot-separated string.

func (*ConfigPath) ToString

func (p *ConfigPath) ToString() string

ToString converts the config path to a dot-separated string

type FileConfig added in v1.3.0

type FileConfig struct {
	TodoDir   string
	TodoFile  string
	TrashFile string
}

FileConfig stores the where the most important files for sibylgo are located.

func NewFileConfigFromConfig added in v1.3.0

func NewFileConfigFromConfig(cfg *Config) *FileConfig

NewFileConfigFromConfig reads the important file paths from the configuration and returns them as a FileConfig object.

func NewFileConfigFromTodoFile added in v1.3.0

func NewFileConfigFromTodoFile(todoFile string) *FileConfig

NewFileConfigFromTodoFile derives all the important file paths from the todoFile. It assumes that all other files are in the same directory as the todoFile.

Jump to

Keyboard shortcuts

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