-
Notifications
You must be signed in to change notification settings - Fork 115
/
config.go
161 lines (129 loc) · 4.03 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
// Copyright 2011-2018 Paul Ruane.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 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 <http://www.gnu.org/licenses/>.
package cli
import (
"fmt"
"github.com/oniony/TMSU/storage"
"strings"
)
var ConfigCommand = Command{
Name: "config",
Synopsis: "Views or amends database settings",
Usages: []string{"tmsu config",
"tmsu config NAME[=VALUE]..."},
Description: `Lists or views the database settings for the current database.
Without arguments the complete set of settings are shown, otherwise lists the settings for the specified setting NAMEs.
If a VALUE is specified then the setting is updated.`,
Options: Options{},
Exec: configExec,
}
// unexported
func configExec(options Options, args []string, databasePath string) (error, warnings) {
store, err := openDatabase(databasePath)
if err != nil {
return err, nil
}
defer store.Close()
tx, err := store.Begin()
if err != nil {
return err, nil
}
defer tx.Commit()
if len(args) == 0 {
if err := listAllSettings(store, tx); err != nil {
return fmt.Errorf("could not list settings"), nil
}
}
if len(args) == 1 && strings.Index(args[0], "=") == -1 {
printSettingValue(store, tx, args[0])
return nil, nil
}
for _, arg := range args {
parts := strings.Split(arg, "=")
switch len(parts) {
case 1:
name := parts[0]
if err := printSetting(store, tx, name); err != nil {
return fmt.Errorf("could not show value for setting '%v': %v", name, err), nil
}
case 2:
name := parts[0]
value := parts[1]
if err := amendSetting(store, tx, name, value); err != nil {
return fmt.Errorf("could not amend setting '%v' to '%v': %v", name, value, err), nil
}
default:
return fmt.Errorf("invalid argument, '%v'", arg), nil
}
}
return nil, nil
}
func listAllSettings(store *storage.Storage, tx *storage.Tx) error {
settings, err := store.Settings(tx)
if err != nil {
return fmt.Errorf("could not retrieve settings: %v", err)
}
for _, setting := range settings {
printSettingAndValue(setting.Name, setting.Value)
}
return nil
}
func printSetting(store *storage.Storage, tx *storage.Tx, name string) error {
if name == "" {
return fmt.Errorf("setting name must be specified")
}
setting, err := store.Setting(tx, name)
if err != nil {
return fmt.Errorf("could not retrieve setting '%v'", err)
}
if setting == nil {
return fmt.Errorf("no such setting '%v'", name)
}
printSettingAndValue(setting.Name, setting.Value)
return nil
}
func printSettingValue(store *storage.Storage, tx *storage.Tx, name string) error {
if name == "" {
return fmt.Errorf("setting name must be specified")
}
setting, err := store.Setting(tx, name)
if err != nil {
return fmt.Errorf("could not retrieve setting '%v'", err)
}
if setting == nil {
return fmt.Errorf("no such setting '%v'", name)
}
fmt.Println(setting.Value)
return nil
}
func printSettingAndValue(name, value string) {
fmt.Printf("%v=%v\n", name, value)
}
func amendSetting(store *storage.Storage, tx *storage.Tx, name, value string) error {
if name == "" {
return fmt.Errorf("setting name must be specified")
}
if value == "" {
return fmt.Errorf("setting '%v' value must be specified", name)
}
setting, err := store.Setting(tx, name)
if err != nil {
return fmt.Errorf("could not retrieve setting '%v'", err)
}
if setting == nil {
return fmt.Errorf("no such setting '%v'", name)
}
if _, err = store.UpdateSetting(tx, name, value); err != nil {
return fmt.Errorf("could not update setting '%v': %v", name, err)
}
return nil
}