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

Added common dependencies #95

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ We contributors to BuildSafe:
* Hanshal(@hanshal101)
* Manik(@manik2708)
* Balaaditya(@BalaadityaPatanjali)

* Utsab(@utsab818)
35 changes: 27 additions & 8 deletions cmd/init/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"strings"

"github.com/buildsafedev/bsf/pkg/hcl2nix"
"github.com/buildsafedev/bsf/pkg/langdetect"
Expand All @@ -13,9 +14,13 @@ import (
var (
commonDevDeps = []string{"coreutils-full@9.5", "bash@5.2.15"}
commonRTDeps = []string{"cacert@3.95"}
rustDeps = []string{"cargo@1.75.0"}
pythonDeps = []string{"python3@3.12.2", "poetry@1.8.2"}
goDeps = []string{"go@1.22.3", "gotools@0.18.0", "delve@1.22.1"}
jsNpmDeps = []string{"nodejs@20.11.1"}
)

func generatehcl2NixConf(pt langdetect.ProjectType, pd *langdetect.ProjectDetails, baseImgName string) (hcl2nix.Config, error) {
func generatehcl2NixConf(pt langdetect.ProjectType, pd *langdetect.ProjectDetails, baseImgName string, addCommonDeps bool, commonDepsType string) (hcl2nix.Config, error) {
switch pt {
case langdetect.GoModule:
return genGoModuleConf(pd), nil
Expand All @@ -34,18 +39,32 @@ func generatehcl2NixConf(pt langdetect.ProjectType, pd *langdetect.ProjectDetail
}
return config, nil
case langdetect.BaseImage:
return generateEmptyConf(baseImgName), nil
return generateEmptyConf(baseImgName, addCommonDeps, commonDepsType), nil
default:
return hcl2nix.Config{
Packages: hcl2nix.Packages{},
}, fmt.Errorf("language is not supported")
}
}

func generateEmptyConf(imageName string) hcl2nix.Config {
func generateEmptyConf(imageName string, addCommonDeps bool, commonDepsType string) hcl2nix.Config {
devDeps := commonDevDeps
if addCommonDeps {
commonDepsType := strings.ToLower(strings.TrimSpace(commonDepsType))
switch commonDepsType {
case "go":
devDeps = append(devDeps, goDeps...)
case "python":
devDeps = append(devDeps, pythonDeps...)
case "rust":
devDeps = append(devDeps, rustDeps...)
case "jsnpm":
devDeps = append(devDeps, jsNpmDeps...)
}
}
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: commonDevDeps,
Development: devDeps,
Runtime: commonRTDeps,
},
OCIArtifact: []hcl2nix.OCIArtifact{
Expand Down Expand Up @@ -82,7 +101,7 @@ func genRustCargoConf() (hcl2nix.Config, error) {
CrateName = "my-project"
}

rustDevDeps := append(commonDevDeps, "cargo@1.75.0")
rustDevDeps := append(commonDevDeps, rustDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: rustDevDeps,
Expand All @@ -99,7 +118,7 @@ func genRustCargoConf() (hcl2nix.Config, error) {

func genPythonPoetryConf() hcl2nix.Config {
// TODO: maybe we should note down the path of the poetry.lock file and use it here.
poetryDevDeps := append(commonDevDeps, "python3@3.12.2", "poetry@1.8.2")
poetryDevDeps := append(commonDevDeps, pythonDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: poetryDevDeps,
Expand Down Expand Up @@ -130,7 +149,7 @@ func genGoModuleConf(pd *langdetect.ProjectDetails) hcl2nix.Config {

}

goDevDeps := append(commonDevDeps, "go@1.22.3", "gotools@0.18.0", "delve@1.22.1")
goDevDeps := append(commonDevDeps, goDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: goDevDeps,
Expand Down Expand Up @@ -161,7 +180,7 @@ func genJsNpmConf() (hcl2nix.Config, error) {
return hcl2nix.Config{}, fmt.Errorf("error fetching project name: %v", err)
}

nodeDevDeps := append(commonDevDeps, "nodejs@20.11.1")
nodeDevDeps := append(commonDevDeps, jsNpmDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: nodeDevDeps,
Expand Down
19 changes: 17 additions & 2 deletions cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,29 @@ var InitCmd = &cobra.Command{

var imageName string
var pt langdetect.ProjectType
var includeCommonDeps bool
var commonDeps string

if isBaseImage {
imageName, err = IoPrompt("What should the image name be?")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}
pt = langdetect.BaseImage

includeCommonDeps, err = YesNoPrompt("Would you like to add common dependencies (compiler, linter, debugger, etc.)?")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}

if includeCommonDeps {
commonDeps, err = IoPrompt("Which common deps would you like to add? (Go/Python/Rust/JsNpm)")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}
}
}

sc, err := search.NewClientWithAddr(conf.BuildSafeAPI, conf.BuildSafeAPITLS)
Expand All @@ -60,7 +75,7 @@ var InitCmd = &cobra.Command{
os.Exit(1)
}

m := model{sc: sc, pt: pt, baseImgName: imageName}
m := model{sc: sc, pt: pt, baseImgName: imageName, addCommonDeps: includeCommonDeps, commonDepsType: commonDeps}
m.resetSpinner()
if _, err := tea.NewProgram(m).Run(); err != nil {
os.Exit(1)
Expand Down
26 changes: 14 additions & 12 deletions cmd/init/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ var (
sucessStyle = styles.SucessStyle.Render
spinnerStyle = styles.SpinnerStyle
// helpStyle = styles.HelpStyle.Render
errorStyle = styles.ErrorStyle.Render
stages = 4
utsab818 marked this conversation as resolved.
Show resolved Hide resolved
errorStyle = styles.ErrorStyle.Render
stages = 4
)

type model struct {
spinner spinner.Model
sc buildsafev1.SearchServiceClient
stageMsg string
permMsg string
stage int
pt langdetect.ProjectType
pd *langdetect.ProjectDetails
baseImgName string
spinner spinner.Model
sc buildsafev1.SearchServiceClient
stageMsg string
permMsg string
stage int
pt langdetect.ProjectType
pd *langdetect.ProjectDetails
baseImgName string
addCommonDeps bool
commonDepsType string
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -100,7 +102,7 @@ func (m *model) processStages(stage int) error {
if m.baseImgName != "" {
pt = langdetect.BaseImage
}

m.stageMsg = textStyle("Detected language as " + string(pt))
if m.pt == langdetect.Unknown {
m.permMsg = errorStyle("Project language isn't currently supported. Some features might not work.")
Expand All @@ -123,7 +125,7 @@ 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, m.baseImgName)
conf, err := generatehcl2NixConf(m.pt, m.pd, m.baseImgName, m.addCommonDeps, m.commonDepsType)
if err != nil {
m.stageMsg = errorStyle(err.Error())
return err
Expand Down
Loading