diff --git a/.gitignore b/.gitignore index 8937b727b2db0b45417aaa5686b6c61948e8b3f8..b9124448cdd168c946273844dd5cf6b3ca121a39 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ +test/ ds \ No newline at end of file diff --git a/Containerfile b/Containerfile index 21deae1e91a2367a911c9874c5d17ad71163f0f6..4d6102b7dc9cf7efc0bea32fad366c49673085b0 100644 --- a/Containerfile +++ b/Containerfile @@ -6,10 +6,10 @@ WORKDIR /app # Copy the Go Modules manifests COPY go.mod ./ -#COPY go.sum ./ +COPY go.sum ./ # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed -# RUN go mod download +RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . diff --git a/commands/add.go b/commands/add.go index 8a215790e60d7c13768c7bf36496ab34d616106e..4ae7c3d2b9612ecf76caa83577672e144930b10b 100644 --- a/commands/add.go +++ b/commands/add.go @@ -2,12 +2,6 @@ package commands import ( "fmt" - "os" - "path/filepath" - "time" - - "datasmith/config" - "datasmith/templates" "datasmith/utils" ) @@ -21,7 +15,7 @@ func addTable(name string) { // TODO: Do Stuff - fmt.Printf("Added new table '%s' to the project\n", name) + fmt.Printf("Added new table '%s' to the project\n", slug) } // Help returns the help information for the add command diff --git a/commands/init.go b/commands/init.go index 68697724925f8b6b7bb73a085410703db049c42d..eb5319dbff7168453f9c38d695afbd5c7aea3ad4 100644 --- a/commands/init.go +++ b/commands/init.go @@ -2,24 +2,28 @@ package commands import ( "fmt" - "os" "os/exec" + "os" "path/filepath" - "time" - - "datasmith/config" - "datasmith/templates" "datasmith/utils" ) -func Init(name string) { - - +func InitProject(name string, dbType string) { utils.PromptIfEmpty(&name, "Enter the name of the project: ") slug := utils.Slugify(name) + if dbType == "" { + dbType = promptForDbType() + } + + fmt.Printf("Initializing new project structure in '%s' with database type '%s'\n", slug, dbType) + + // Create project directories + createProjectDirs(slug) + // TODO: copy base files + fmt.Printf("Initialized new project structure in '%s'\n", slug) fmt.Println("Initializing Git repository...") @@ -28,7 +32,36 @@ func Init(name string) { } +// promptForDbType prompts the user to select a database type +func promptForDbType() string { + options := []utils.MenuOption{ + {Display: "MySQL (default)", Value: "mysql"}, + {Display: "PostgreSQL", Value: "postgres"}, + } + + dbType, err := utils.SelectMenu(options, "Select database type:") + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + return dbType +} + +// createProjectDirs creates the necessary project directories +func createProjectDirs(projectName string) { + dirs := []string{"k8s", "sql"} + + for _, dir := range dirs { + path := filepath.Join(projectName, dir) + if err := os.MkdirAll(path, os.ModePerm); err != nil { + fmt.Printf("Error creating directory '%s': %v\n", path, err) + } else { + fmt.Printf("Created directory: %s\n", path) + } + } +} + // Help returns the help information for the init command func InitHelp() string { - return "init [name] : Create a new project with the specified name." + return "init [name] [--type mysql|postgres] : Create a new project with the specified name and database type." } diff --git a/go.mod b/go.mod index 9f81aca22b8f5c7ded5ba0ca7528b6fe6aaba184..a261b1e60e445e99ee91bb386c75e3cd8608a0e5 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module datasmith -go 1.22.4 \ No newline at end of file +go 1.22.4 + +require ( + github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect + github.com/manifoldco/promptui v0.9.0 // indirect + golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..66c56875ee8841f1dc7e16f232012c331f2a856e --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= +github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 2393c2dd620e5912da51607be7df48435beb3fd8..9968a73bff53c74a24bed7a235810f74585136a3 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strings" "datasmith/commands" "datasmith/config" ) @@ -21,9 +22,16 @@ func main() { switch command { case "init": - var name string + var name, dbType string args := os.Args[2:] - commands.InitProject(name, args) + for i, arg := range args { + if arg == "--type" && i+1 < len(args) { + dbType = args[i+1] + } else if !strings.HasPrefix(arg, "--") { + name = arg + } + } + commands.InitProject(name, dbType) fmt.Println("Done. Now run 'ds add table' to add a table to the database.") case "add": if len(os.Args) < 3 { diff --git a/utils/utils.go b/utils/utils.go index 193b7e83e1a0f1b6bb84d2381c11119044d7d670..fbb43f3b2a1e712c4e4c2316b2e070e32028df94 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,14 +3,20 @@ package utils import ( "bufio" "fmt" - "io" "os" "os/exec" - "path/filepath" + "os/signal" "regexp" "strings" + "github.com/manifoldco/promptui" ) +// MenuOption represents an option in the menu +type MenuOption struct { + Display string + Value string +} + // CheckGit checks if Git is installed and available in PATH func CheckGit() bool { _, err := exec.LookPath("git") @@ -35,5 +41,40 @@ func PromptIfEmpty(value *string, prompt string) { input, _ := reader.ReadString('\n') *value = strings.TrimSpace(input) } +} + +// SelectMenu displays a menu with options and returns the selected value +func SelectMenu(options []MenuOption, prompt string) (string, error) { + // Handle interrupt signal (Ctrl+C) + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + go func() { + <-c + fmt.Println("\nAborted by user") + os.Exit(1) + }() + + promptItems := make([]string, len(options)) + for i, option := range options { + promptItems[i] = option.Display + } + + promptUI := promptui.Select{ + Label: prompt, + Items: promptItems, + } + + _, result, err := promptUI.Run() + if err != nil { + return "", err + } + + for _, option := range options { + if option.Display == result { + return option.Value, nil + } + } + return "", nil } \ No newline at end of file