Class: CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/crossplane/cli.rb

Instance Method Summary collapse

Instance Method Details

#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

#lex(filename) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/crossplane/cli.rb', line 114

def lex(filename)
	payload = CrossPlane::Lexer.new(
		filename: filename,
	).lex()
	lex = (not options['line_numbers'].nil? and options['line_numbers'] == true) ? payload : payload.map{|e| e[0]}

	if options['out']
		File.open(options['out'], 'w') do |f|
			f.write(JSON.pretty_generate(lex))
		end
	else
		puts options['pretty'] ? JSON.pretty_generate(lex) : lex.to_json
	end
end

#minify(filename) ⇒ Object



130
131
132
133
# File 'lib/crossplane/cli.rb', line 130

def minify(filename)
	puts 'minifiy'
	exit
end

#parse(filename) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/crossplane/cli.rb', line 38

def parse(filename)
	payload = CrossPlane::Parser.new(
		filename: filename,
		combine: options['combine'] || false,
		strict: options['strict'] || false,
		catch_errors: options['no_catch'] ? false : true,
		comments: options['include_comments'] || false,
		ignore: options['ignore'] ? options['ignore'].split(/\s*,\s*/) : [],
		single: options['single'] || false,
	).parse()
	
	if options['out']
		File.open(options['out'], 'w') do |f|
			f.write(JSON.pretty_generate(payload))
		end
	else
		puts options['pretty'] ? JSON.pretty_generate(payload) : payload.to_json
	end
	exit 0
end