/
secretsEncrypt.go
130 lines (107 loc) · 2.76 KB
/
secretsEncrypt.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
/*
Copyright © 2023 Dataflows
*/
package cmd
import (
"context"
"fmt"
"runtime"
"github.com/spf13/cobra"
"github.com/thedataflows/go-commons/pkg/config"
"github.com/thedataflows/go-commons/pkg/file"
"github.com/thedataflows/go-commons/pkg/log"
"github.com/thedataflows/go-commons/pkg/search"
"github.com/thedataflows/kubestrap/pkg/constants"
)
type SecretsEncrypt struct {
cmd *cobra.Command
parent *Secrets
}
var (
_ = NewSecretsEncrypt(secrets)
)
func init() {
}
func NewSecretsEncrypt(parent *Secrets) *SecretsEncrypt {
se := &SecretsEncrypt{
parent: parent,
}
se.cmd = &cobra.Command{
Use: "encrypt",
Short: "Encrypt secrets that are relative to the current project root directory",
Long: ``,
Aliases: []string{"e"},
RunE: se.RunSecretsEncryptCommand,
SilenceErrors: parent.Cmd().SilenceErrors,
SilenceUsage: parent.Cmd().SilenceUsage,
}
parent.Cmd().AddCommand(se.cmd)
se.cmd.Flags().BoolP(
se.KeyInplace(),
"i",
true,
"Write files in-place instead of outputting to stdout",
)
// Bind flags to config
config.ViperBindPFlagSet(se.cmd, nil)
return se
}
func (s *SecretsEncrypt) RunSecretsEncryptCommand(cmd *cobra.Command, args []string) error {
if err := s.CheckRequiredFlags(); err != nil {
return err
}
if len(args) == 0 {
args = []string{constants.DefaultSecretFilesPattern}
}
for _, arg := range args {
for _, result := range s.findFiles(arg).Results {
if result.Err != nil {
log.Errorf("error finding files: %s", result.Err)
continue
}
if file.IsDirectory(result.FilePath) {
continue
}
log.Infof("%sing: %s", cmd.Use, result.FilePath)
newArgs := []string{
"sops",
"--" + cmd.Use,
"--config", s.parent.SopsConfig(),
s.Inplace(),
result.FilePath,
}
config.ViperSet(raw.Cmd(), raw.KeyBufferedOutput(), fmt.Sprintf("%v", s.Inplace() == ""))
if err := raw.RunRawCommand(raw.Cmd(), newArgs); err != nil {
log.Error(err)
continue
}
}
}
return nil
}
func (s *SecretsEncrypt) findFiles(pattern string) *search.Results {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
finder := &search.JustLister{
OpenFile: false,
}
// This wil not filter anything, will return all files and all directories
fileFilter := &search.FileFilterByPattern{
PlainPattern: "",
RegexPattern: pattern,
ApplyToDirs: false,
}
return search.FindFile(ctx, s.parent.KubeClusterDir(), fileFilter, finder, runtime.NumCPU())
}
func (s *SecretsEncrypt) CheckRequiredFlags() error {
return s.parent.CheckRequiredFlags()
}
func (s *SecretsEncrypt) KeyInplace() string {
return "in-place"
}
func (s *SecretsEncrypt) Inplace() string {
if config.ViperGetBool(s.cmd, s.KeyInplace()) {
return "--in-place"
}
return ""
}