-
Notifications
You must be signed in to change notification settings - Fork 42
/
utils.go
175 lines (141 loc) · 3.62 KB
/
utils.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
package cmd
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const ToolName = "direktivctl"
var maxSize int64 = 1073741824
func ProjectFolder() (string, error) {
projectFile := viper.GetString("projectFile")
if projectFile != "" {
return path.Dir(projectFile), nil
}
return "", fmt.Errorf("project directory not found")
}
func Fail(cmd *cobra.Command, s string, x ...interface{}) {
cmd.PrintErrf(strings.TrimSuffix(s, "\n")+"\n", x...)
os.Exit(1)
}
func pingNamespace() error {
urlGetNode := fmt.Sprintf("%s/tree/", UrlPrefix)
req, err := http.NewRequestWithContext(
context.Background(),
http.MethodGet,
urlGetNode,
nil,
)
if err != nil {
return fmt.Errorf("failed to create request file: %w", err)
}
AddAuthHeaders(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to ping namespace %s: %w", urlGetNode, err)
}
defer resp.Body.Close()
// it is either ok or forbidden which means the namespace exists
// but the user might not have access to the explorer
// it would be still ok to e.g. send events
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusForbidden {
// if resp.StatusCode == http.StatusUnauthorized {
// return fmt.Errorf("failed to ping namespace %s, request was unauthorized", urlGetNode)
// }
errBody, err := io.ReadAll(resp.Body)
if err == nil {
return fmt.Errorf("failed to ping namespace %s, server responded with %s\n------DUMPING ERROR BODY ------\n%s", urlGetNode, resp.Status, string(errBody))
}
return fmt.Errorf("failed to ping namespace %s, server responded with %s\n------DUMPING ERROR BODY ------\nCould read response body", urlGetNode, resp.Status)
}
return nil
}
func SafeLoadFile(filePath string) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
if filePath == "" {
// skip if filePath is empty
return buf, nil
}
fStat, err := os.Stat(filePath)
if err != nil {
return buf, err
}
if fStat.Size() > maxSize {
return buf,
fmt.Errorf("file is larger than maximum allowed size: %v. Set configfile 'max-size' to change",
maxSize)
}
fData, err := os.ReadFile(filePath)
if err != nil {
return buf, err
}
buf = bytes.NewBuffer(fData)
return buf, nil
}
func SafeLoadStdIn() (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
fi, err := os.Stdin.Stat()
if err != nil {
return buf, err
}
if fi.Mode()&os.ModeNamedPipe == 0 {
// No stdin
return buf, nil
}
if fi.Size() > maxSize {
return buf,
fmt.Errorf("stdin is larger than maximum allowed size: %v. Set configfile 'max-size' to change",
maxSize)
}
fData, err := io.ReadAll(os.Stdin)
if err != nil {
return buf, err
}
buf = bytes.NewBuffer(fData)
return buf, nil
}
func InitConfiguration(cmd *cobra.Command, args []string) {
err := initCLI(cmd)
if err != nil {
Fail(cmd, "Got an error while initializing: %v", err)
}
cmdPrepareSharedValues()
if err := pingNamespace(); err != nil {
log.Fatalf("%v", err)
}
}
func InitConfigurationAndProject(cmd *cobra.Command, args []string) {
InitConfiguration(cmd, args)
err := initProjectDir(cmd)
if err != nil {
Fail(cmd, "Got an error while initializing: %v", err)
}
err = InitWD()
if err != nil {
Fail(cmd, "Got an error while initializing: %v", err)
}
}
func InitWD() error {
directory := viper.GetString("directory")
if directory == "" {
pwd, err := os.Getwd()
if err != nil {
return err
}
viper.Set("directory", pwd)
}
return nil
}
func GetMaxSize() int64 {
if cfgMaxSize := viper.GetInt64("max-size"); cfgMaxSize > 0 {
return cfgMaxSize
}
return maxSize
}