2021-01-26 11:31:08 +08:00
|
|
|
// Copyright 2020 The frp Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-12-11 11:46:12 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
glbEnvs map[string]string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
glbEnvs = make(map[string]string)
|
|
|
|
envs := os.Environ()
|
|
|
|
for _, env := range envs {
|
2022-04-05 12:48:57 +08:00
|
|
|
pair := strings.SplitN(env, "=", 2)
|
|
|
|
if len(pair) != 2 {
|
2018-12-11 11:46:12 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-04-05 12:48:57 +08:00
|
|
|
glbEnvs[pair[0]] = pair[1]
|
2018-12-11 11:46:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Values struct {
|
|
|
|
Envs map[string]string // environment vars
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetValues() *Values {
|
|
|
|
return &Values{
|
|
|
|
Envs: glbEnvs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func RenderContent(in []byte) (out []byte, err error) {
|
|
|
|
tmpl, errRet := template.New("frp").Parse(string(in))
|
2018-12-11 11:46:12 +08:00
|
|
|
if errRet != nil {
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := bytes.NewBufferString("")
|
|
|
|
v := GetValues()
|
|
|
|
err = tmpl.Execute(buffer, v)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
out = buffer.Bytes()
|
2018-12-11 11:46:12 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func GetRenderedConfFromFile(path string) (out []byte, err error) {
|
2018-12-11 11:46:12 +08:00
|
|
|
var b []byte
|
2021-09-29 10:33:57 +08:00
|
|
|
b, err = os.ReadFile(path)
|
2018-12-11 11:46:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
out, err = RenderContent(b)
|
2018-12-11 11:46:12 +08:00
|
|
|
return
|
|
|
|
}
|