@@ -100,11 +100,8 @@ def search_command(terms: List[str]) -> int:
100100)
101101from cfbs .index import _VERSION_INDEX , Index
102102from cfbs .git import (
103- git_exists ,
103+ git_configure_and_initialize ,
104104 is_git_repo ,
105- git_get_config ,
106- git_set_config ,
107- git_init ,
108105 CFBSGitError ,
109106 ls_remote ,
110107)
@@ -169,11 +166,16 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int:
169166
170167
171168@cfbs_command ("init" )
172- def init_command (index = None , masterfiles = None , non_interactive = False ) -> int :
169+ def init_command (
170+ index = None ,
171+ masterfiles = None ,
172+ non_interactive = False ,
173+ use_git : Union [bool , None ] = None ,
174+ ) -> int :
173175 if is_cfbs_repo ():
174176 raise CFBSUserError ("Already initialized - look at %s" % cfbs_filename ())
175177
176- name = prompt_user (
178+ project_name = prompt_user (
177179 non_interactive ,
178180 "Please enter the name of this CFEngine Build project" ,
179181 default = "Example project" ,
@@ -186,7 +188,7 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
186188
187189 config = OrderedDict (
188190 {
189- "name" : name ,
191+ "name" : project_name ,
190192 "type" : "policy-set" , # TODO: Prompt whether user wants to make a module
191193 "description" : description ,
192194 "build" : [],
@@ -195,81 +197,47 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
195197 if index :
196198 config ["index" ] = index
197199
198- do_git = get_args ().git
199- is_git = is_git_repo ()
200- if do_git is None :
201- if is_git :
202- do_git = prompt_user_yesno (
200+ if use_git is None :
201+ if is_git_repo ():
202+ use_git = prompt_user_yesno (
203203 non_interactive ,
204204 "This is a git repository. Do you want cfbs to make commits to it?" ,
205205 )
206206 else :
207- do_git = prompt_user_yesno (
207+ use_git = prompt_user_yesno (
208208 non_interactive ,
209209 "Do you want cfbs to initialize a git repository and make commits to it?" ,
210210 )
211- else :
212- assert do_git in ("yes" , "no" )
213- do_git = True if do_git == "yes" else False
214-
215- if do_git is True :
216- if not git_exists ():
217- print ("Command 'git' was not found" )
218- return 1
219211
212+ if use_git is True :
220213 user_name = get_args ().git_user_name
221- if not user_name :
222- user_name = git_get_config ("user.name" )
223- user_name = prompt_user (
224- non_interactive ,
225- "Please enter user name to use for git commits" ,
226- default = user_name or "cfbs" ,
227- )
228-
229214 user_email = get_args ().git_user_email
230- if not user_email :
231- user_email = git_get_config ("user.email" )
232- node_name = os .uname ().nodename
233- user_email = prompt_user (
234- non_interactive ,
235- "Please enter user email to use for git commits" ,
236- default = user_email or ("cfbs@%s" % node_name ),
237- )
238-
239- if not is_git :
240- try :
241- git_init (user_name , user_email , description )
242- except CFBSGitError as e :
243- print (str (e ))
244- return 1
245- else :
246- if not git_set_config ("user.name" , user_name ) or not git_set_config (
247- "user.email" , user_email
248- ):
249- print ("Failed to set Git user name and email" )
250- return 1
215+ git_configure_and_initialize (
216+ user_name , user_email , non_interactive , description
217+ )
251218
252- config ["git" ] = do_git
219+ config ["git" ] = use_git
253220
254221 data = pretty (config , CFBS_DEFAULT_SORTING_RULES ) + "\n "
255222 with open (cfbs_filename (), "w" ) as f :
256223 f .write (data )
257224 assert is_cfbs_repo ()
258225
259- if do_git :
226+ if use_git :
260227 try :
261228 git_commit_maybe_prompt (
262229 "Initialized a new CFEngine Build project" ,
263230 non_interactive ,
264231 [cfbs_filename ()],
265232 )
266- except CFBSGitError as e :
267- print (str (e ))
233+ except CFBSGitError :
268234 os .unlink (cfbs_filename ())
269- return 1
235+ raise
270236
271237 print (
272- "Initialized an empty project called '{}' in '{}'" .format (name , cfbs_filename ())
238+ "Initialized an empty project called '{}' in '{}'" .format (
239+ project_name , cfbs_filename ()
240+ )
273241 )
274242
275243 """
0 commit comments