mirror of
https://github.com/usual2970/certimate.git
synced 2025-08-16 14:21:46 +00:00
Support deploying one certificate to multiple SSH hosts, and support deploying multiple certificates to one SSH host.
This commit is contained in:
30
internal/utils/variables/variables.go
Normal file
30
internal/utils/variables/variables.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package variables
|
||||
|
||||
import "strings"
|
||||
|
||||
// Parse2Map 将变量赋值字符串解析为map
|
||||
func Parse2Map(str string) map[string]string {
|
||||
|
||||
m := make(map[string]string)
|
||||
|
||||
lines := strings.Split(str, ";")
|
||||
|
||||
for _, line := range lines {
|
||||
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
kv := strings.Split(line, "=")
|
||||
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
m[kv[0]] = kv[1]
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
56
internal/utils/variables/variables_test.go
Normal file
56
internal/utils/variables/variables_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package variables
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse2Map(t *testing.T) {
|
||||
type args struct {
|
||||
str string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
args: args{
|
||||
str: "a=1;b=2;c=3",
|
||||
},
|
||||
want: map[string]string{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
"c": "3",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "test2",
|
||||
args: args{
|
||||
str: `a=1;
|
||||
b=2;
|
||||
c=`,
|
||||
},
|
||||
want: map[string]string{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
"c": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "test3",
|
||||
args: args{
|
||||
str: "1",
|
||||
},
|
||||
want: map[string]string{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := Parse2Map(tt.args.str); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Parse2Map() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user