From dc80296d5bdcd3836a7fb66caed52ddd3302666d Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sat, 3 Jun 2023 15:16:20 +0200 Subject: [PATCH] feat(parameters): output parameter to put the new maze in a file --- Generators/global.go | 6 ++++++ README.md | 3 +++ main.go | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/Generators/global.go b/Generators/global.go index 209d641..5a74f85 100644 --- a/Generators/global.go +++ b/Generators/global.go @@ -2,6 +2,7 @@ package Generators import ( "fmt" + "os" ) type Maze struct { @@ -206,6 +207,11 @@ func (s *Scheme) GenerateText() string { return f } +func (m *Maze) Output(path string) error { + scheme := m.ToScheme() + return os.WriteFile(path, []byte(scheme.GenerateText()), 0664) +} + // mergeCells merge two cells and their group // // big is the cell with the biggest group. diff --git a/README.md b/README.md index 7b215ec..8b48d4d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ This application takes two parameters: - `-h uint` - Set the height of the maze - `-w uint` - Set the width of the maze +It has also another parameter: +- `-o string` - Set the output file (if it does not give, no output in file) + ## Technologies - Go 1.20 diff --git a/main.go b/main.go index 5ed0c2a..f51c322 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/msmp-core/maze-generator-cli/Generators" "os" "regexp" @@ -22,8 +23,10 @@ func main() { } widthO := regexp.MustCompile(`-w [0-9]+`) height0 := regexp.MustCompile(`-h [0-9]+`) + output0 := regexp.MustCompile(`-o [0-9a-zA-Z/.\-_]+`) unWidth := widthO.FindString(cli) unHeight := height0.FindString(cli) + unOutput := output0.FindString(cli) if unHeight == "" || unWidth == "" { help() return @@ -44,6 +47,14 @@ func main() { panic(err) } m.RenderWalls() + if unOutput != "" { + output := strings.ReplaceAll(unOutput, "-o ", "") + err = m.Output(output) + if err != nil { + panic(err) + } + fmt.Printf("Successfully outputted at %s\n", output) + } } func help() {