-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_env.go
66 lines (55 loc) · 1.81 KB
/
plugin_env.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
package kplugins
import (
"github.com/ShindouMihou/korin/internal/kstrings"
"github.com/ShindouMihou/korin/pkg/klabels"
"os"
"strings"
)
type PluginEnvironmentKey struct {
Plugin
}
func NewPluginEnvironmentKey() Plugin {
return PluginEnvironmentKey{}
}
func (p PluginEnvironmentKey) Name() string {
return "KorinEnvironmentKey"
}
func (p PluginEnvironmentKey) Group() string {
return "pw.mihou"
}
func (p PluginEnvironmentKey) Version() string {
return "1.0.0"
}
func (p PluginEnvironmentKey) Context(file string) *any {
return nil
}
func (p PluginEnvironmentKey) FreeContext(file string) {
}
func (p PluginEnvironmentKey) Process(line string, index int, headers *Headers, stack []klabels.Analysis, context *any) (string, error) {
analysis := stack[index]
return ReadHelper.Require(klabels.VariableKind, analysis.Labels, func(label klabels.Label) (string, error) {
variables := (label).Data.([]klabels.VariableDeclaration)
return ReadHelper.GetParameters("+k:env", analysis.Labels, func(parameters []string) (string, error) {
var kind string
for i, variable := range variables {
if variable.Value != nil && kstrings.HasPrefix(*variable.Value, "\"{$ENV:") && kstrings.HasSuffix(*variable.Value, "}\"") {
environmentKey := strings.TrimPrefix(*variable.Value, "\"{$ENV:")
environmentKey = strings.TrimSuffix(environmentKey, "}\"")
environmentValue := os.Getenv(environmentKey)
if len(parameters) == 1 {
kind = parameters[0]
variable.Type = ¶meters[0]
} else {
if variable.Type != nil {
kind = *variable.Type
}
}
newValue := SyntaxHelper.AutoValueBasedOnKind(environmentValue, kind)
variable.Value = &newValue
variables[i] = variable
}
}
return SyntaxHelper.AutoVariableDeclaration(line, index, stack, variables), nil
})
})
}