-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve support for instance ID tag
- Loading branch information
1 parent
f314c7c
commit be0cdd5
Showing
9 changed files
with
163 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,33 @@ | ||
package validationutil | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
IDMaxLen = 50 // maximum length of an ID | ||
sanitizeIDRegex = regexp.MustCompile(`[^a-zA-Z0-9_\-\.]+`) // matches invalid characters in an ID | ||
idRegex = regexp.MustCompile(`[a-zA-Z0-9_\-\.]*`) // matches a valid ID (including empty string) | ||
) | ||
|
||
func SanitizeID(id string) string { | ||
return sanitizeIDRegex.ReplaceAllString(id, "_") | ||
} | ||
|
||
// ValidateID checks if an ID is valid. | ||
// It returns an error if the ID contains invalid characters, is empty, or is too long. | ||
// The maxLen parameter is the maximum length of the ID. If maxLen is 0, the ID length is not checked. | ||
func ValidateID(id string, maxLen int) error { | ||
if !idRegex.MatchString(id) { | ||
return errors.New("contains invalid characters") | ||
} | ||
if len(id) == 0 { | ||
return errors.New("empty") | ||
} | ||
if maxLen > 0 && len(id) > maxLen { | ||
return fmt.Errorf("too long (> %d chars)", maxLen) | ||
} | ||
return nil | ||
} |
6 changes: 4 additions & 2 deletions
6
...rnal/config/stringutil/stringutil_test.go → ...fig/validationutil/validationutil_test.go
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
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
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,36 @@ | ||
package repo | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// TagForPlan returns a tag for the plan. | ||
func TagForPlan(planId string) string { | ||
return fmt.Sprintf("plan:%s", planId) | ||
} | ||
|
||
// TagForInstance returns a tag for the instance. | ||
func TagForInstance(instanceId string) string { | ||
return fmt.Sprintf("created-by:%s", instanceId) | ||
} | ||
|
||
// InstanceIDFromTags returns the instance ID from the tags, or an empty string if not found. | ||
func InstanceIDFromTags(tags []string) string { | ||
for _, tag := range tags { | ||
if strings.HasPrefix(tag, "created-by:") { | ||
return tag[len("created-by:"):] | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
// PlanFromTags returns the plan ID from the tags, or an empty string if not found. | ||
func PlanFromTags(tags []string) string { | ||
for _, tag := range tags { | ||
if strings.HasPrefix(tag, "plan:") { | ||
return tag[len("plan:"):] | ||
} | ||
} | ||
return "" | ||
} |
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
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
Oops, something went wrong.