Public Access
feat: basalt 0.0.1
This commit is contained in:
22 files changed
+1364
-1
No files matched your search
+123
@@ -0,0 +1,123 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/manifoldco/promptui"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func init() {
|
||||
newCmd.AddCommand(serviceCmd)
|
||||
}
|
||||
|
||||
type ServiceTemplateData struct {
|
||||
ModuleName string
|
||||
Name string
|
||||
LowerName string
|
||||
}
|
||||
|
||||
var serviceCmd = &cobra.Command{
|
||||
Use: "service",
|
||||
Short: "Create a new service",
|
||||
Long: `Create a new service for your Keystone project.`,
|
||||
Run: genService,
|
||||
}
|
||||
|
||||
func genService(cmd *cobra.Command, args []string) {
|
||||
namePrompt := promptui.Prompt{
|
||||
Label: "Service Name",
|
||||
}
|
||||
|
||||
serviceNameInput, err := namePrompt.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("Prompt failed %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
moduleName, err := getModuleName()
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error retrieving module name: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
print(moduleName)
|
||||
|
||||
serviceName := strings.ToLower(strings.TrimSpace(serviceNameInput))
|
||||
cases := cases.Title(language.English)
|
||||
|
||||
fmt.Printf("🚀 Creating service '%s'...\n", serviceName)
|
||||
|
||||
if err := genFile(
|
||||
"templates/service.go.tpl",
|
||||
serviceName,
|
||||
fmt.Sprintf("internal/service/%s.go", serviceName),
|
||||
ServiceTemplateData{
|
||||
ModuleName: moduleName,
|
||||
Name: cases.String(serviceName),
|
||||
LowerName: serviceName,
|
||||
},
|
||||
); err != nil {
|
||||
fmt.Printf("❌ Error creating service: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
updateServiceFile(serviceName, cases)
|
||||
|
||||
fmt.Printf("✅ Service '%s' created successfully!\n", serviceNameInput)
|
||||
|
||||
}
|
||||
|
||||
func updateServiceFile(serviceName string, cases cases.Caser) {
|
||||
currDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error getting current directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
serviceFilePath := fmt.Sprintf("%s/internal/service/service.go", currDir)
|
||||
|
||||
content, err := os.ReadFile(serviceFilePath)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error reading service file: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fileContent := string(content)
|
||||
srvc := cases.String(serviceName)
|
||||
|
||||
interfaceSnippet := fmt.Sprintf(`// %sService defines operations for %s
|
||||
type %sService interface {
|
||||
// TODO: Add service methods
|
||||
}
|
||||
|
||||
`, srvc, srvc, srvc)
|
||||
|
||||
compileTimeCheck := fmt.Sprintf("\t_ %sService = (*%sService)(nil)",
|
||||
srvc, serviceName)
|
||||
|
||||
fileContent = insertInterface(fileContent, interfaceSnippet)
|
||||
fileContent = strings.Replace(fileContent, "var (", "var (\n"+compileTimeCheck, 1)
|
||||
|
||||
err = os.WriteFile(serviceFilePath, []byte(fileContent), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error writing to service file: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Service interface for '%s' added successfully!\n", serviceName)
|
||||
}
|
||||
|
||||
func insertInterface(fileContent, interfaceSnippet string) string {
|
||||
parts := strings.SplitN(fileContent, "var (", 2)
|
||||
|
||||
if len(parts) != 2 {
|
||||
return fileContent
|
||||
}
|
||||
|
||||
return parts[0] + interfaceSnippet + "var (" + parts[1]
|
||||
}
|
||||
Reference in new issue
Block a user