Skip to content

Commit

Permalink
Merge pull request #83 from auth0/add/user-input-file
Browse files Browse the repository at this point in the history
[A0CLI-44] feat: Adding ability for users to pass files as input params in rules commands
  • Loading branch information
Alexis Kulash authored Jan 27, 2021
2 parents 65ffb62 + 4365353 commit 895677f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cli

import (
"fmt"
"io/ioutil"
"regexp"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
Expand Down Expand Up @@ -244,6 +246,16 @@ func createRulesCmd(cli *cli) *cobra.Command {
}
}

if strings.Contains(flags.Script, ".js") {
content, err := parseFileByName(flags.Script)

if err != nil {
return err
}

flags.Script = content
}

r := &management.Rule{
Name: &flags.Name,
Script: &flags.Script,
Expand Down Expand Up @@ -415,6 +427,16 @@ func updateRulesCmd(cli *cli) *cobra.Command {
}
}

if strings.Contains(flags.Script, ".js") {
content, err := parseFileByName(flags.Script)

if err != nil {
return err
}

flags.Script = content
}

r := &management.Rule{
Name: &flags.Name,
Script: &flags.Script,
Expand Down Expand Up @@ -466,3 +488,22 @@ func enableRule(rule *management.Rule, cli *cli) error {
func disableRule(rule *management.Rule, cli *cli) error {
return cli.api.Rule.Update(rule.GetID(), &management.Rule{Enabled: auth0.Bool(false)})
}

// TODO: Fix tildas when passing relative filepaths, e.g. ~/Downloads/example.js
func parseFileByName(inputFile string) (string, error) {
f, err := ioutil.ReadFile(inputFile)

if err != nil {
return "", fmt.Errorf("Error reading file: %s", err)
}

if err != nil {
return "", fmt.Errorf("Cannot parse file %s: %w", f, err)
}

if err != nil {
return "", err
}

return string(f), nil
}

0 comments on commit 895677f

Please sign in to comment.