Coverage for src / cli / clone.py: 85%

25 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 16:36 +0000

1import sys 

2 

3import cloup 

4 

5from .. import answers, jinja, layout, questions, template 

6from .cli import cli 

7 

8 

9def _validate_template_file(_ctx, _param, value): 

10 if value.startswith("/"): 

11 sys.exit(f'error: template filename "{value}" must be relative path to template directory') 

12 return value 

13 

14 

15# 

16# mk-scaffold clone [OPTIONS] TEMPLATE 

17# 

18@cli.command(help="Clone and templatize a repository.") 

19@cloup.option( 

20 "-b", 

21 "--branch", 

22 metavar="BRANCH", 

23 default=None, 

24 show_default=False, 

25 help="Checkout git BRANCH of git repository", 

26) 

27@cloup.option( 

28 "--filename", 

29 "template_filename", 

30 metavar="FILENAME", 

31 default="scaffold.yml", 

32 show_default=True, 

33 callback=_validate_template_file, 

34 help="Filename of the scaffold file to use.", 

35) 

36@cloup.option( 

37 "-i", 

38 "--input-file", 

39 "answer_file", 

40 metavar="FILE", 

41 type=cloup.Path(exists=True, dir_okay=False, readable=True), 

42 help="Location of a yaml input file, usually named '.answers.yml', with answers to the questions.", 

43) 

44@cloup.option( 

45 "-o", 

46 "--output-dir", 

47 metavar="DIR", 

48 default=".", 

49 type=cloup.Path(file_okay=False, dir_okay=True), 

50 show_default=False, 

51 help="Where to output the generated files. [default: current directory]", 

52) 

53@cloup.argument( 

54 "template_path", 

55 nargs=1, 

56 metavar="TEMPLATE", 

57 type=cloup.Path(), 

58 help="Directory or git repository that contains 'scaffold.yml' template file", 

59) 

60def clone(**kwargs): 

61 # Load the answers with a default jinja 

62 env, ctx = jinja.create() 

63 

64 answer = answers.load(kwargs.get("answer_file")) 

65 answer = answers.validate(answer) 

66 ctx = answers.process(answer, env, ctx) 

67 

68 tpl = template.Template(**kwargs) 

69 tpl = template.find(tpl) 

70 tpl = template.load(tpl) 

71 

72 tpl, env, ctx = questions.prompt(tpl, ctx) 

73 

74 tpl = layout.clone(tpl, env, ctx, **kwargs) 

75 tpl = layout.process(tpl, env, ctx, **kwargs)