diff --git a/.changesets/patch-1788520819013459586.md b/.changesets/patch-1788520819013459586.md new file mode 100644 index 0000000..4828654 --- /dev/null +++ b/.changesets/patch-1788520819013459586.md @@ -0,0 +1 @@ +Migrate description input to chzyer/readline to fix multi-print pasting bugs, add interrupt handling for clean exits, and resolve filepath variable shadowing. diff --git a/changeset/changeset.go b/changeset/changeset.go index e3e597c..659345c 100644 --- a/changeset/changeset.go +++ b/changeset/changeset.go @@ -1,8 +1,9 @@ package changeset import ( - "bufio" + "errors" "fmt" + "io" "os" "path/filepath" "strings" @@ -10,6 +11,7 @@ import ( "github.com/ChanduBobbili/changesetgoo/constants" "github.com/ChanduBobbili/changesetgoo/enums" + "github.com/chzyer/readline" "github.com/manifoldco/promptui" ) @@ -22,6 +24,10 @@ func PromptReleaseType() (enums.ReleaseType, error) { _, result, err := prompt.Run() if err != nil { + // Handle graceful shutdown for Ctrl+C and Ctrl+D + if errors.Is(err, promptui.ErrInterrupt) || errors.Is(err, promptui.ErrEOF) { + os.Exit(0) + } return "", err } @@ -35,11 +41,11 @@ func AddChangeset(releaseType enums.ReleaseType, message string) error { } filename := fmt.Sprintf("%s-%d.md", releaseType, time.Now().UnixNano()) - filepath := filepath.Join(constants.ChangesDir, filename) + filePath := filepath.Join(constants.ChangesDir, filename) - // ✅ Only write the plain description (no headings, no "###") + // Only write the plain description (no headings, no "###") content := strings.TrimSpace(message) + "\n" - return os.WriteFile(filepath, []byte(content), 0644) + return os.WriteFile(filePath, []byte(content), 0644) } // InteractiveAdd allows user to input bump type and description @@ -49,9 +55,22 @@ func InteractiveAdd() error { return err } - fmt.Print("Enter change description: ") - reader := bufio.NewReader(os.Stdin) - desc, _ := reader.ReadString('\n') + // Use readline directly to fix the multi-print pasting bug + rl, err := readline.New("Enter change description: ") + if err != nil { + return err + } + defer rl.Close() + + desc, err := rl.Readline() + if err != nil { + // Handle graceful shutdown for Ctrl+C and Ctrl+D during text input + if errors.Is(err, readline.ErrInterrupt) || errors.Is(err, io.EOF) { + os.Exit(0) + } + return err + } + desc = strings.TrimSpace(desc) if err := AddChangeset(bump, desc); err != nil { diff --git a/go.mod b/go.mod index 41d940c..fb1c149 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,6 @@ go 1.25.1 require github.com/manifoldco/promptui v0.9.0 require ( - github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect - golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect + github.com/chzyer/readline v1.5.1 // indirect + golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect ) diff --git a/go.sum b/go.sum index 34fdb2a..3b2873f 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,18 @@ github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM= +github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI= +github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04= +github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=