Skip to content

Commit

Permalink
direnv intigration
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 3641ab5 commit 0fa592f
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use flake
use flake bsf/.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/buildsafedev/bsf/cmd/build"
"github.com/buildsafedev/bsf/cmd/configure"
"github.com/buildsafedev/bsf/cmd/develop"
"github.com/buildsafedev/bsf/cmd/direnv"
"github.com/buildsafedev/bsf/cmd/dockerfile"
initCmd "github.com/buildsafedev/bsf/cmd/init"
"github.com/buildsafedev/bsf/cmd/nixgenerate"
Expand Down Expand Up @@ -63,6 +64,7 @@ func Execute() {
rootCmd.AddCommand(scan.ScanCmd)
rootCmd.AddCommand(update.UpdateCmd)
rootCmd.AddCommand(attestation.AttCmd)
rootCmd.AddCommand(direnv.Direnv)

if os.Getenv("BSF_DEBUG_MODE") == "true" {
rootCmd.AddCommand(configure.ConfigureCmd)
Expand Down
163 changes: 163 additions & 0 deletions cmd/direnv/direnv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package direnv

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

"github.com/buildsafedev/bsf/cmd/styles"
"github.com/spf13/cobra"
)

var Direnv = &cobra.Command{
Use: "direnv",
Short: "direnv initializes the direnv environment for the project",
Run: func(cmd *cobra.Command, args []string) {
err := generateEnvrc()
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
os.Exit(1)
}
err = fetchGitignore()
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
os.Exit(1)
}

if envVar != "" {
err = setDIrenv(envVar)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
os.Exit(1)
}
}
},
}

var (
envVar string
)

func init() {
Direnv.Flags().StringVarP(&envVar, "env", "e", "", "set environment variable [key=value]")
}

func generateEnvrc() error {

if _, err := os.Stat(".envrc"); err == nil {

read, err := os.ReadFile(".envrc")
if err != nil {
return err
}

if !strings.Contains(string(read), "use flake bsf/.") {

file, err := os.OpenFile(".envrc", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}

_, err = file.WriteString("\nuse flake bsf/.")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
return err
}
return nil
}
fmt.Println(styles.HelpStyle.Render(" ✅ .envrc already exists"))

} else {
file, err := os.Create(".envrc")
if err != nil {
return err
}

defer file.Close()

_, err = file.WriteString("use flake bsf/.")
if err != nil {
return err
}

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

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

return nil
}

func fetchGitignore() error {

if _, err := os.Stat(".gitignore"); err == nil {

read, err := os.ReadFile(".gitignore")
if err != nil {
return err
}

if !strings.Contains(string(read), ".envrc") {

file, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_WRONLY, 0644)
_, err = file.WriteString("\n.envrc")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
return err
}
}
fmt.Println(styles.HelpStyle.Render(" ✅ .gitignore already exists"))

return nil
} else {
_, err := os.Create(".gitignore")
if err != nil {
return err
}

file, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}

_, err = file.WriteString("\n.envrc")
if err != nil {
return err
}

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

return nil

}

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 = 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
}
56 changes: 1 addition & 55 deletions cmd/precheck/precheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,60 +71,11 @@ func IsFlakesEnabled() {
}
}

// generates .envrc for Direnv intigration
func generateEnvrc() error {

if _, err := os.Stat(".envrc"); err == nil {

read, err := os.ReadFile(".envrc")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
os.Exit(1)
}

if !strings.Contains(string(read), "use flake bsf/.") {

file, err := os.OpenFile(".envrc", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
os.Exit(1)
}

_, err = file.WriteString("\nuse flake bsf/.")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
os.Exit(1)
}
return nil
}
fmt.Println(styles.HelpStyle.Render(" ✅ .envrc already exists"))

} else {
file, err := os.Create(".envrc")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
os.Exit(1)
}

defer file.Close()

_, err = file.WriteString("use flake bsf/.")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("", err.Error()))
os.Exit(1)
}
}

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

return nil
}

// AllPrechecks runs all the prechecks
func AllPrechecks() {
fmt.Println(styles.TextStyle.Render("Running prechecks..."))
var wg sync.WaitGroup
wg.Add(3)
wg.Add(2)
go func() {
ValidateNixVersion()
wg.Done()
Expand All @@ -134,11 +85,6 @@ func AllPrechecks() {
wg.Done()

}()
go func() {
generateEnvrc()
wg.Done()
}()

wg.Wait()

fmt.Println(styles.SucessStyle.Render(" Prechecks ran successfully"))
Expand Down

0 comments on commit 0fa592f

Please sign in to comment.