sg: cloud ephemeral - handle empty times better and santizes name better (#62795)

* santize names to how cloud sanitizes it

* handle empty times
This commit is contained in:
William Bezuidenhout 2024-05-20 18:54:50 +02:00 committed by GitHub
parent d8cc95009d
commit d531c60d5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 5 deletions

View File

@ -20,7 +20,9 @@ const CloudEmoji = "☁️"
func sanitizeInstanceName(name string) string {
name = strings.ToLower(name)
return strings.ReplaceAll(name, "/", "-")
name = strings.ReplaceAll(name, "/", "-")
name = strings.ReplaceAll(name, "_", "-")
return name
}
func inferInstanceNameFromBranch(ctx context.Context) (string, error) {

View File

@ -126,7 +126,7 @@ func createDeploymentForVersion(ctx context.Context, email, name, version string
}
pending.Writef("Deploy instance details: \n%s", inst.String())
pending.Complete(output.Linef(output.EmojiSuccess, output.StyleSuccess, "Deployment %q created for version %q - access at: %s", spec.Name, spec.Version, inst.URL))
pending.Complete(output.Linef(output.EmojiSuccess, output.StyleSuccess, "Deployment %q created for version %q - access at: %s", inst.Name, inst.Version, inst.URL))
return nil
}
@ -180,8 +180,8 @@ func createDeploymentName(originalName, version, email, branch string) string {
} else if version != "" {
// if a version is given we generate a name based on the email user and the given version
// to make sure the deployment is unique
user := strings.ReplaceAll(email[0:strings.Index(email, "@")], ".", "_")
deploymentName = user[:min(12, len(user))] + "_" + version
user := strings.ReplaceAll(email[0:strings.Index(email, "@")], ".", "-")
deploymentName = user[:min(12, len(user))] + "-" + version
} else {
deploymentName = branch
}

View File

@ -43,6 +43,14 @@ type Instance struct {
}
func (i *Instance) String() string {
// Protobuf returns the unix zero epoch if the time is nil, so we check for that
// and we also check if we do have a valid time that it is not zero
fmtTime := func(t time.Time) string {
if isUnixEpochZero(t) || t.IsZero() {
return "n/a"
}
return i.CreatedAt.Format(time.RFC3339)
}
return fmt.Sprintf(`ID : %s
Name : %s
InstanceType : %s
@ -59,7 +67,7 @@ Status : %s
ActionURL : %s
Error : %s
`, i.ID, i.Name, i.InstanceType, i.Environment, i.Version, i.URL, i.AdminEmail,
i.CreatedAt.Format(time.RFC3339), i.DeletedAt.Format(time.RFC3339), i.ExpiresAt.Format(time.RFC3339), i.Project, i.Region,
fmtTime(i.CreatedAt), fmtTime(i.DeletedAt), fmtTime(i.ExpiresAt), i.Project, i.Region,
i.Status.Status, i.Status.ActionURL, i.Status.Error)
}
@ -150,6 +158,10 @@ func newInstance(src *cloudapiv1.Instance) (*Instance, error) {
}, nil
}
func isUnixEpochZero(t time.Time) bool {
return t.Unix() == 0
}
func parseStatusReason(reason string) (string, string, error) {
if reason == "" {
return "", "", nil