Skip to content
Snippets Groups Projects
Commit 090834e5 authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Added Basic Init

parent 443e4d9e
Branches
No related tags found
No related merge requests found
Pipeline #82020 canceled
build/
test/
ds
\ No newline at end of file
......@@ -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 . .
......
......@@ -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
......
......@@ -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."
}
module datasmith
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
)
......@@ -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 {
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment