From 8d0ded903be3b72714040bcd752a232312b7ab43 Mon Sep 17 00:00:00 2001 From: Dax Date: Sat, 14 Dec 2024 17:36:26 +0530 Subject: [PATCH 1/3] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a3965d..926b3e9 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ make build To set up the initial project structure, run: ```bash -static setup +cd 1 +static setup < > ``` This command will create the following directory structure: From 38ffe51367a233ff1deede4e293ecec14f9ab1d3 Mon Sep 17 00:00:00 2001 From: Dax Date: Sun, 15 Dec 2024 16:12:57 +0530 Subject: [PATCH 2/3] Update main.go In this update, the add command was modified to allow adding multiple pages in a loop, making it easier to add several pages at once. Additionally, the help command was implemented, providing users with a clear overview of all available commands in the static tool. The setup command was enhanced to prompt users interactively for the project name, description, and author during setup. A loading bar with a tick box was added to visually indicate the progress during the project setup. Furthermore, the user interaction was improved with clear prompts and feedback messages, and input validations were added for fields such as project name, description, and author to ensure correct inputs. --- cmd/main.go | 179 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 160 insertions(+), 19 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index eb8e1e8..029a2e6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,18 +1,115 @@ package main import ( - "1/1/functions/config" - "1/1/functions/setup" - "fmt" - "net/http" - "os" - "path/filepath" - "sync" - - "github.com/fsnotify/fsnotify" - "github.com/gorilla/websocket" + "bufio" // For reading user input + "fmt" // For formatted I/O operations + "net/http" // For HTTP server functionality + "os" // For file and environment operations + "os/exec" + "path/filepath" // For handling file paths + "strings" // For string manipulations + "sync" // For synchronizing concurrent operations + "time" + + "github.com/fsnotify/fsnotify" // For file system notifications + "github.com/gorilla/websocket" // For WebSocket handling + + "1/1/functions/config" // Custom configuration handling + "1/1/functions/setup" // Custom project setup utilities +) + +const ( + Reset = "\033[0m" + Red = "\033[31m" + Green = "\033[32m" + Yellow = "\033[33m" ) +// handling the add command +func handleAddCommand() { + if len(os.Args) < 3 { + fmt.Println("Please provide a page name: static add ") + return + } + pageName := os.Args[2] + + // Validate page name + if strings.ContainsAny(pageName, " -./\\<>:\"|?*") || strings.HasSuffix(pageName, ".exe") { + fmt.Println(Red + "Error: Invalid page name. Use only alphanumeric characters and avoid special symbols or '.exe' extensions." + Reset) + return + } + + fmt.Printf(Yellow+"You are about to add a page named '%s'. Confirm (y/n): "+Reset, pageName) + reader := bufio.NewReader(os.Stdin) + confirmation, _ := reader.ReadString('\n') + confirmation = strings.TrimSpace(strings.ToLower(confirmation)) + + // Validate confirmation input + if confirmation != "y" && confirmation != "n" { + fmt.Println(Red + "Invalid input. Please type 'y' for yes or 'n' for no." + Reset) + return + } + + if confirmation == "n" { + fmt.Println(Yellow + "Page addition canceled." + Reset) + return + } + + // Add the page + setup.AddPage(pageName) + fmt.Println(Green + "Page created successfully. Running compile command..." + Reset) + + // Automatically run the compile command + compileCmd := exec.Command(".\\static.exe", "compile") + compileCmd.Stdout = os.Stdout + compileCmd.Stderr = os.Stderr + + err := compileCmd.Run() + if err != nil { + fmt.Println(Red+"Error running compile command:"+Reset, err) + } else { + fmt.Println(Green + "Compile command executed successfully!" + Reset) + } +} + +// loading bar component +func greenRectangularLoadingBar(duration time.Duration) { + green := "\033[32m" // ANSI code for green + reset := "\033[0m" // ANSI code to reset color + + totalLength := 30 // Length of the progress bar (number of blocks) + + // Print the start of the progress bar with the message + fmt.Print("Setting up the project: [") + + // Display empty part of the progress bar (filled with spaces for now) + for i := 0; i < totalLength; i++ { + fmt.Print(" ") + } + fmt.Print("]") + + // Simulate the progress and update the progress bar + for i := 0; i <= totalLength; i++ { + fmt.Print("\rSetting up the project: [") + // Print the filled portion (green blocks) + for j := 0; j < i; j++ { + fmt.Print(green + "█" + reset) + } + // Print the remaining empty portion + for j := i; j < totalLength; j++ { + fmt.Print(" ") + } + fmt.Print("]") + + // Adjust the speed of the progress bar filling + time.Sleep(duration / time.Duration(totalLength)) + } + + // After the bar is filled, print a completion message + fmt.Println("\nProject setup complete!") +} + +// main function func main() { if len(os.Args) < 2 { fmt.Println("Usage: static [setup|add ]") @@ -24,28 +121,61 @@ func main() { command := os.Args[1] switch command { case "setup": - if len(os.Args) < 3 { - fmt.Println("Please provide the project name: static setup ") + reader := bufio.NewReader(os.Stdin) + + // Prompt for project name + fmt.Print("Project name: ") + projectName, _ := reader.ReadString('\n') + projectName = strings.TrimSpace(projectName) + + // Validate project name + if strings.Contains(projectName, " ") { + fmt.Println("Error: Project name cannot contain spaces.") return } - projectName := os.Args[2] - setup.SetupProject(projectName, staticjson) - case "add": - if len(os.Args) < 3 { - fmt.Println("Please provide a page name: static add ") + if len(projectName) > 100 { + fmt.Println("Error: Project name cannot exceed 100 characters.") return } - pageName := os.Args[2] - setup.AddPage(pageName) + + // Prompt for description + fmt.Print(Yellow + "Description: " + Reset) + description, _ := reader.ReadString('\n') + description = strings.TrimSpace(description) + + // Prompt for author name + fmt.Print(Yellow + "Author: " + Reset) + author, _ := reader.ReadString('\n') + author = strings.TrimSpace(author) + + // Simulate loading bar + greenRectangularLoadingBar(2 * time.Second) + + // Output details + fmt.Printf(Green + "\nProject setup complete:\n" + Reset) + fmt.Printf(Green+"Project name: %s\nDescription: %s\nAuthor: %s\n"+Reset, projectName, description, author) + + // Call the setup function + setup.SetupProject(projectName, staticjson) + + case "add": + handleAddCommand() + case "compile": setup.CompileProject() + case "watch": watchProject() + + case "help": + showHelp() + default: fmt.Println("Unknown command. Use 'setup' or 'add '.") } } +// watch command to host the project func watchProject() { // WebSocket upgrader from the Gorilla WebSocket package. var upgrader = websocket.Upgrader{ @@ -173,3 +303,14 @@ func watchProject() { // The server will block here, so no need for a select{} or similar. } + +// Show .help text +func showHelp() { + fmt.Println("Usage: static [command] [options]") + fmt.Println("\nCommands:") + fmt.Println(" setup Configure your project interactively. It will guide you through the setup process.") + fmt.Println(" add Add one or more pages to the project in a loop.") + fmt.Println(" compile Compile the entire project for production.") + fmt.Println(" watch Start a development server and watch for file changes.") + fmt.Println(" help Show this help message and list all available commands.") +} From f34ff22c7f82765579d7421e8a231304e8b78ca1 Mon Sep 17 00:00:00 2001 From: Dax Date: Thu, 19 Dec 2024 15:42:56 +0530 Subject: [PATCH 3/3] BUILD cmd folder created --- cmd/build_cmd/add.go | 67 ++++++++++++++++++++++++++++++++++++++++ cmd/build_cmd/compile.go | 1 + cmd/build_cmd/help.go | 1 + cmd/build_cmd/setup.go | 1 + cmd/build_cmd/watch.go | 1 + 5 files changed, 71 insertions(+) create mode 100644 cmd/build_cmd/add.go create mode 100644 cmd/build_cmd/compile.go create mode 100644 cmd/build_cmd/help.go create mode 100644 cmd/build_cmd/setup.go create mode 100644 cmd/build_cmd/watch.go diff --git a/cmd/build_cmd/add.go b/cmd/build_cmd/add.go new file mode 100644 index 0000000..fac6b26 --- /dev/null +++ b/cmd/build_cmd/add.go @@ -0,0 +1,67 @@ +package buildcmd + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "strings" +) + +const ( + Reset = "\033[0m" + Red = "\033[31m" + Green = "\033[32m" + Yellow = "\033[33m" +) + +// HandleAddCommand adds a new page to the project. +func HandleAddCommand() { + if len(os.Args) < 3 { + fmt.Println("Please provide a page name: static add ") + return + } + pageName := os.Args[2] + + // Validate page name + if strings.ContainsAny(pageName, " -./\\<>:\"|?*") || strings.HasSuffix(pageName, ".exe") { + fmt.Println(Red + "Error: Invalid page name. Use only alphanumeric characters and avoid special symbols or '.exe' extensions." + Reset) + return + } + + fmt.Printf(Yellow+"You are about to add a page named '%s'. Confirm (y/n): "+Reset, pageName) + reader := bufio.NewReader(os.Stdin) + confirmation, _ := reader.ReadString('\n') + confirmation = strings.TrimSpace(strings.ToLower(confirmation)) + + // Validate confirmation input + if confirmation != "y" && confirmation != "n" { + fmt.Println(Red + "Invalid input. Please type 'y' for yes or 'n' for no." + Reset) + return + } + + if confirmation == "n" { + fmt.Println(Yellow + "Page addition canceled." + Reset) + return + } + + // Add the page + err := os.WriteFile(pageName+".html", []byte("

"+pageName+"

"), 0644) + if err != nil { + fmt.Println(Red+"Error creating page file:"+Reset, err) + return + } + fmt.Println(Green + "Page created successfully. Running compile command..." + Reset) + + // Automatically run the compile command + compileCmd := exec.Command(".\\static.exe", "compile") + compileCmd.Stdout = os.Stdout + compileCmd.Stderr = os.Stderr + + err = compileCmd.Run() + if err != nil { + fmt.Println(Red+"Error running compile command:"+Reset, err) + } else { + fmt.Println(Green + "Compile command executed successfully!" + Reset) + } +} diff --git a/cmd/build_cmd/compile.go b/cmd/build_cmd/compile.go new file mode 100644 index 0000000..184ee30 --- /dev/null +++ b/cmd/build_cmd/compile.go @@ -0,0 +1 @@ +package buildcmd diff --git a/cmd/build_cmd/help.go b/cmd/build_cmd/help.go new file mode 100644 index 0000000..184ee30 --- /dev/null +++ b/cmd/build_cmd/help.go @@ -0,0 +1 @@ +package buildcmd diff --git a/cmd/build_cmd/setup.go b/cmd/build_cmd/setup.go new file mode 100644 index 0000000..184ee30 --- /dev/null +++ b/cmd/build_cmd/setup.go @@ -0,0 +1 @@ +package buildcmd diff --git a/cmd/build_cmd/watch.go b/cmd/build_cmd/watch.go new file mode 100644 index 0000000..184ee30 --- /dev/null +++ b/cmd/build_cmd/watch.go @@ -0,0 +1 @@ +package buildcmd