@@ -27,7 +27,7 @@ and usage of using your command. For example:
27
27
Cobra is a CLI library for Go that empowers applications.
28
28
This application is a tool to generate the needed files
29
29
to quickly create a Cobra application.` ,
30
- Aliases : []string {"r " },
30
+ Aliases : []string {"e " },
31
31
PreRun : func (cmd * cobra.Command , args []string ) {
32
32
if verbose {
33
33
log .SetLevel (l .DebugLevel )
@@ -47,6 +47,22 @@ to quickly create a Cobra application.`,
47
47
48
48
fileName := args [0 ]
49
49
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
+
50
66
// load the file
51
67
contents , err := utils .LoadFile (fileName )
52
68
if err != nil {
@@ -98,8 +114,13 @@ to quickly create a Cobra application.`,
98
114
os .Exit (1 )
99
115
}
100
116
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
+
101
122
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 )
103
124
if err != nil {
104
125
log .Errorf ("Error getting input: %s" , err )
105
126
os .Exit (1 )
@@ -111,11 +132,17 @@ to quickly create a Cobra application.`,
111
132
}
112
133
}
113
134
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
+ }
119
146
}
120
147
121
148
err = utils .WriteFile (fileName , finalCode )
@@ -132,6 +159,7 @@ func init() {
132
159
133
160
editCmd .Flags ().BoolVarP (& force , "force" , "f" , false , "Force overwrite of existing files" )
134
161
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" )
135
163
editCmd .Flags ().IntVarP (& startLine , "start" , "s" , 1 , "Start line" )
136
164
editCmd .Flags ().IntVarP (& endLine , "end" , "e" , 0 , "End line" )
137
165
editCmd .Flags ().StringVarP (& chatPrompt , "goal" , "g" , "" , "Goal of the edit" )
0 commit comments