Skip to content

Commit

Permalink
fix after review
Browse files Browse the repository at this point in the history
Signed-off-by: Horiodino <[email protected]>
  • Loading branch information
Horiodino committed Jun 11, 2024
1 parent 0fa592f commit be8b642
Showing 1 changed file with 56 additions and 23 deletions.
79 changes: 56 additions & 23 deletions cmd/direnv/direnv.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package direnv

import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"

"github.com/buildsafedev/bsf/cmd/styles"
Expand Down Expand Up @@ -32,6 +34,12 @@ var Direnv = &cobra.Command{
os.Exit(1)
}
}

cmD := exec.Command("direnv", "allow")
err = cmD.Run()
if err != nil {
os.Exit(1)
}
},
}

Expand Down Expand Up @@ -66,7 +74,6 @@ func generateEnvrc() error {
}
return nil
}
fmt.Println(styles.HelpStyle.Render(" ✅ .envrc already exists"))

} else {
file, err := os.Create(".envrc")
Expand All @@ -81,13 +88,6 @@ func generateEnvrc() error {
return err
}

fmt.Println(styles.HelpStyle.Render(" ✅ .envrc generated"))

cmd := exec.Command("direnv", "allow")
err = cmd.Run()
if err != nil {
return err
}
}

return nil
Expand All @@ -111,7 +111,6 @@ func fetchGitignore() error {
return err
}
}
fmt.Println(styles.HelpStyle.Render(" ✅ .gitignore already exists"))

return nil
} else {
Expand All @@ -129,8 +128,6 @@ func fetchGitignore() error {
if err != nil {
return err
}

fmt.Println(styles.HelpStyle.Render(" ✅ .gitignore generated"))
}

return nil
Expand All @@ -139,25 +136,61 @@ func fetchGitignore() error {

func setDIrenv(args string) error {

if !strings.Contains(args, "=") {
return fmt.Errorf("Hint: use --env key=value")
} else {
file, err := os.OpenFile(".envrc", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
err := validateEnvVars(args)
if err != nil {
return err
}
file, err := os.OpenFile(".envrc", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}

_, err = file.WriteString("\nexport " + args)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
return err
}

cmd := exec.Command("direnv", "allow")
err = cmd.Run()
if err != nil {
return err
}

return nil
}

func validateEnvVars(args string) error {
validKeyValRegex := regexp.MustCompile(`^[\w]+=[^\s]+$`)

envVars := strings.Split(args, ",")

for _, envVar := range envVars {
if !validKeyValRegex.MatchString(envVar) {
return errors.New("Invalid key-value pair format")
}

_, err = file.WriteString("\nexport " + args)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
return err
resp := strings.SplitN(envVar, "=", 2)
key := resp[0]
value := resp[1]

if strings.ContainsAny(key, "= \t\n") {
return errors.New("Invalid characters in key")
}

if strings.ContainsAny(value, "\x00") {
return errors.New("Invalid characters in value")
}

cmd := exec.Command("direnv", "allow")
err = cmd.Run()
read, err := os.ReadFile(".envrc")
if err != nil {
return err
}

if strings.Contains(string(read), key) {
return errors.New("Key already exists")
}
}

return nil
}

0 comments on commit be8b642

Please sign in to comment.