Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[A0CLI-44] feat: Adding ability for users to pass files as input params in rules commands #83

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}