/
dump.go
64 lines (54 loc) · 1.56 KB
/
dump.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
// Copyright © 2018 Harry Bagdi <harrybagdi@gmail.com>
package cmd
import (
"strings"
"github.com/ninjaneers-team/uropa/dump"
"github.com/ninjaneers-team/uropa/file"
"github.com/ninjaneers-team/uropa/state"
"github.com/ninjaneers-team/uropa/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
dumpCmdOpaStateFile string
dumpCmdStateFormat string
)
// dumpCmd represents the dump command
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Export Opa configuration to a file",
Long: `Dump command reads all the entities present in Opa
and writes them to a file on disk.
The file can then be read using the Sync o Diff command to again
configure Opa.`,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := utils.GetOpaClient(config)
if err != nil {
return err
}
format := file.Format(strings.ToUpper(dumpCmdStateFormat))
rawState, err := dump.Get(client, dumpConfig)
if err != nil {
return errors.Wrap(err, "reading configuration from Opa")
}
ks, err := state.Get(rawState)
if err != nil {
return errors.Wrap(err, "building state")
}
if err := file.OpaStateToFile(ks, file.WriteConfig{
Filename: dumpCmdOpaStateFile,
FileFormat: format,
}); err != nil {
return err
}
return nil
},
}
func init() {
rootCmd.AddCommand(dumpCmd)
dumpCmd.Flags().StringVarP(&dumpCmdOpaStateFile, "output-file", "o",
"opa", "file to which to write Opa's configuration."+
"Use '-' to write to stdout.")
dumpCmd.Flags().StringVar(&dumpCmdStateFormat, "format",
"yaml", "output file format: json or yaml")
}