Skip to content

Commit

Permalink
chore: improve watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Feb 17, 2024
1 parent 25ace6d commit 009f56e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
13 changes: 4 additions & 9 deletions cmd/kod/internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package internal

import (
"fmt"
"log"
"time"

"github.com/fsnotify/fsnotify"
"github.com/samber/lo"
"github.com/spf13/cobra"
)

Expand All @@ -19,15 +19,10 @@ var generate = &cobra.Command{
{
if watch, _ := cmd.Flags().GetBool("watch"); watch {
// Create new watcher.
w, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
w := lo.Must(fsnotify.NewWatcher())
defer w.Close()

Watch(&watcher{w: w}, ".", func() {
doGenerate(cmd, ".", args)
})
Watch(&watcher{w: w}, ".", func() { doGenerate(cmd, ".", args) })
}

doGenerate(cmd, ".", args)
Expand All @@ -38,7 +33,7 @@ var generate = &cobra.Command{
func doGenerate(cmd *cobra.Command, dir string, args []string) {
startTime := time.Now()

if s2i := cmd.Flag("struct2interface").Changed; s2i {
if s2i, _ := cmd.Flags().GetBool("struct2interface"); s2i {
err := Struct2Interface(cmd, ".")
if err != nil {
fmt.Println(err)
Expand Down
4 changes: 4 additions & 0 deletions cmd/kod/internal/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ import (
func TestGenerate(t *testing.T) {
execute("generate github.com/go-kod/kod/tests/graphcase/...")
}

func TestGenerateWithStruct2Interface(t *testing.T) {
execute("generate -s github.com/go-kod/kod/tests/graphcase/...")
}
4 changes: 2 additions & 2 deletions tests/case1/kod_gen_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 009f56e

Please sign in to comment.