mirror of
				https://github.com/TheLocehiliosan/yadm
				synced 2025-06-13 13:03:58 +00:00 
			
		
		
		
	Add source to templates (#163)
				
					
				
			A new variable is exposed to templates, which holds the filename of the
template source. The primary use case is to be able to include a warning
message within the template. For example:
  # Do not edit. This file auto-generated from {{ yadm.source }}.
			
			
This commit is contained in:
		
							parent
							
								
									0c7aec6dd7
								
							
						
					
					
						commit
						aeb6a54ad7
					
				| @ -106,3 +106,20 @@ def test_template_builtin(runner, yadm, tmpdir): | |||||||
|     assert run.success |     assert run.success | ||||||
|     assert run.err == '' |     assert run.err == '' | ||||||
|     assert output_file.read() == EXPECTED |     assert output_file.read() == EXPECTED | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_source(runner, yadm, tmpdir): | ||||||
|  |     """Test yadm.source""" | ||||||
|  | 
 | ||||||
|  |     input_file = tmpdir.join('input') | ||||||
|  |     input_file.write('{{yadm.source}}', ensure=True) | ||||||
|  |     output_file = tmpdir.join('output') | ||||||
|  | 
 | ||||||
|  |     script = f""" | ||||||
|  |         YADM_TEST=1 source {yadm} | ||||||
|  |         template_builtin "{input_file}" "{output_file}" | ||||||
|  |     """ | ||||||
|  |     run = runner(command=['bash'], inp=script) | ||||||
|  |     assert run.success | ||||||
|  |     assert run.err == '' | ||||||
|  |     assert output_file.read().strip() == str(input_file) | ||||||
|  | |||||||
| @ -97,3 +97,21 @@ def test_template_j2(runner, yadm, tmpdir, processor): | |||||||
|     assert run.success |     assert run.success | ||||||
|     assert run.err == '' |     assert run.err == '' | ||||||
|     assert output_file.read() == EXPECTED |     assert output_file.read() == EXPECTED | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @pytest.mark.parametrize('processor', ('j2cli', 'envtpl')) | ||||||
|  | def test_source(runner, yadm, tmpdir, processor): | ||||||
|  |     """Test YADM_SOURCE""" | ||||||
|  | 
 | ||||||
|  |     input_file = tmpdir.join('input') | ||||||
|  |     input_file.write('{{YADM_SOURCE}}', ensure=True) | ||||||
|  |     output_file = tmpdir.join('output') | ||||||
|  | 
 | ||||||
|  |     script = f""" | ||||||
|  |         YADM_TEST=1 source {yadm} | ||||||
|  |         template_{processor} "{input_file}" "{output_file}" | ||||||
|  |     """ | ||||||
|  |     run = runner(command=['bash'], inp=script) | ||||||
|  |     assert run.success | ||||||
|  |     assert run.err == '' | ||||||
|  |     assert output_file.read().strip() == str(input_file) | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								yadm
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								yadm
									
									
									
									
									
								
							| @ -297,6 +297,7 @@ BEGIN { | |||||||
|   c["hostname"] = host |   c["hostname"] = host | ||||||
|   c["user"]     = user |   c["user"]     = user | ||||||
|   c["distro"]   = distro |   c["distro"]   = distro | ||||||
|  |   c["source"]   = source | ||||||
|   vld           = conditions() |   vld           = conditions() | ||||||
|   ifs           = "^{%" blank "*if" |   ifs           = "^{%" blank "*if" | ||||||
|   els           = "^{%" blank "*else" blank "*%}$" |   els           = "^{%" blank "*else" blank "*%}$" | ||||||
| @ -340,6 +341,7 @@ EOF | |||||||
|     -v host="$local_host" \ |     -v host="$local_host" \ | ||||||
|     -v user="$local_user" \ |     -v user="$local_user" \ | ||||||
|     -v distro="$local_distro" \ |     -v distro="$local_distro" \ | ||||||
|  |     -v source="$input" \ | ||||||
|     "$awk_pgm" \ |     "$awk_pgm" \ | ||||||
|     "$input" > "$output" |     "$input" > "$output" | ||||||
| } | } | ||||||
| @ -353,6 +355,7 @@ function template_j2cli() { | |||||||
|   YADM_HOSTNAME="$local_host" \ |   YADM_HOSTNAME="$local_host" \ | ||||||
|   YADM_USER="$local_user"     \ |   YADM_USER="$local_user"     \ | ||||||
|   YADM_DISTRO="$local_distro" \ |   YADM_DISTRO="$local_distro" \ | ||||||
|  |   YADM_SOURCE="$input"        \ | ||||||
|   "$J2CLI_PROGRAM" "$input" -o "$output" |   "$J2CLI_PROGRAM" "$input" -o "$output" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -365,6 +368,7 @@ function template_envtpl() { | |||||||
|   YADM_HOSTNAME="$local_host" \ |   YADM_HOSTNAME="$local_host" \ | ||||||
|   YADM_USER="$local_user"     \ |   YADM_USER="$local_user"     \ | ||||||
|   YADM_DISTRO="$local_distro" \ |   YADM_DISTRO="$local_distro" \ | ||||||
|  |   YADM_SOURCE="$input"        \ | ||||||
|   "$ENVTPL_PROGRAM" --keep-template "$input" -o "$output" |   "$ENVTPL_PROGRAM" --keep-template "$input" -o "$output" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								yadm.1
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								yadm.1
									
									
									
									
									
								
							| @ -584,6 +584,7 @@ During processing, the following variables are available in the template: | |||||||
|  yadm.hostname   YADM_HOSTNAME   hostname (without domain) |  yadm.hostname   YADM_HOSTNAME   hostname (without domain) | ||||||
|  yadm.os         YADM_OS         uname -s |  yadm.os         YADM_OS         uname -s | ||||||
|  yadm.user       YADM_USER       id -u -n |  yadm.user       YADM_USER       id -u -n | ||||||
|  |  yadm.source     YADM_SOURCE     Template filename | ||||||
| 
 | 
 | ||||||
| Examples: | Examples: | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user