value.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2020 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package legacy
  15. import (
  16. "bytes"
  17. "os"
  18. "strings"
  19. "text/template"
  20. )
  21. var glbEnvs map[string]string
  22. func init() {
  23. glbEnvs = make(map[string]string)
  24. envs := os.Environ()
  25. for _, env := range envs {
  26. pair := strings.SplitN(env, "=", 2)
  27. if len(pair) != 2 {
  28. continue
  29. }
  30. glbEnvs[pair[0]] = pair[1]
  31. }
  32. }
  33. type Values struct {
  34. Envs map[string]string // environment vars
  35. }
  36. func GetValues() *Values {
  37. return &Values{
  38. Envs: glbEnvs,
  39. }
  40. }
  41. func RenderContent(in []byte) (out []byte, err error) {
  42. tmpl, errRet := template.New("frp").Parse(string(in))
  43. if errRet != nil {
  44. err = errRet
  45. return
  46. }
  47. buffer := bytes.NewBufferString("")
  48. v := GetValues()
  49. err = tmpl.Execute(buffer, v)
  50. if err != nil {
  51. return
  52. }
  53. out = buffer.Bytes()
  54. return
  55. }
  56. func GetRenderedConfFromFile(path string) (out []byte, err error) {
  57. var b []byte
  58. b, err = os.ReadFile(path)
  59. if err != nil {
  60. return
  61. }
  62. out, err = RenderContent(b)
  63. return
  64. }