Skip to content

Commit

Permalink
Merge branch 'main' into initpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshitgondwal authored Jul 13, 2024
2 parents 0e29326 + 6ba6ac0 commit 3c82d93
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
31 changes: 23 additions & 8 deletions cmd/init/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,34 @@ var (
)

func generatehcl2NixConf(pt langdetect.ProjectType, pd *langdetect.ProjectDetails) (hcl2nix.Config, error) {
fmt.Println("IN GENERATE HCL NIX CONFIG -------------------------------------------")
fmt.Println("NAME: ", pd.Name)
switch pt {
case langdetect.GoModule:
return genGoModuleConf(pd), nil
case langdetect.PythonPoetry:
return genPythonPoetryConf(), nil
return genPythonPoetryConf(pd), nil
case langdetect.RustCargo:
config, err := genRustCargoConf()
config, err := genRustCargoConf(pd)
if err != nil {
return hcl2nix.Config{}, err
}
return config, nil
case langdetect.JsNpm:
config, err := genJsNpmConf()
config, err := genJsNpmConf(pd)
if err != nil {
return hcl2nix.Config{}, err
}
return config, nil
default:
return generateEmptyConf(), nil
return generateEmptyConf(pd), nil
}
}

func generateEmptyConf() hcl2nix.Config {
func generateEmptyConf(pd *langdetect.ProjectDetails) hcl2nix.Config {
if pd.Name == "" {
pd.Name = "expl"
}
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: commonDevDeps,
Expand All @@ -58,7 +63,10 @@ func generateEmptyConf() hcl2nix.Config {
},
}
}
func genRustCargoConf() (hcl2nix.Config, error) {
func genRustCargoConf(pd *langdetect.ProjectDetails) (hcl2nix.Config, error) {
if pd.Name == "" {
pd.Name = "expl"
}
content, err := os.ReadFile("Cargo.toml")
if err != nil {
return hcl2nix.Config{}, fmt.Errorf("error reading file: %v", err)
Expand Down Expand Up @@ -104,7 +112,7 @@ func genRustCargoConf() (hcl2nix.Config, error) {
}, nil
}

func genPythonPoetryConf() hcl2nix.Config {
func genPythonPoetryConf(pd *langdetect.ProjectDetails) hcl2nix.Config {
// TODO: maybe we should note down the path of the poetry.lock file and use it here.
poetryDevDeps := append(commonDevDeps, "[email protected]", "[email protected]")
return hcl2nix.Config{
Expand All @@ -123,6 +131,7 @@ func genPythonPoetryConf() hcl2nix.Config {
OCIArtifact: []hcl2nix.OCIArtifact{
{
Artifact: "pkgs",

Name: baseImageName,
Cmd: []string{},
Entrypoint: []string{},
Expand All @@ -135,6 +144,9 @@ func genPythonPoetryConf() hcl2nix.Config {
}

func genGoModuleConf(pd *langdetect.ProjectDetails) hcl2nix.Config {
if pd.Name == "" {
pd.Name = "expl"
}
var name, entrypoint string
if pd != nil {
name = pd.Name
Expand Down Expand Up @@ -171,7 +183,10 @@ func genGoModuleConf(pd *langdetect.ProjectDetails) hcl2nix.Config {

}

func genJsNpmConf() (hcl2nix.Config, error) {
func genJsNpmConf(pd *langdetect.ProjectDetails) (hcl2nix.Config, error) {
if pd.Name == "" {
pd.Name = "expl"
}
data, err := os.ReadFile("package-lock.json")
if err != nil {
return hcl2nix.Config{}, fmt.Errorf("error reading file: %v", err)
Expand Down
67 changes: 66 additions & 1 deletion cmd/init/init.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package init

import (
"bufio"
"fmt"
"os"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/buildsafedev/bsf/cmd/styles"
"github.com/buildsafedev/bsf/pkg/clients/search"
"github.com/buildsafedev/bsf/pkg/hcl2nix"
"github.com/buildsafedev/bsf/pkg/langdetect"
)

// InitCmd represents the init command
Expand All @@ -33,19 +36,81 @@ var InitCmd = &cobra.Command{
os.Exit(1)
}

res := YesNoPrompt("Do you want docker file to be initialized", false)

var pd langdetect.ProjectDetails
var pt langdetect.ProjectType
if res {
pd.Name = *IOprompt("What should the name be?", "").(*string)
} else {
var projName = IOprompt("What type of projects you are setting up ? (Go/Rust/JavaScript/Poetry)", nil)
fmt.Println("TYPE : ", projName)
var lang langdetect.ProjectType
switch projName.(string) {
case "Go":
lang = langdetect.GoModule
case "Rust":
lang = langdetect.RustCargo
case "JS":
lang = langdetect.JsNpm
case "Poetry":
lang = langdetect.PythonPoetry
}
pt = lang
}

sc, err := search.NewClientWithAddr(conf.BuildSafeAPI, conf.BuildSafeAPITLS)
if err != nil {
os.Exit(1)
}

m := model{sc: sc}
m := model{sc: sc, pd: &pd, pt: pt}
m.resetSpinner()
if _, err := tea.NewProgram(m).Run(); err != nil {
os.Exit(1)
}
},
}

func YesNoPrompt(label string, defaultChoice bool) bool {
choices := "Y/n"
if !defaultChoice {
choices = "y/N"
}

r := bufio.NewReader(os.Stdin)
var s string
for {
fmt.Fprintf(os.Stderr, "%s (%s) ", strings.TrimSpace(label), choices)
s, _ = r.ReadString('\n')
s = strings.TrimSpace(s)
if s == "" {
return defaultChoice
}
s = strings.ToLower(s)
if s == "y" || s == "yes" {
return true
}
if s == "n" || s == "no" {
return false
}
}
}

func IOprompt(label string, defaultvalue any) any {
r := bufio.NewReader(os.Stdin)
var s string
for {
fmt.Fprintf(os.Stderr, "%s : ", strings.TrimSpace(label))
s, _ = r.ReadString('\n')
s = strings.TrimSpace(s)
if s == "" {
return nil
}
}
return &s
}

// GetBSFInitializers generates the nix files
func GetBSFInitializers() (bsfv1.SearchServiceClient, *hcl2nix.FileHandlers, error) {
if _, err := os.Stat("bsf.hcl"); err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/init/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (m *model) processStages(stage int) error {
return nil
case 1:
// var err error
pt, pd, err := langdetect.FindProjectType()
pt, _, err := langdetect.FindProjectType()
if err != nil {
m.stageMsg = errorStyle(err.Error())
return err
Expand All @@ -100,7 +100,6 @@ func (m *model) processStages(stage int) error {
m.permMsg = errorStyle("Project language isn't currently supported. Some features might not work.")
}
m.pt = pt
m.pd = pd
return nil
case 2:
m.stageMsg = textStyle("Resolving dependencies... ")
Expand All @@ -116,7 +115,6 @@ func (m *model) processStages(stage int) error {
defer fh.LockFile.Close()
defer fh.FlakeFile.Close()
defer fh.DefFlakeFile.Close()

conf, err := generatehcl2NixConf(m.pt, m.pd)
if err != nil {
m.stageMsg = errorStyle(err.Error())
Expand Down
Binary file added main
Binary file not shown.

0 comments on commit 3c82d93

Please sign in to comment.