Skip to content

Commit

Permalink
feat: add support to generate infrastructure and inject the provider …
Browse files Browse the repository at this point in the history
…to DI
  • Loading branch information
mukezhz committed Jan 7, 2024
1 parent a0107cb commit 855c1c9
Show file tree
Hide file tree
Showing 23 changed files with 607 additions and 44 deletions.
89 changes: 89 additions & 0 deletions cmd/add_infrastructure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cmd

import (
"os"
"path/filepath"
"strings"

"github.com/gookit/color"
"github.com/mukezhz/geng/pkg/constant"
"github.com/mukezhz/geng/pkg/model"
"github.com/mukezhz/geng/pkg/terminal"
"github.com/mukezhz/geng/pkg/utility"
"github.com/spf13/cobra"
)

var addInfrastructureCmd = &cobra.Command{
Use: "add infra [name]",
Short: "Add a new infrastructure",
Args: cobra.MaximumNArgs(2),
Run: addInfrastructureHandler,
}

func addInfrastructureHandler(_ *cobra.Command, args []string) {
if len(args) > 0 && !strings.Contains(args[0], "infra") {
color.Redln("Error: invalid command")
return
}
projectModule, err := utility.GetModuleNameFromGoModFile()
if err != nil {
color.Redln("Error finding Module name from go.mod:", err)
return
}
data := utility.GetModuleDataFromModuleName("", projectModule.Module, projectModule.GoVersion)
currentDir, err := os.Getwd()
if err != nil {
color.Redln("Error getting current directory:", err)
panic(err)
}
projectPath, err := utility.FindGitRoot(currentDir)
if err != nil {
color.Redln("Error finding Git root:", err)
return
}
infrastructureModulePath := filepath.Join(projectPath, "pkg", "infrastructure", "module.go")
templateInfraPath := filepath.Join(".", "templates", "wesionary", "infrastructure")
infrasTmpl := utility.ListDirectory(templatesFS, templateInfraPath)
infras := utility.Map[string, string](infrasTmpl, func(q string) string {
return strings.Replace(q, ".tmpl", "", 1)
})
questions := []terminal.ProjectQuestion{
terminal.NewCheckboxQuestion(constant.ModueleNameKEY, "Select the infrastructure? [<space> to select]", infras),
}

terminal.StartInteractiveTerminal(questions)

addInfrastructure(questions, infrasTmpl, infrastructureModulePath, data, false)
utility.PrintColorizeInfrastructureDetail(data, infras)
}

func addInfrastructure(questions []terminal.ProjectQuestion, infrasTmpl []string, infrastructureModulePath string, data model.ModuleData, isNewProject bool) []int {
var functions []string
var items []int
for _, q := range questions {
switch q.Key {
case constant.InfrastructureNameKEY:
selected := q.Input.Selected()
for s := range selected {
functions = append(functions,
utility.GetFunctionDeclarations(filepath.Join(".", "templates", "wesionary", "infrastructure", infrasTmpl[s]))...,
)
items = append(items, s)
}
}
}
updatedCode := utility.AddListOfProvideInFxOptions(infrastructureModulePath, functions)
utility.WriteContentToPath(infrastructureModulePath, updatedCode)

for _, i := range items {
templatePath := filepath.Join(".", "templates", "wesionary", "infrastructure", infrasTmpl[i])
var targetRoot string
if isNewProject {
targetRoot = filepath.Join(data.ProjectName, "pkg", "infrastructure", strings.Replace(infrasTmpl[i], ".tmpl", ".go", 1))
} else {
targetRoot = filepath.Join(".", "pkg", "infrastructure", strings.Replace(infrasTmpl[i], ".tmpl", ".go", 1))
}
utility.GenerateFromEmbeddedTemplate(templatesFS, templatePath, targetRoot, data)
}
return items
}
18 changes: 17 additions & 1 deletion cmd/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"path/filepath"
"strings"

