Method: CLI#build

Defined in:
lib/crossplane/cli.rb

#build(filename) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/crossplane/cli.rb', line 61

def build(filename)
	dirname = Dir.pwd unless dirname
	
	# read the json payload from the specified file
	payload = JSON.parse(File.read(filename))
	builder = CrossPlane::Builder.new(
		payload: payload['config'][0]['parsed']
	)	
	
	if not options['force'] and not options['stdout']
		existing = []
		payload['config'].each do |config|
			path = config['file']
			p = Pathname.new(path)
			path = p.absolute? ? path: File.join(dirname, path)
			if File.exist?(path)
				existing.push(path)
			end
		end


		# ask the user if it's okay to overwrite existing files
		if existing.length > 0
			puts(format('building %s would overwrite these files:', filename))
			puts existing.join("\n")
			# if not _prompt_yes():
			#   print('not overwritten')
			#   return
		end
	end

	# if stdout is set then just print each file after another like nginx -T
	#if options['stdout']
		payload['config'].each do |config|
			path = config['file']
			p = Pathname.new(path)
			path = p.absolute? ? path: File.join(dirname, path)
			parsed = config['parsed']
			output = builder.build(
				parsed,
				indent: options['indent'] || 4, # fix default option in config.rb
				tabs: options['tabs'],
				header: options['header']
			)
			output = output.rstrip + "\n"
			puts output
		end
		#puts output
	#end
end