Skip to content

Commit 823bc75

Browse files
committed
Update edit command to support file existence check and append option
1 parent 13fd144 commit 823bc75

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

cmd/edit.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ and usage of using your command. For example:
2727
Cobra is a CLI library for Go that empowers applications.
2828
This application is a tool to generate the needed files
2929
to quickly create a Cobra application.`,
30-
Aliases: []string{"r"},
30+
Aliases: []string{"e"},
3131
PreRun: func(cmd *cobra.Command, args []string) {
3232
if verbose {
3333
log.SetLevel(l.DebugLevel)
@@ -47,6 +47,22 @@ to quickly create a Cobra application.`,
4747

4848
fileName := args[0]
4949

50+
// check if the file exists
51+
exists, err := utils.FileExists(fileName)
52+
if err != nil {
53+
log.Errorf("Error checking if file exists: %s", err)
54+
os.Exit(1)
55+
}
56+
57+
if !exists {
58+
// create a new empty file
59+
err = os.WriteFile(fileName, []byte(""), 0644)
60+
if err != nil {
61+
log.Errorf("Error creating new file: %s", err)
62+
os.Exit(1)
63+
}
64+
}
65+
5066
// load the file
5167
contents, err := utils.LoadFile(fileName)
5268
if err != nil {
@@ -98,8 +114,13 @@ to quickly create a Cobra application.`,
98114
os.Exit(1)
99115
}
100116

117+
confirmMsg := "Would you like to overwrite the file with the new code? (y/N): "
118+
if appendFile {
119+
confirmMsg = "Would you like to append the new code to the file? (y/N): "
120+
}
121+
101122
if !force {
102-
confirm, err := utils.Input("Would you like to overwrite the file with the new code? (y/N): ")
123+
confirm, err := utils.Input(confirmMsg)
103124
if err != nil {
104125
log.Errorf("Error getting input: %s", err)
105126
os.Exit(1)
@@ -111,11 +132,17 @@ to quickly create a Cobra application.`,
111132
}
112133
}
113134

114-
// write the new code to the file
115-
finalCode, err := textfile.ReplaceLines(contents, startLine, endLine, newCode)
116-
if err != nil {
117-
log.Errorf("Error replacing lines: %s", err)
118-
os.Exit(1)
135+
var finalCode string
136+
if appendFile {
137+
// add the new code to the end of the file
138+
finalCode = contents + "\n" + newCode + "\n"
139+
} else {
140+
// write the new code to the file
141+
finalCode, err = textfile.ReplaceLines(contents, startLine, endLine, newCode)
142+
if err != nil {
143+
log.Errorf("Error replacing lines: %s", err)
144+
os.Exit(1)
145+
}
119146
}
120147

121148
err = utils.WriteFile(fileName, finalCode)
@@ -132,6 +159,7 @@ func init() {
132159

133160
editCmd.Flags().BoolVarP(&force, "force", "f", false, "Force overwrite of existing files")
134161
editCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
162+
editCmd.Flags().BoolVarP(&appendFile, "append", "a", false, "Append to the end of a file instead of overwriting it")
135163
editCmd.Flags().IntVarP(&startLine, "start", "s", 1, "Start line")
136164
editCmd.Flags().IntVarP(&endLine, "end", "e", 0, "End line")
137165
editCmd.Flags().StringVarP(&chatPrompt, "goal", "g", "", "Goal of the edit")

cmd/vars.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var countFinalTokens bool
4242

4343
var startLine int
4444
var endLine int
45+
var appendFile bool
4546

4647
var log = l.NewWithOptions(os.Stderr, l.Options{
4748
Level: l.InfoLevel,

pkg/utils/misc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package utils
2+
3+
import "os"
4+
5+
func FileExists(filePath string) (bool, error) {
6+
_, err := os.Stat(filePath)
7+
if err == nil {
8+
return true, nil
9+
}
10+
if os.IsNotExist(err) {
11+
return false, nil
12+
}
13+
return false, err
14+
}

0 commit comments

Comments
 (0)