"github.com/gookit/color"
"github.com/mukezhz/geng/pkg/constant"
Expand All @@ -25,15 +26,23 @@ func createProject(cmd *cobra.Command, args []string) {
var projectDescription string
var author string
var directory string
var questions []terminal.ProjectQuestion

templateInfraPath := filepath.Join(".", "templates", "wesionary", "infrastructure")
infrasTmpl := utility.ListDirectory(templatesFS, templateInfraPath)
infras := utility.Map[string, string](infrasTmpl, func(q string) string {
return strings.Replace(q, ".tmpl", "", 1)
})

if len(args) == 0 {
questions := []terminal.ProjectQuestion{
questions = []terminal.ProjectQuestion{
terminal.NewShortQuestion(constant.ProjectNameKEY, constant.ProjectName+" *", "Enter Project Name:"),
terminal.NewShortQuestion(constant.ProjectModuleNameKEY, constant.ProjectModuleName+" *", "Enter Module Name:"),
terminal.NewShortQuestion(constant.AuthorKEY, constant.Author+" [Optional]", "Enter Author Detail[Mukesh Chaudhary <mukezhz@duck.com>] [Optional]"),
terminal.NewLongQuestion(constant.ProjectDescriptionKEY, constant.ProjectDescription+" [Optional]", "Enter Project Description [Optional]"),
terminal.NewShortQuestion(constant.GoVersionKEY, constant.GoVersion+" [Optional]", "Enter Go Version (Default: 1.20) [Optional]"),
terminal.NewShortQuestion(constant.DirectoryKEY, constant.Directory+" [Optional]", "Enter Project Directory (Default: package_name) [Optional]"),
terminal.NewCheckboxQuestion(constant.InfrastructureNameKEY, "Select the infrastructure? [<space> to select] [Optional]", infras),
}
terminal.StartInteractiveTerminal(questions)

Expand Down Expand Up @@ -86,6 +95,13 @@ func createProject(cmd *cobra.Command, args []string) {
color.Redln("Error generate file", err)
return
}
for _, q := range questions {
switch q.Key {
case constant.InfrastructureNameKEY:
infrastructureModulePath := filepath.Join(projectName, "pkg", "infrastructure", "module.go")
addInfrastructure(questions, infrasTmpl, infrastructureModulePath, data, true)
}
}

utility.PrintColorizeProjectDetail(data)
fmt.Println("")
Expand Down
1 change: 1 addition & 0 deletions cmd/run_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ func init() {
rootCmd.AddCommand(newModuleCmd)
rootCmd.AddCommand(newProjectCmd)
rootCmd.AddCommand(runProjectCmd)
rootCmd.AddCommand(addInfrastructureCmd)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand Down
31 changes: 13 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/charmbracelet/bubbles v0.17.1 h1:0SIyjOnkrsfDo88YvPgAWvZMwXe26TP6drRvmkjyUu4=
github.com/charmbracelet/bubbles v0.17.1/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
Expand Down Expand Up @@ -37,8 +40,6 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand All @@ -56,8 +57,8 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -96,34 +97,28 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=
go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU=
go.uber.org/fx v1.20.1 h1:zVwVQGS8zYvhh9Xxcu4w1M6ESyeMzebzj2NbSayZ4Mk=
go.uber.org/fx v1.20.1/go.mod h1:iSYNbHf2y55acNCwCXKx7LbWb5WG1Bnue5RDXz1OREg=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 2 additions & 0 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
GoVersionKEY = "goVersion"
DirectoryKEY = "directory"
ModueleNameKEY = "moduleName"
InfrastructureNameKEY = "infrastructureName"
)

const (
Expand All @@ -18,4 +19,5 @@ const (
GoVersion = "Go Version"
Directory = "Project Directory"
ModuleName = "Module Name"
InfrastructureName = "Infrastructures"
)
21 changes: 4 additions & 17 deletions pkg/terminal/bubble_tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package terminal

import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"log"

tea "github.com/charmbracelet/bubbletea"
)

func (m *Model) Init() tea.Cmd {
Expand All @@ -22,8 +22,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "ctrl+c", tea.KeyEsc.String(), tea.KeyEscape.String():
return m, tea.Quit
case tea.KeyEsc.String():
return m, tea.Quit

case "enter":
if m.index == len(m.questions)-1 {
m.done = true
Expand All @@ -41,7 +40,6 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *Model) View() string {
current := m.questions[m.index]
if m.done {
var output string
for _, q := range m.questions {
Expand All @@ -52,18 +50,7 @@ func (m *Model) View() string {
if m.width == 0 {
return "loading..."
}
// stack some left-aligned strings together in the center of the window
return lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Left,
current.Question,
m.styles.InputField.Render(current.Input.View()),
),
)
return DefaultView(m)
}

func (m *Model) Next() {
Expand Down
109 changes: 109 additions & 0 deletions pkg/terminal/check_box_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package terminal

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type CheckBoxField struct {
selected map[int]any
choices []string
cursor int
title string
}

func NewCheckBoxField(t string, choices []string) *CheckBoxField {
m := CheckBoxField{
choices: choices,
selected: make(map[int]any),
title: t,
}
return &m
}

func (m *CheckBoxField) Blink() tea.Msg {
return nil
}

func (m *CheckBoxField) Init() tea.Cmd {
return nil
}

func (m *CheckBoxField) Update(msg tea.Msg) (Input, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
}
return m, nil
}

func (m *CheckBoxField) View() string {
s := ""

for i, choice := range m.choices {
cursor := " "
if m.cursor == i {
cursor = ">"
}

checked := " " // not selected
if _, ok := m.selected[i]; ok {
checked = "x"
}
var style = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4")).
PaddingTop(2).
PaddingLeft(4).
Width(22)
style.Render(m.title)

s += fmt.Sprintf("\n\n %s [%s] %s \n", cursor, checked, choice)
}

// Send the UI for rendering
return s

}

func (m *CheckBoxField) Focus() tea.Cmd {
return nil
}

func (a *CheckBoxField) SetValue(s string) {

}

func (a *CheckBoxField) Blur() tea.Msg {
return nil
}

func (a *CheckBoxField) Value() string {
return ""
}

func (a *CheckBoxField) Selected() map[int]any {
return a.selected
}
Loading

0 comments on commit 855c1c9

Please sign in to comment.