/
configs.go
93 lines (75 loc) 路 1.68 KB
/
configs.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
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package model
import (
"fmt"
"os"
"github.com/clivern/poodle/core/util"
"github.com/BurntSushi/toml"
)
// Configs type
type Configs struct {
General General `toml:"General"`
Gist Gist `toml:"Gist"`
Services Services `toml:"Services"`
}
// General type
type General struct {
Editor string `toml:"editor"`
Column int `toml:"column"`
Selectcmd string `toml:"selectcmd"`
Backend string `toml:"backend"`
Sortby string `toml:"sortby"`
}
// Gist type
type Gist struct {
AccessToken string `toml:"access_token"`
GistID string `toml:"gist_id"`
Username string `toml:"username"`
Public bool `toml:"public"`
AutoSync bool `toml:"auto_sync"`
}
// Services type
type Services struct {
Directory string `toml:"directory"`
}
// NewConfigs creates an instance of Configs
func NewConfigs() *Configs {
path := fmt.Sprintf(
"%s%s",
util.EnsureTrailingSlash(os.Getenv("HOME")),
"poodle/definitions",
)
return &Configs{
General: General{
Editor: "nano",
Column: 40,
Selectcmd: "fzf --ansi",
Backend: "gist",
},
Services: Services{
Directory: path,
},
}
}
// Decode decodes from file to struct
func (g *Configs) Decode(path string) error {
if _, err := toml.DecodeFile(path, &g); err != nil {
return err
}
return nil
}
// Encode encodes struct and store on file
func (g *Configs) Encode(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
err = toml.NewEncoder(f).Encode(g)
if err != nil {
return err
}
return nil
}