-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add name utils and update reconcile utils (#35)
* feat: add name utils * feat: update reconcile utils * feat: add function that escape `\t` to blank
- Loading branch information
Showing
3 changed files
with
119 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var reTab = regexp.MustCompile(`^\t+`) | ||
|
||
func IndentTabToSpaces(code string, spaces int) string { | ||
indentation := strings.Repeat(" ", spaces) | ||
return reTab.ReplaceAllString(code, indentation) | ||
} | ||
|
||
func IndentTab4Spaces(code string) string { | ||
return IndentTabToSpaces(code, 4) | ||
} | ||
|
||
func IndentTab2Spaces(code string) string { | ||
return IndentTabToSpaces(code, 2) | ||
} | ||
|
||
var re2Spaces = regexp.MustCompile(`^` + strings.Repeat(" ", 2)) | ||
var re4Spaces = regexp.MustCompile(`^` + strings.Repeat(" ", 4)) | ||
|
||
func IndentSpacesToTab(code string, spaces int) string { | ||
switch spaces { | ||
case 2: | ||
return re2Spaces.ReplaceAllString(code, "\t") | ||
case 4: | ||
return re4Spaces.ReplaceAllString(code, "\t") | ||
default: | ||
re := regexp.MustCompile(`^` + strings.Repeat(" ", spaces)) | ||
return re.ReplaceAllString(code, "\t") | ||
} | ||
} | ||
|
||
func Indent4SpacesToTab(code string) string { | ||
return IndentSpacesToTab(code, 4) | ||
} | ||
|
||
func Indent2SpacesToTab(code string) string { | ||
return IndentSpacesToTab(code, 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util | ||
|
||
type ResourceNameGenerator struct { | ||
InstanceName string | ||
RoleName string | ||
GroupName string | ||
} | ||
|
||
// NewResourceNameGenerator new a ResourceNameGenerator | ||
func NewResourceNameGenerator(instanceName, roleName, groupName string) *ResourceNameGenerator { | ||
return &ResourceNameGenerator{ | ||
InstanceName: instanceName, | ||
RoleName: roleName, | ||
GroupName: groupName, | ||
} | ||
} | ||
|
||
// NewResourceNameGeneratorOneRole new a ResourceNameGenerator without roleName | ||
func NewResourceNameGeneratorOneRole(instanceName, groupName string) *ResourceNameGenerator { | ||
return &ResourceNameGenerator{ | ||
InstanceName: instanceName, | ||
GroupName: groupName, | ||
} | ||
} | ||
|
||
// GenerateResourceName generate resource Name | ||
func (r *ResourceNameGenerator) GenerateResourceName(extraSuffix string) string { | ||
var res string | ||
if r.InstanceName != "" { | ||
res = r.InstanceName + "-" | ||
} | ||
if r.GroupName != "" { | ||
res = res + r.GroupName + "-" | ||
} | ||
if r.RoleName != "" { | ||
res = res + r.RoleName | ||
} else { | ||
res = res[:len(res)-1] | ||
} | ||
if extraSuffix != "" { | ||
return res + "-" + extraSuffix | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters