-
Notifications
You must be signed in to change notification settings - Fork 203
/
common.go
84 lines (65 loc) · 1.44 KB
/
common.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
package secret
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"golang.org/x/crypto/ssh/terminal"
"github.com/flant/werf/pkg/logger"
"github.com/flant/werf/pkg/util"
)
type GenerateOptions struct {
FilePath string
OutputFilePath string
Values bool
}
func ReadFileData(filePath string) ([]byte, error) {
if exist, err := util.FileExists(filePath); err != nil {
return nil, err
} else if !exist {
absFilePath, err := filepath.Abs(filePath)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("secret file '%s' not found", absFilePath)
}
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return fileData, err
}
func InputFromInteractiveStdin() ([]byte, error) {
var data []byte
var err error
isStdoutTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
if isStdoutTerminal {
logger.LogHighlightF("Enter secret: ")
}
data, err = terminal.ReadPassword(int(os.Stdin.Fd()))
if isStdoutTerminal {
fmt.Println()
}
if err != nil {
return nil, err
}
return data, nil
}
func InputFromStdin() ([]byte, error) {
var data []byte
var err error
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
return data, nil
}
func SaveGeneratedData(filePath string, data []byte) error {
if err := os.MkdirAll(filepath.Dir(filePath), 0777); err != nil {
return err
}
if err := ioutil.WriteFile(filePath, data, 0644); err != nil {
return err
}
return nil
}