-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathconfig.go
71 lines (61 loc) · 1.89 KB
/
config.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
package config
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sigs.k8s.io/yaml"
)
type AgentConfig struct {
WorkDir string `json:"workDirectory,omitempty"`
LocalEnabled bool `json:"localEnabled,omitempty"`
LocalPlanDir string `json:"localPlanDirectory,omitempty"`
AppliedPlanDir string `json:"appliedPlanDirectory,omitempty"`
RemoteEnabled bool `json:"remoteEnabled,omitempty"`
ConnectionInfoFile string `json:"connectionInfoFile,omitempty"`
PreserveWorkDir bool `json:"preserveWorkDirectory,omitempty"`
ImagesDir string `json:"imagesDirectory,omitempty"`
AgentRegistriesFile string `json:"agentRegistriesFile,omitempty"`
ImageCredentialProviderConfig string `json:"imageCredentialProviderConfig,omitempty"`
ImageCredentialProviderBinDir string `json:"imageCredentialProviderBinDirectory,omitempty"`
}
type ConnectionInfo struct {
KubeConfig string `json:"kubeConfig"`
Namespace string `json:"namespace"`
SecretName string `json:"secretName"`
}
func Parse(path string, result interface{}) error {
if path == "" {
return fmt.Errorf("empty file passed")
}
fi, err := os.Stat(path)
if err != nil {
return fmt.Errorf("error gathering file information for file %s: %w", path, err)
}
if err := permissionsCheck(fi, path); err != nil {
return err
}
if err := pathOwnedByRoot(fi, path); err != nil {
return err
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
file := filepath.Base(path)
switch {
case strings.Contains(file, ".json"):
return json.NewDecoder(f).Decode(result)
case strings.Contains(file, ".yaml"):
b, err := io.ReadAll(f)
if err != nil {
return err
}
return yaml.Unmarshal(b, result)
default:
return fmt.Errorf("file %s was not a JSON or YAML file", file)
}
}