-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to generate infrastructure and inject the provider …
…to DI
- Loading branch information
Showing
23 changed files
with
607 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.