-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathconfig.go
70 lines (64 loc) · 1.91 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
// Copyright (c) 2020-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"log"
"os"
"github.com/olekukonko/tablewriter"
"go.etcd.io/bbolt"
)
func (n *Nami) SetConfig(k, v string) error {
err := n.DB.Update(func(tx *bbolt.Tx) error {
t, err := tx.CreateBucketIfNotExists([]byte("config"))
if err != nil {
return err
}
if err := t.Put([]byte(k), []byte(v)); err != nil {
return err
}
return nil
})
return err
}
func (n *Nami) GetConfig(k string) (string, error) {
s := ""
err := n.DB.Update(func(tx *bbolt.Tx) error {
t, err := tx.CreateBucketIfNotExists([]byte("config"))
if err != nil {
return err
}
b := t.Get([]byte(k))
if b == nil {
return nil
}
s = string(b)
return nil
})
return s, err
}
func (n *Nami) PrintConfigs() {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Key", "Value", "Description"})
table.Append([]string{"nami.db", "~/.nami/db", "Nami db file, can't be modified"})
table.Append([]string{"nami.bin", "~/.nami/bin", "Nami bin dir, can't be modified"})
table.Append([]string{"nami.dir", "~/.nami/dir", "Nami dir dir, can't be modified"})
s, err := n.GetConfig("github.token")
if err != nil {
log.Println(err)
return
}
table.Append([]string{"github.token", s, "Github token for releasing your own project"})
table.Render()
}