forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-config-history.go
191 lines (169 loc) · 5.59 KB
/
admin-config-history.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package cmd
import (
"fmt"
"log"
"strconv"
"strings"
json "github.com/pydio/cells/x/jsonx"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/x/filex"
)
var (
configVersionShow string
configVersionDiff string
configVersionRestore string
configVersionDb string
)
// configHistoryCmd represents the list command
var configHistoryCmd = &cobra.Command{
Use: "history",
Short: "Show the history of all configuration changes",
Long: `
DESCRIPTION
View all changes ever made to the configuration and revert to a given version if necessary.
A version is created at each call to config.Save() inside the application, along with a log message
and the user originating this call.
`,
Run: func(cmd *cobra.Command, args []string) {
store := config.VersionsStore
if configVersionShow != "" {
if id, e := strconv.ParseUint(configVersionShow, 10, 64); e == nil {
version, e := store.Retrieve(id)
if e != nil {
log.Fatal("Cannot retrieve this version")
}
b, _ := json.MarshalIndent(version.Data, "", " ")
cmd.Println(string(b))
} else {
log.Fatal("Cannot parse version Id")
}
return
}
if configVersionDiff != "" {
var targetVersionDiff string
if strings.Contains(configVersionDiff, ":") {
parts := strings.Split(configVersionDiff, ":")
configVersionDiff = parts[0]
targetVersionDiff = parts[1]
}
if id, e := strconv.ParseUint(configVersionDiff, 10, 64); e == nil {
if id <= 1 {
log.Fatal("Please select a version Id higher than 1 for diffing with previous version")
}
version, e := store.Retrieve(id)
if e != nil {
log.Fatal("Cannot retrieve this version")
}
var previousVersion *filex.Version
if targetVersionDiff != "" {
targetId, e := strconv.ParseUint(targetVersionDiff, 10, 64)
if e != nil {
log.Fatal("Cannot parse target version")
}
previousVersion = version
version, e = store.Retrieve(targetId)
if e != nil {
log.Fatal("Cannot parse retrieve target version")
}
} else {
previousVersion, e = store.Retrieve(id - 1)
}
if e != nil {
log.Fatal("Cannot retrieve previous version")
}
differ := gojsondiff.New()
bytesPrev, _ := json.Marshal(previousVersion.Data)
bytesLast, _ := json.Marshal(version.Data)
d, e := differ.Compare(bytesPrev, bytesLast)
if e != nil {
log.Fatal("Cannot diff versions", e)
}
if !d.Modified() {
cmd.Println("No differences found between two versions")
return
}
config := formatter.AsciiFormatterConfig{
ShowArrayIndex: true,
Coloring: true,
}
formatter := formatter.NewAsciiFormatter(previousVersion.Data, config)
diffString, e := formatter.Format(d)
if e != nil {
log.Fatal("Cannot format diffs", e)
}
cmd.Print(diffString)
} else {
log.Fatal("Cannot parse version Id")
}
return
}
if configVersionRestore != "" {
if id, e := strconv.ParseUint(configVersionRestore, 10, 64); e == nil {
version, e := store.Retrieve(id)
if e != nil {
log.Fatal("Cannot retrieve this version")
}
prompt := promptui.Select{
Label: "This will override configuration with a previous version, are you sure you want to do that?",
Items: []string{"Yes", "No"},
}
index, _, _ := prompt.Run()
if index == 0 {
config.Set(version.Data)
config.Save("cli", "Config Restoration to version "+configVersionRestore)
}
} else {
log.Fatal("Cannot parse version Id")
}
return
}
versions, e := store.List(0, 20)
if e != nil {
log.Fatal(e)
}
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetHeader([]string{"Id", "Date", "Context", "Log Message"})
table.SetAutoWrapText(false)
for _, version := range versions {
table.Append([]string{
fmt.Sprintf("%d", version.Id),
version.Date.Format("2006 Jan _2 15:04:05"), //fmt.Sprintf("%s", version.Date),
version.User,
version.Log,
})
}
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.Render()
},
}
func init() {
configHistoryCmd.Flags().StringVar(&configVersionDiff, "diff", "", "Display a Diff between two versions, either by providing VERSION1:VERSION2 or just VERSION1 (will be compared to previous one)")
configHistoryCmd.Flags().StringVar(&configVersionShow, "cat", "", "Print the JSON content of the config for this version")
configHistoryCmd.Flags().StringVar(&configVersionRestore, "restore", "", "Restore configuration to this specific version")
configHistoryCmd.Flags().StringVar(&configVersionDb, "file", "", "Point to a specific DB file instead of default")
ConfigCmd.AddCommand(configHistoryCmd)
}