Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
feat: validate humanitec ids
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Würbach <[email protected]>
  • Loading branch information
johanneswuerbach committed Nov 13, 2023
1 parent 4e33322 commit 6e1428a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/command/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"

"github.com/score-spec/score-humanitec/internal/humanitec"
api "github.com/score-spec/score-humanitec/internal/humanitec_go/client"
Expand Down Expand Up @@ -74,6 +75,11 @@ func delta(cmd *cobra.Command, args []string) error {
return err
}

// Validate the ID
if err := validateIDs(); err != nil {
return err
}

// Prepare a new deployment
//
log.Print("Preparing a new deployment...\n")
Expand Down Expand Up @@ -124,3 +130,22 @@ func delta(cmd *cobra.Command, args []string) error {

return nil
}

var validID = regexp.MustCompile(`^[a-z0-9](?:-?[a-z0-9]+)+$`)

func validateIDs() error {
ids := []struct {
name string
id string
}{
{"organization", orgID}, {"application", appID}, {"environment", envID},
}

for _, e := range ids {
if !validID.MatchString(e.id) {
return fmt.Errorf("invalid %s id '%s'. Did you use the %s name instead of the id?", e.name, e.id, e.name)
}
}

return nil
}

0 comments on commit 6e1428a

Please sign in to comment.