Skip to content

Commit

Permalink
Merge pull request #20 from lucasres/feature/output-file
Browse files Browse the repository at this point in the history
Add output base e output file
  • Loading branch information
lucasres authored Oct 4, 2021
2 parents d35da38 + 670e385 commit c6451a0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
25 changes: 24 additions & 1 deletion cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (

"github.com/lucasres/adr-gen/internal/engine"
"github.com/lucasres/adr-gen/internal/file"
"github.com/lucasres/adr-gen/internal/output"
"github.com/lucasres/adr-gen/pkg/helpers"
"github.com/spf13/cobra"
)

const (
analyzeFlagTimeout = "timeout"
analyzeFlagPath = "path"
outputFlagPath = "output"
)

func NewAnalyzeCommand() *cobra.Command {
Expand All @@ -26,6 +28,7 @@ func NewAnalyzeCommand() *cobra.Command {
// Add as flags que o comando tem
cmd.Flags().IntP(analyzeFlagTimeout, "t", 30, "Set timeout of process")
cmd.Flags().StringP(analyzeFlagPath, "p", "", "Specify the path to be analyzed")
cmd.Flags().StringP(outputFlagPath, "o", "", "Specify the path to be generate docs")

return cmd
}
Expand All @@ -37,12 +40,21 @@ func runAnalyze(cmd *cobra.Command, args []string) {
path, err := cmd.Flags().GetString(analyzeFlagPath)
helpers.PrintAndExitIfGetFlagReturnError(analyzeFlagPath, err)

output, err := cmd.Flags().GetString(outputFlagPath)
helpers.PrintAndExitIfGetFlagReturnError(outputFlagPath, err)

if len(path) < 1 {
helpers.PrintErrorAndExit(
fmt.Errorf("the path to be analyzed is invalid, please specify a valid path with \"--path\" flag"),
)
}

if len(output) < 1 {
helpers.PrintErrorAndExit(
fmt.Errorf("the output path is invalid, please specify a valid path with \"--output\" flag"),
)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

Expand Down Expand Up @@ -77,9 +89,15 @@ func runAnalyze(cmd *cobra.Command, args []string) {
go func(fileContents []byte) {
defer wg.Done()
e := getAnalyzeEngine()
if err := e.Run(fileContents); err != nil {
hits, err := e.Run(fileContents)

o := getOutput()
o.Write(output, hits)

if err != nil {
errChanel <- err
}

}(content)
}

Expand Down Expand Up @@ -114,3 +132,8 @@ func getAnalyzeEngine() engine.Engine {
// @todo: Create Engine based in some configuration
return engine.NewSengine()
}

func getOutput() output.OutputBase {
// @todo: Create Outup based in some configuration
return output.NewFileOutput()
}
3 changes: 1 addition & 2 deletions internal/engine/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ type Engine interface {
PreProcess(content []byte) ([]byte, error)
Parse([]byte) (*ContentParsed, error)
Analize(content ContentParsed) (map[string]ADRHit, error)
Output(hits map[string]ADRHit) error
Run(content []byte) error
Run(content []byte) (map[string]ADRHit, error)
}
21 changes: 5 additions & 16 deletions internal/engine/sengine.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package engine

import (
"errors"
"fmt"
"regexp"
"strings"
)
Expand Down Expand Up @@ -43,35 +41,26 @@ func (e *Sengine) PreProcess(content []byte) ([]byte, error) {
return content, nil
}

func (e *Sengine) Output(hits map[string]ADRHit) error {
return nil
}

func (e *Sengine) Run(content []byte) error {
func (e *Sengine) Run(content []byte) (map[string]ADRHit, error) {
processed, err := e.PreProcess(content)

if err != nil {
return err
return nil, err
}

parsed, err := e.Parse(processed)

if err != nil {
return err
return nil, err
}

hits, err := e.Analize(*parsed)

if err != nil {
return err
}

for a, b := range hits {
fmt.Println(a)
fmt.Println(b.Description, b.ID)
return nil, err
}

return errors.New("must be not implemented")
return hits, nil
}

func NewSengine() *Sengine {
Expand Down
23 changes: 23 additions & 0 deletions internal/file/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package file

import "io/ioutil"

type Writer interface {
Write(path, content string) error
}

type LocalWriter struct{}

func (w *LocalWriter) Write(path, content string) error {
err := ioutil.WriteFile(path, []byte(content), 0644)

if err != nil {
return err
}

return nil
}

func NewLocalWrite() *LocalWriter {
return &LocalWriter{}
}
7 changes: 7 additions & 0 deletions internal/output/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package output

import "github.com/lucasres/adr-gen/internal/engine"

type OutputBase interface {
Write(path string, hits map[string]engine.ADRHit) error
}
26 changes: 26 additions & 0 deletions internal/output/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package output

import (
"github.com/lucasres/adr-gen/internal/engine"
"github.com/lucasres/adr-gen/internal/file"
)

type FileOutput struct{}

func (o *FileOutput) Write(path string, hits map[string]engine.ADRHit) error {
w := file.NewLocalWrite()

for _, hit := range hits {
err := w.Write(path+hit.ID, hit.Description)

if err != nil {
return err
}
}

return nil
}

func NewFileOutput() *FileOutput {
return &FileOutput{}
}

0 comments on commit c6451a0

Please sign in to comment.