From 5c80b8dbb9e6431927d42d87bb5627c32da47335 Mon Sep 17 00:00:00 2001 From: a-mt Date: Sat, 19 Jan 2019 12:56:51 +0100 Subject: [PATCH] Add new fields to UserConfig --- cmd/writeas/api.go | 3 +++ cmd/writeas/cli.go | 1 - cmd/writeas/fonts.go | 12 +++++++++++- cmd/writeas/options.go | 21 +++++++++++++++++++++ cmd/writeas/userconfig.go | 21 ++++++++++++++++++++- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/cmd/writeas/api.go b/cmd/writeas/api.go index 670dba2..083277c 100644 --- a/cmd/writeas/api.go +++ b/cmd/writeas/api.go @@ -83,6 +83,9 @@ func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) ( if lang := language(c, true); lang != "" { pp.Language = &lang } + if rtl := rtl(); rtl { + pp.IsRTL = &rtl + } p, err := cl.CreatePost(pp) if err != nil { return nil, fmt.Errorf("Unable to post: %v", err) diff --git a/cmd/writeas/cli.go b/cmd/writeas/cli.go index 6092688..289422a 100644 --- a/cmd/writeas/cli.go +++ b/cmd/writeas/cli.go @@ -57,7 +57,6 @@ var postFlags = []cli.Flag{ cli.StringFlag{ Name: "font", Usage: "Sets post font to given value", - Value: defaultFont, }, cli.StringFlag{ Name: "lang", diff --git a/cmd/writeas/fonts.go b/cmd/writeas/fonts.go index 28684e9..4e1a5bc 100644 --- a/cmd/writeas/fonts.go +++ b/cmd/writeas/fonts.go @@ -30,10 +30,20 @@ var postFontMap = map[string]postFont{ func getFont(code bool, font string) string { if code { - if font != "" && font != defaultFont { + if font != "" { fmt.Printf("A non-default font '%s' and --code flag given. 'code' type takes precedence.\n", font) } return "code" + + // Font defined in config file + } else if font == "" { + uc, _ := loadConfig() + + if uc != nil && uc.Posts.Font != "" { + font = uc.Posts.Font + } else { + return string(defaultFont) + } } // Validate font value diff --git a/cmd/writeas/options.go b/cmd/writeas/options.go index 30222cd..d115302 100644 --- a/cmd/writeas/options.go +++ b/cmd/writeas/options.go @@ -24,6 +24,13 @@ func language(c *cli.Context, auto bool) string { if !auto { return "" } + + // Lang defined in config file + uc, _ := loadConfig() + if uc != nil && uc.Posts.Lang != "" { + return uc.Posts.Lang + } + // Automatically detect language l, err := jibber_jabber.DetectLanguage() if err != nil { @@ -33,6 +40,15 @@ func language(c *cli.Context, auto bool) string { return l } +func rtl() bool { + uc, _ := loadConfig() + + if uc != nil { + return uc.Posts.IsRTL + } + return false +} + func collection(c *cli.Context) string { if coll := c.String("c"); coll != "" { return coll @@ -40,5 +56,10 @@ func collection(c *cli.Context) string { if coll := c.String("b"); coll != "" { return coll } + uc, _ := loadConfig() + + if uc != nil { + return uc.Posts.Collection + } return "" } diff --git a/cmd/writeas/userconfig.go b/cmd/writeas/userconfig.go index e904dff..d6f600b 100644 --- a/cmd/writeas/userconfig.go +++ b/cmd/writeas/userconfig.go @@ -20,16 +20,34 @@ type ( PostsConfig struct { Directory string `ini:"directory"` + Font string `ini:"font"` + Lang string `ini:"lang"` + IsRTL bool `ini:"rtl"` + Collection string `ini:"collection"` } UserConfig struct { API APIConfig `ini:"api"` Posts PostsConfig `ini:"posts"` } + + ConfigSingleton struct { + uc *UserConfig + err error + } ) +var _instance *ConfigSingleton = nil +// Only load config file once func loadConfig() (*UserConfig, error) { - // TODO: load config to var shared across app + if _instance == nil { + uc, err := reloadConfig() + _instance = &ConfigSingleton{uc, err} + } + return _instance.uc, _instance.err +} + +func reloadConfig() (*UserConfig, error) { cfg, err := ini.LooseLoad(filepath.Join(userDataDir(), userConfigFile)) if err != nil { return nil, err @@ -44,6 +62,7 @@ func loadConfig() (*UserConfig, error) { return uc, nil } + func saveConfig(uc *UserConfig) error { cfg := ini.Empty() err := ini.ReflectFrom(cfg, uc)