File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -61,23 +61,8 @@ See ochami-config(5) for details on the configuration options.`,
6161 fileToModify = config .UserConfigFile
6262 }
6363
64- // Ask to create file if it doesn't exist
65- if create , err := ios .askToCreate (fileToModify ); err != nil {
66- if err != FileExistsError {
67- log .Logger .Error ().Err (err ).Msg ("error asking to create file" )
68- logHelpError (cmd )
69- os .Exit (1 )
70- }
71- } else if create {
72- if err := createIfNotExists (fileToModify ); err != nil {
73- log .Logger .Error ().Err (err ).Msg ("error creating file" )
74- logHelpError (cmd )
75- os .Exit (1 )
76- }
77- } else {
78- log .Logger .Error ().Msg ("user declined to create file, not modifying" )
79- os .Exit (0 )
80- }
64+ // Handle file creation based on --create flag
65+ handleFileCreation (cmd , fileToModify )
8166
8267 // Perform modification
8368 dflt , err := cmd .Flags ().GetBool ("default" )
Original file line number Diff line number Diff line change @@ -58,23 +58,8 @@ See ochami-config(5) for details on the configuration options.`,
5858 os .Exit (1 )
5959 }
6060
61- // Ask to create file if it doesn't exist.
62- if create , err := ios .askToCreate (fileToModify ); err != nil {
63- if err != FileExistsError {
64- log .Logger .Error ().Err (err ).Msg ("error asking to create file" )
65- logHelpError (cmd )
66- os .Exit (1 )
67- }
68- } else if create {
69- if err := createIfNotExists (fileToModify ); err != nil {
70- log .Logger .Error ().Err (err ).Msg ("error creating file" )
71- logHelpError (cmd )
72- os .Exit (1 )
73- }
74- } else {
75- log .Logger .Error ().Msg ("user declined to create file, not modifying" )
76- os .Exit (0 )
77- }
61+ // Handle file creation based on --create flag
62+ handleFileCreation (cmd , fileToModify )
7863
7964 // Perform modification
8065 if err := config .ModifyConfig (fileToModify , args [0 ], args [1 ]); err != nil {
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ See ochami-config(5) for details on the configuration options.`,
3939func init () {
4040 configCmd .PersistentFlags ().Bool ("system" , false , "modify system config" )
4141 configCmd .PersistentFlags ().Bool ("user" , true , "modify user config" )
42+ configCmd .PersistentFlags ().Bool ("create" , false , "create config file and parent directories if they do not exist" )
4243
4344 rootCmd .AddCommand (configCmd )
4445}
Original file line number Diff line number Diff line change @@ -216,6 +216,44 @@ func createIfNotExists(path string) error {
216216 return nil
217217}
218218
219+ // handleFileCreation checks if a file should be created based on the --create flag.
220+ // If --create is set, it automatically creates the file. If not set, it prompts the user.
221+ func handleFileCreation (cmd * cobra.Command , fileToModify string ) {
222+ createFlag , err := cmd .Flags ().GetBool ("create" )
223+ if err != nil {
224+ log .Logger .Error ().Err (err ).Msg ("failed to retrieve \" create\" flag" )
225+ logHelpError (cmd )
226+ os .Exit (1 )
227+ }
228+
229+ if createFlag {
230+ // Automatically create file if --create flag is set
231+ if err := createIfNotExists (fileToModify ); err != nil {
232+ log .Logger .Error ().Err (err ).Msg ("error creating file" )
233+ logHelpError (cmd )
234+ os .Exit (1 )
235+ }
236+ } else {
237+ // Use existing askToCreate logic when --create flag is not set
238+ if create , err := ios .askToCreate (fileToModify ); err != nil {
239+ if err != FileExistsError {
240+ log .Logger .Error ().Err (err ).Msg ("error asking to create file" )
241+ logHelpError (cmd )
242+ os .Exit (1 )
243+ }
244+ } else if create {
245+ if err := createIfNotExists (fileToModify ); err != nil {
246+ log .Logger .Error ().Err (err ).Msg ("error creating file" )
247+ logHelpError (cmd )
248+ os .Exit (1 )
249+ }
250+ } else {
251+ log .Logger .Error ().Msg ("user declined to create file, not modifying" )
252+ os .Exit (0 )
253+ }
254+ }
255+ }
256+
219257// checkToken takes a pointer to a Cobra command and checks to see if --token
220258// was set. If not, an error is printed and the program exits.
221259func checkToken (cmd * cobra.Command ) {
You can’t perform that action at this time.
0 commit comments