-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (140 loc) · 4.62 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/Mrpye/go-action-workflow/actions/action_docker"
"github.com/Mrpye/go-action-workflow/actions/action_file"
"github.com/Mrpye/go-action-workflow/workflow"
"github.com/Mrpye/golib/log"
"github.com/spf13/viper"
)
func main() {
//***********
//Setup viper
//***********
viper.SetConfigFile("config.json")
viper.ReadInConfig()
//*****************
//create a workflow
//*****************
wf := workflow.CreateWorkflow()
//**********************************
//Only show errors and print actions
//**********************************
wf.LogLevel = workflow.LOG_INFO
wf.ActionList["docker_reg_download"] = action_docker.Action_DockerRegDownloadImages
wf.ActionList["docker_reg_upload"] = action_docker.Action_DockerRegUploadImages
wf.ActionList["file_delete"] = action_file.Action_Delete
wf.TargetMapFunc = MapConfigToTarget
wf.ReadConfigFunc["viper"] = ReadViperConfig
//*************************
//load the workflow manifest
//*************************
err := wf.LoadManifest("./workflow.yaml")
if err != nil {
println(err.Error())
}
//********************
//Run the workflow job
//********************
err = wf.RunJob("example", map[string]interface{}{"env": "dev"}, nil)
if err != nil {
println(err.Error())
}
}
func MapConfigToTarget(w *workflow.Workflow, m interface{}, target interface{}) (interface{}, error) {
//*****************************
//Get the env from runtime_vars
//*****************************
env, err := w.GetRuntimeVar("env")
if err != nil {
return nil, err
}
env = env.(string)
if w.LogLevel == workflow.LOG_VERBOSE {
log.LogVerbose(fmt.Sprintf("env: %s\n", env))
}
//*******************
//Get the target_name
//*******************
target_name, err := w.GetConfigTokenString("target_name", m.(*workflow.TemplateData), false)
if err != nil {
return nil, err
}
type_name := strings.ReplaceAll(strings.ToLower(reflect.TypeOf(target).String()), "*", "")
if target_name == "" {
target_name = type_name
} else {
target_name = type_name + "_" + target_name
}
if w.LogLevel == workflow.LOG_VERBOSE {
log.LogVerbose(fmt.Sprintf("Target name: %s\n", target_name))
}
//**********************************************
//Use reflection to map the config to the target
//**********************************************
v := reflect.Indirect(reflect.ValueOf(target))
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
c := typeOfS.Field(i).Tag
g := c.Get("yaml")
if g == "" {
continue
}
//******************************************
//Remap the config value to the target filed
//******************************************
if v.Field(i).Kind() == reflect.Int {
temp_config_val, _ := w.GetConfigValue("", target_name+"."+g, "int", "targets", env.(string))
if w.LogLevel == workflow.LOG_VERBOSE {
log.LogVerbose(fmt.Sprintf("%s: %s\n", g, temp_config_val))
}
v.Field(i).SetInt(temp_config_val.(int64))
} else if v.Field(i).Kind() == reflect.String {
temp_config_val, _ := w.GetConfigValue("", target_name+"."+g, "string", "targets", env.(string))
if w.LogLevel == workflow.LOG_VERBOSE {
log.LogVerbose(fmt.Sprintf("%s: %v\n", g, temp_config_val))
}
v.Field(i).SetString(temp_config_val.(string))
} else if v.Field(i).Kind() == reflect.Bool {
temp_config_val, _ := w.GetConfigValue("", target_name+"."+g, "bool", "targets", env.(string))
if w.LogLevel == workflow.LOG_VERBOSE {
log.LogVerbose(fmt.Sprintf("%s: %v\n", g, temp_config_val))
}
v.Field(i).SetBool(temp_config_val.(bool))
} else if v.Field(i).Kind() == reflect.Float64 {
temp_config_val, _ := w.GetConfigValue("", target_name+"."+g, "float", "targets", env.(string))
if w.LogLevel == workflow.LOG_VERBOSE {
log.LogVerbose(fmt.Sprintf("%s: %v\n", g, temp_config_val))
}
v.Field(i).SetFloat(temp_config_val.(float64))
}
}
return target, nil
}
func ReadViperConfig(key string, data_type string, custom ...string) (interface{}, error) {
if key == "" {
return nil, errors.New("key is empty")
}
//****************************************************************************
//custom is used to pass other string data to the config function
//this is useful if you want to pass a config file name or something like that
//****************************************************************************
/*for _, v := range custom {
println(v)
}*/
switch data_type {
case "string":
return viper.GetString(key), nil
case "int":
return viper.GetInt(key), nil
case "bool":
return viper.GetBool(key), nil
case "float64":
return viper.GetFloat64(key), nil
default:
return viper.GetString(key), nil
}
}