Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ Usage:
ghokin [command]

Available Commands:
check Check a file/folder is well formatted
fmt Format a feature file/folder
check Check files/folders are well formatted
completion Generate the autocompletion script for the specified shell
fmt Format stdin or feature files/folders
help Help about any command
version App version

Flags:
--config string config file
Expand All @@ -55,30 +57,30 @@ cat features/test.feature|ghokin fmt stdout

### fmt replace

Format and replace a file or all files in a directory
Format and replace files or all files in one or several directory

```
ghokin fmt replace features/test.feature
ghokin fmt replace features/test-1.feature features/test-2.feature
```

or

```
ghokin fmt replace features/
ghokin fmt replace features-1/ features-2/
```

### check

Ensure a file or all files in a directory are well formatted, exit with an error code otherwise
Ensure files or all files in one or several directory are well formatted, exit with an error code otherwise

```
ghokin check features/test.feature
ghokin check features/test-1.feature features/test-2.feature
```

or

```
ghokin check features/
ghokin check features-1/ features-2/
```

## Documentation
Expand Down
16 changes: 9 additions & 7 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import (
)

var checkCmd = &cobra.Command{
Use: "check [file or folder path]",
Short: "Check a file/folder is well formatted",
Long: "Check a file/folder is well formatted, otherwise it exit with an error code and the list of file badly formatted",
Use: "check [files or folders path]",
Short: "Check files/folders are well formatted",
Long: "Check files/folders are well formatted, otherwise it exit with an error code and the list of file badly formatted",
Run: setupCmdFunc(check),
}

func check(msgHandler messageHandler, cmd *cobra.Command, args []string) {
if len(args) != 1 {
msgHandler.errorFatalStr("you must provide a filename or a folder as argument")
if len(args) == 0 {
msgHandler.errorFatalStr("you must provide filenames or folders as argument")
}

if errs := getFileManager().Check(args[0], extensions); len(errs) > 0 {
if errs := getFileManager().Check(args, extensions); len(errs) > 0 {
for _, e := range errs {
msgHandler.error(e)
}

msgHandler.exit(1)
}

msgHandler.success(`"%s" is well formatted`, args[0])
for _, a := range args {
msgHandler.success(`"%s" is well formatted`, a)
}
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestCheckErrors(t *testing.T) {
scenarios := []scenario{
{
[]string{},
"you must provide a filename or a folder as argument\n",
"you must provide filenames or folders as argument\n",
},
{
[]string{"fixtures/whatever.feature"},
Expand Down
2 changes: 1 addition & 1 deletion cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var fmtCmd = &cobra.Command{
Use: "fmt",
Short: "Format stdin or a feature file/folder",
Short: "Format stdin or feature files/folders",
Run: setupCmdFunc(format),
}

Expand Down
14 changes: 8 additions & 6 deletions cmd/fmt_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import (
var extensions []string

var fmtReplaceCmd = &cobra.Command{
Use: "replace [file or folder path]",
Short: "Format and replace a file or a pool of files in folder",
Use: "replace [files or folders path]",
Short: "Format and replace files",
Run: setupCmdFunc(formatAndReplace),
}

func formatAndReplace(msgHandler messageHandler, cmd *cobra.Command, args []string) {
if len(args) != 1 {
msgHandler.errorFatalStr("you must provide a filename or a folder as argument")
if len(args) == 0 {
msgHandler.errorFatalStr("you must provide filenames or folders as argument")
}

if errs := getFileManager().TransformAndReplace(args[0], extensions); len(errs) > 0 {
if errs := getFileManager().TransformAndReplace(args, extensions); len(errs) > 0 {
for _, e := range errs {
msgHandler.error(e)
}

msgHandler.exit(1)
}

msgHandler.success(`"%s" formatted`, args[0])
for _, a := range args {
msgHandler.success(`"%s" formatted`, a)
}
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/fmt_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestFormatAndReplaceWithErrors(t *testing.T) {
scenarios := []scenario{
{
[]string{},
"you must provide a filename or a folder as argument\n",
"you must provide filenames or folders as argument\n",
},
{
[]string{"fixtures/whatever.feature"},
Expand Down
38 changes: 20 additions & 18 deletions ghokin/file_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,34 @@ func (f FileManager) Transform(filename string) ([]byte, error) {

// TransformAndReplace formats and applies shell commands on file or folder
// and replace the content of files
func (f FileManager) TransformAndReplace(path string, extensions []string) []error {
return f.process(path, extensions, replaceFileWithContent)
func (f FileManager) TransformAndReplace(paths []string, extensions []string) []error {
return f.process(paths, extensions, replaceFileWithContent)
}

// Check ensures file or folder is well formatted
func (f FileManager) Check(path string, extensions []string) []error {
return f.process(path, extensions, check)
func (f FileManager) Check(paths []string, extensions []string) []error {
return f.process(paths, extensions, check)
}

func (f FileManager) process(path string, extensions []string, processFile func(file string, content []byte) error) []error {
func (f FileManager) process(paths []string, extensions []string, processFile func(file string, content []byte) error) []error {
errors := []error{}
fi, err := os.Stat(path)
if err != nil {
return append(errors, err)
}

switch mode := fi.Mode(); {
case mode.IsDir():
errors = append(errors, f.processPath(path, extensions, processFile)...)
case mode.IsRegular():
b, err := f.Transform(path)
for _, path := range paths {
fi, err := os.Stat(path)
if err != nil {
return append(errors, err)
}
if err := processFile(path, b); err != nil {
errors = append(errors, err)

switch mode := fi.Mode(); {
case mode.IsDir():
errors = append(errors, f.processPath(path, extensions, processFile)...)
case mode.IsRegular():
b, err := f.Transform(path)
if err != nil {
return append(errors, err)
}
if err := processFile(path, b); err != nil {
errors = append(errors, err)
}
}
}
return errors
Expand All @@ -124,7 +126,7 @@ func (f FileManager) processPath(path string, extensions []string, processFile f
return []error{}
}

for i := 0; i < 10; i++ {
for range 10 {
wg.Add(1)

go func() {
Expand Down
Loading
Loading