-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
68 lines (56 loc) · 1.56 KB
/
state.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
package state
import (
// "github.com/hetznercloud/hcloud-go/hcloud"
"fmt"
"os"
"path"
"path/filepath"
"dario.cat/mergo"
"github.com/buyoio/goodies/git"
"github.com/buyoio/goodies/streams"
"github.com/buyoio/runr/pkg/i18n"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
func NewState(cmd *cobra.Command) (*cobra.Command, *Runr) {
streams := &streams.IO{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
l := &Runr{
cmd: cmd,
quiet: false,
config: getDefaultConfigPath(),
io: streams,
fileState: &State{
Logs: &Logs{},
},
}
cmd.PersistentPreRunE = l.persistentPreRunE
cmd.PersistentPostRunE = l.persistentPostRunE
config := l.config
if cwd, err := os.Getwd(); err == nil {
config, _ = filepath.Rel(cwd, config)
}
flags := cmd.PersistentFlags()
flags.StringVar(&l.config, "config", config, "Path to the config file")
flags.BoolVarP(&l.quiet, "quiet", "q", false, "Only print error messages")
l.fileState.Logs.AddFlags(flags)
return cmd, l
}
func (state *State) mergeSecrets() error {
secretsFile := path.Join(getDefaultConfigPath(), secretFileName)
if _, err := os.Stat(secretsFile); err != nil {
return nil
}
if !git.IsIgnored(secretsFile) {
return fmt.Errorf(i18n.T("Secrets file (%s) is not ignored by git"), secretsFile)
}
secrets := &State{}
yamlFile, err := os.ReadFile(secretsFile)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, secrets)
if err != nil {
return fmt.Errorf(i18n.T("Failed to unmarshal secrets file (%s)\n\n%s"), secretsFile, err)
}
return mergo.Merge(state, secrets, mergo.WithOverride)
}