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

Feat/init project cmd #6

Merged
merged 4 commits into from
Jan 27, 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
99 changes: 89 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"log"
"os"
"os/exec"

"gopkg.in/yaml.v2"

"github.com/charmbracelet/huh"
"github.com/samber/lo"
)
Expand Down Expand Up @@ -57,7 +60,37 @@ func welcomeFormCmd() (subcommand SubcommandType) {
return
}

type Config struct {
Project ProjectDetails `yaml:"project"`
}

func initProjectCmd() {
project, err := askProjectDetailsForm()
if err != nil || project.Name == "" || project.Owner == "" {
log.Fatal("Invalid project details")
}
log.Println("Project Details: ", project)

cfg := Config{
Project: *project,
}

file := "bruh.yaml"

if _, err := os.Stat(file); !os.IsNotExist(err) {
log.Fatal("bruh.yaml already exists.")
}

blob, err := yaml.Marshal(cfg)
if err != nil {
log.Fatal(err)
}

err = os.WriteFile(file, blob, 0644)
if err != nil {
log.Fatal(err)
}

mkdirCmd := exec.Command("mkdir", AppsDir, LibsDir)
_, stderr := mkdirCmd.Output()
if stderr != nil {
Expand All @@ -75,6 +108,36 @@ func initProjectCmd() {
log.Println("Initialized go workspace")
}

type ProjectDetails struct {
Name string `yaml:"name"`
Owner string `yaml:"owner"`
}

func askProjectDetailsForm() (*ProjectDetails, error) {
var project ProjectDetails

err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Project Name:").
CharLimit(50).
Inline(true).
Value(&project.Name),
huh.NewInput().
Title("Owner:").
CharLimit(50).
Inline(true).
Value(&project.Owner),
),
).Run()

if err != nil {
return nil, err
}

return &project, nil
}

func createModuleCmd() {
var moduleType ModuleType
var moduleName string
Expand All @@ -98,16 +161,30 @@ func createModuleCmd() {
log.Fatal(err)
}

cfg := &Config{}

blob, err := os.ReadFile("bruh.yaml")
if err != nil {
log.Fatal(err)
}

err = yaml.Unmarshal(blob, cfg)
if err != nil {
log.Fatal(err)
}

project := cfg.Project

switch moduleType {
case App:
createAppCmd(moduleName)
createAppCmd(moduleName, project)
case Lib:
createLibCmd(moduleName)
createLibCmd(moduleName, project)
}
}

func createAppCmd(appName string) {
err := createGoModule(appName, App)
func createAppCmd(appName string, project ProjectDetails) {
err := createGoModule(appName, App, project)
if err != nil {
log.Fatal(err)
}
Expand All @@ -132,8 +209,8 @@ func createAppCmd(appName string) {
log.Println("Created main file: ", "main.go")
}

func createLibCmd(libName string) {
err := createGoModule(libName, Lib)
func createLibCmd(libName string, project ProjectDetails) {
err := createGoModule(libName, Lib, project)
if err != nil {
log.Fatal(err)
}
Expand All @@ -149,13 +226,13 @@ func createLibCmd(libName string) {
log.Println("Created main file: ", "main.go")
}

func createGoModule(moduleName string, moduleType ModuleType) error {
func createGoModule(moduleName string, moduleType ModuleType, project ProjectDetails) error {
err := createModuleFolder(moduleName, moduleType)
if err != nil {
return err
}

err = initGoModule(moduleName, moduleType)
err = initGoModule(moduleName, moduleType, project)
if err != nil {
return err
}
Expand All @@ -181,10 +258,12 @@ func createModuleFolder(moduleName string, moduleType ModuleType) error {
return nil
}

func initGoModule(moduleName string, moduleType ModuleType) error {
func initGoModule(moduleName string, moduleType ModuleType, project ProjectDetails) error {
rootDir := lo.If(moduleType == App, AppsDir).Else(LibsDir)

goModInitCmd := exec.Command("go", "mod", "init", moduleName)
module := "github.com/" + project.Owner + "/" + project.Name + "/" + rootDir + "/" + moduleName

goModInitCmd := exec.Command("go", "mod", "init", module)
goModInitCmd.Dir = rootDir + "/" + moduleName
_, err := goModInitCmd.Output()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module github.com/maxguuse/bruh

go 1.21.5

require github.com/charmbracelet/huh v0.3.0
require (
github.com/charmbracelet/huh v0.3.0
github.com/samber/lo v1.39.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
Expand All @@ -21,7 +25,6 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.5 // indirect
github.com/samber/lo v1.39.0 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=