Skip to content

Commit

Permalink
guard against using reserved words for function names
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Aug 26, 2021
1 parent cbb6160 commit d5af1b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/commands/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (d *DeployCmd) deploySync() error {
}
updates = append(updates, su...)
d.updates = updates
if err := d.validateUpdates(); err != nil {
return fmt.Errorf("deployment failed - %v", err)
}
return nil
}

Expand Down Expand Up @@ -426,3 +429,12 @@ func (d *DeployCmd) refreshCredentials() error {
d.aws = awsClient
return nil
}

func (d *DeployCmd) validateUpdates() error {
for _, u := range d.updates {
if u.Function != nil && u.Action == mantil.Add && !mantil.FunctionNameAvailable(u.Function.Name) {
return fmt.Errorf("api name \"%s\" is reserved", u.Function.Name)
}
}
return nil
}
3 changes: 3 additions & 0 deletions internal/commands/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
)

func Api(name string, methods []string) error {
if !mantil.FunctionNameAvailable(name) {
return fmt.Errorf("could not generate api - name \"%s\" is reserved", name)
}
projectPath, err := mantil.FindProjectRoot(".")
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions internal/mantil/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mantil

var reservedFunctionNames = []string{
"public",
}

func FunctionNameAvailable(name string) bool {
for _, rn := range reservedFunctionNames {
if name == rn {
return false
}
}
return true
}

0 comments on commit d5af1b5

Please sign in to comment.