Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new fields to UserConfig #24

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/writeas/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion cmd/writeas/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion cmd/writeas/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions cmd/writeas/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,12 +40,26 @@ 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
}
if coll := c.String("b"); coll != "" {
return coll
}
uc, _ := loadConfig()

if uc != nil {
return uc.Posts.Collection
}
return ""
}
21 changes: 20 additions & 1 deletion cmd/writeas/userconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +62,7 @@ func loadConfig() (*UserConfig, error) {
return uc, nil
}


func saveConfig(uc *UserConfig) error {
cfg := ini.Empty()
err := ini.ReflectFrom(cfg, uc)
Expand Down