...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package config
18
19 import (
20 "encoding/json"
21 "net/http"
22 "path/filepath"
23 )
24
25 const secretToken = "<secret>"
26
27
28 type Secret string
29
30
31
32
33 var MarshalSecretValue bool = false
34
35
36 func (s Secret) MarshalYAML() (interface{}, error) {
37 if MarshalSecretValue {
38 return string(s), nil
39 }
40 if s != "" {
41 return secretToken, nil
42 }
43 return nil, nil
44 }
45
46
47 func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
48 type plain Secret
49 return unmarshal((*plain)(s))
50 }
51
52
53 func (s Secret) MarshalJSON() ([]byte, error) {
54 if MarshalSecretValue {
55 return json.Marshal(string(s))
56 }
57 if len(s) == 0 {
58 return json.Marshal("")
59 }
60 return json.Marshal(secretToken)
61 }
62
63 type Header map[string][]Secret
64
65 func (h *Header) HTTPHeader() http.Header {
66 if h == nil || *h == nil {
67 return nil
68 }
69
70 header := make(http.Header)
71
72 for name, values := range *h {
73 var s []string
74 if values != nil {
75 s = make([]string, 0, len(values))
76 for _, value := range values {
77 s = append(s, string(value))
78 }
79 }
80 header[name] = s
81 }
82
83 return header
84 }
85
86
87
88 type DirectorySetter interface {
89
90
91 SetDirectory(dir string)
92 }
93
94
95
96 func JoinDir(dir, path string) string {
97 if path == "" || filepath.IsAbs(path) {
98 return path
99 }
100 return filepath.Join(dir, path)
101 }
102
View as plain text