-
Notifications
You must be signed in to change notification settings - Fork 21
/
puppet_helper.rb
108 lines (86 loc) · 3.33 KB
/
puppet_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# frozen_string_literal: true
require 'pathname'
require 'tempfile'
require 'puppet-languageserver/session_state/object_cache'
require 'puppet-languageserver/global_queues'
module PuppetLanguageServer
module PuppetHelper
def self.module_path
return @module_path unless @module_path.nil?
# TODO: It would be nice if this wasn't using the whole puppet environment to calculate the modulepath directoties
# In the meantime memoize it. Currently you can't change the modulepath mid-process.
begin
env = Puppet.lookup(:environments).get!(Puppet.settings[:environment])
rescue Puppet::Environments::EnvironmentNotFound, StandardError
env = Puppet.lookup(:current_environment)
end
return [] if env.nil?
@module_path = env.modulepath
end
# Node Graph
def self.get_node_graph(session_state, content, local_workspace)
with_temporary_file(content) do |filepath|
ap = PuppetLanguageServer::Sidecar::Protocol::ActionParams.new
ap['source'] = filepath
args = ["--action-parameters=#{ap.to_json}"]
args << "--local-workspace=#{local_workspace}" unless local_workspace.nil?
sidecar_queue.execute('node_graph', args, false, session_state.connection_id)
end
end
def self.get_puppet_resource(session_state, typename, title, local_workspace)
ap = PuppetLanguageServer::Sidecar::Protocol::ActionParams.new
ap['typename'] = typename
ap['title'] = title unless title.nil?
args = ["--action-parameters=#{ap.to_json}"]
args << "--local-workspace=#{local_workspace}" unless local_workspace.nil?
sidecar_queue.execute('resource_list', args, false, session_state.connection_id)
end
def self.get_type(session_state, name)
session_state.object_cache.object_by_name(:type, name)
end
def self.type_names(session_state)
session_state.object_cache.object_names_by_section(:type).map(&:to_s)
end
def self.function(session_state, name, tasks_mode = false)
exclude_origins = tasks_mode ? [] : [:bolt]
session_state.object_cache.object_by_name(
:function,
name,
fuzzy_match: true,
exclude_origins: exclude_origins
)
end
def self.function_names(session_state, tasks_mode = false)
exclude_origins = tasks_mode ? [] : [:bolt]
session_state.object_cache.object_names_by_section(:function, exclude_origins: exclude_origins).map(&:to_s)
end
def self.get_class(session_state, name)
session_state.object_cache.object_by_name(:class, name)
end
def self.class_names(session_state)
session_state.object_cache.object_names_by_section(:class).map(&:to_s)
end
def self.datatype(session_state, name, tasks_mode = false)
exclude_origins = tasks_mode ? [] : [:bolt]
session_state.object_cache.object_by_name(
:datatype,
name,
fuzzy_match: true,
exclude_origins: exclude_origins
)
end
def self.sidecar_queue
PuppetLanguageServer::GlobalQueues.sidecar_queue
end
def self.with_temporary_file(content)
tempfile = Tempfile.new('langserver-sidecar')
tempfile.open
tempfile.write(content)
tempfile.close
yield tempfile.path
ensure
tempfile.delete if tempfile
end
private_class_method :with_temporary_file
end
end