-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
169 lines (140 loc) · 3.18 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"github.com/pelletier/go-toml"
)
type Config struct {
f *os.File
Site struct {
Title string
Description string
Link string
Remote string
Theme string
Blogroll []string
}
Author struct {
Name string
Email string
}
}
var (
configFile = "jrnl.toml"
configStub = `
[site]
title = ""
description = ""
link = ""
remote = ""
theme = ""
blogroll = []
[author]
name = ""
email = ""
`
ConfigCmd = &Command{
Usage: "config <key> <value>",
Short: "set a configuration value within jrnl.toml",
Long: `Config will set the given configuration key with the given value in the
jrnl.toml file. For all properties in that file, this will simply overwrite
what's already there, except for the site.blogroll property which will append
the given value to the pre-existing blogroll. If an empty string is given to
site.blogroll then this will clear down the list.`,
Run: configCmd,
}
)
func CreateConfig(dir string) (*Config, error) {
f, err := os.OpenFile(filepath.Join(dir, configFile), os.O_CREATE|os.O_RDWR, os.FileMode(0644))
if err != nil {
return nil, err
}
c := &Config{}
if err := toml.NewEncoder(f).Encode(c); err != nil {
return nil, err
}
c.f = f
return c, nil
}
func OpenConfig() (*Config, error) {
f, err := os.OpenFile(configFile, os.O_RDWR, os.FileMode(0644))
if err != nil {
return nil, err
}
cfg := &Config{
f: f,
}
err = toml.NewDecoder(f).Decode(cfg)
return cfg, err
}
func (c *Config) Set(key, val string) error {
switch key {
case "site.title":
c.Site.Title = val
case "site.description":
c.Site.Description = val
case "site.link":
c.Site.Link = val
case "site.remote":
c.Site.Remote = val
case "site.theme":
c.Site.Theme = val
case "site.blogroll":
if val == "" {
c.Site.Blogroll = []string{}
break
}
c.Site.Blogroll = append(c.Site.Blogroll, val)
case "author.name":
c.Author.Name = val
case "author.email":
c.Author.Email = val
default:
return errors.New("unknown configuration key")
}
return nil
}
func (c *Config) Close() error { return c.f.Close() }
func (c *Config) Save() error {
// f, err := os.OpenFile(configFile, os.O_TRUNC|os.O_CREATE|os.O_RDWR, os.FileMode(0644))
//
// if err != nil {
// return err
// }
//
// defer f.Close()
if err := c.f.Truncate(0); err != nil {
return err
}
if _, err := c.f.Seek(0, io.SeekStart); err != nil {
return err
}
return toml.NewEncoder(c.f).Encode(c)
}
func configCmd(cmd *Command, args []string) {
if err := initialized(""); err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
if len(args) < 3 {
fmt.Fprintf(os.Stderr, "%s %s: usage: %s\n", cmd.Argv0, args[0], cmd.Usage)
os.Exit(1)
}
cfg, err := OpenConfig()
if err != nil {
println("open config error")
fmt.Fprintf(os.Stderr, "%s %s: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
if err := cfg.Set(args[1], args[2]); err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
if err := cfg.Save(); err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
}