Skip to content

Commit

Permalink
Merge pull request #7 from ffelipelimao/feature/added-pointer-array-des
Browse files Browse the repository at this point in the history
added destionation folder and pointer array fix
  • Loading branch information
ffelipelimao authored Nov 22, 2022
2 parents e70eba6 + d080e1a commit e3326a6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generate builders for Go

[![GoDoc](https://pkg.go.dev/badge/github.com/ffelipelimao/gobuilder)](https://pkg.go.dev/github.com/ffelipelimao/gobuilder)

gobuilder is a library for create files,
at this moment we are creating only from the design pattern Builder.

Expand All @@ -22,19 +24,41 @@ gobuilder gen -n=Test -f=Foo-string,Bar-string

##### Command Line syntax flag

```go
-n // create a name to your mock builder struct ex: -n=Test
```
```go
-f /* create a name the fields to your struct
- **-n** : Create a name to your mock builder struct ex: -n=Test

- **-f** : Create a name the fields to your struct
the fields are separated by "," for you to define a type for your field, you separate it by "-"
ex: -f=Foo-string,Bar-string
in this case Foo is the field and string is the type of this field
*/

- **-d** : Create the destination folder, he need start with ./


## Examples

```bash
gobuilder gen -n=Test -f=Foo-string,Bar-string
```

With Pointer: use letter p

```bash
gobuilder gen -n=Test -f=Foo-pstring,Bar-string
```

With Array: use letter a

```bash
gobuilder gen -n=Test -f=Foo-astring,Bar-string
```

## Help commands

```bash
gobuilder -h
```
or

##### Help command
```bash
gobuilder gen -h
```
Expand Down
31 changes: 26 additions & 5 deletions cmd/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/format"
"log"
"os"
"path/filepath"
"strings"
"text/template"

Expand All @@ -19,7 +20,7 @@ type Object struct {
Fields map[string]string
}

func Start(name string, fields string) error {
func Start(name string, fields string, destination string) error {
if name == "" {
return errors.New("missing -n flag value")
}
Expand All @@ -30,7 +31,8 @@ func Start(name string, fields string) error {
}

filename := strings.ToLower(fmt.Sprintf("%s.go", object.Name))
generate(tmpls.MockBuilderTmpl, filename, object)

createFile(tmpls.MockBuilderTmpl, filename, object, destination)

return nil
}
Expand All @@ -47,7 +49,8 @@ func createObject(name string, fields string) (Object, error) {
}
key := fieldsClovenByType[0]
value := fieldsClovenByType[1]
fieldsMapper[key] = value
v := checkSymbols(value)
fieldsMapper[key] = v
}

return Object{
Expand All @@ -56,7 +59,17 @@ func createObject(name string, fields string) (Object, error) {
}, nil
}

func generate(MockBuilderTmpl string, outputFile string, data Object) {
func checkSymbols(value string) string {
if strings.HasPrefix(value, "p") {
return strings.Replace(value, "p", "*", -1)
}
if strings.HasPrefix(value, "a") {
return strings.Replace(value, "a", "[]", -1)
}
return value
}

func createFile(MockBuilderTmpl string, outputFile string, data Object, destination string) {
tmpl := template.Must(template.New("").
Parse(MockBuilderTmpl))

Expand All @@ -72,7 +85,15 @@ func generate(MockBuilderTmpl string, outputFile string, data Object) {
}

fmt.Println("Writing file: ", outputFile)
f, _ := os.Create(outputFile)

if _, err := os.Stat(destination); os.IsNotExist(err) {
os.MkdirAll(destination, 0700)
}

path := filepath.Join(destination, outputFile)
newFilePath := filepath.FromSlash(path)
f, _ := os.Create(newFilePath)

w := bufio.NewWriter(f)
w.WriteString(string(formatted))
w.Flush()
Expand Down
4 changes: 3 additions & 1 deletion cmd/ports/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var name string
var fields string
var destination string

// genCmd represents the gen command
var genCmd = &cobra.Command{
Expand All @@ -23,7 +24,7 @@ var genCmd = &cobra.Command{
"If you have any doubt see README.md to see examples",

Run: func(cmd *cobra.Command, args []string) {
err := gen.Start(name, fields)
err := gen.Start(name, fields, destination)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Println("if you have any doubt see README.md to see examples")
Expand All @@ -37,4 +38,5 @@ func init() {

genCmd.Flags().StringVarP(&name, "name", "n", "", "mockbuilder name")
genCmd.Flags().StringVarP(&fields, "fields", "f", "", "fields separated by comma with types separated by -")
genCmd.Flags().StringVarP(&destination, "destination", "d", "", "the destination folder")
}
10 changes: 7 additions & 3 deletions cmd/ports/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import (
"github.com/spf13/cobra"
)

const version = "0.0.8"

var rootCmd = &cobra.Command{
Use: "gobuilder",
Short: "gobuilder is a file generator",
Use: "gobuilder",
Short: "gobuilder is a file generator",
Version: version,
Long: `gobuilder is a CLI library for Go that empowers applications.
This application is a tool to generate files quickly.`,
}

func Execute() {
rootCmd.CompletionOptions.DisableDefaultCmd = true

err := rootCmd.Execute()
if err != nil {

os.Exit(1)
}
}

0 comments on commit e3326a6

Please sign in to comment.