gloo-lang 0.9.0 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/lib/VERSION +1 -1
  3. data/lib/dependencies.rb +2 -2
  4. data/lib/{gloo_lang.rb → gloo-lang.rb} +7 -9
  5. data/lib/gloo_lang/app/args.rb +112 -0
  6. data/lib/gloo_lang/app/engine.rb +230 -0
  7. data/lib/gloo_lang/app/help.rb +156 -0
  8. data/lib/gloo_lang/app/log.rb +81 -0
  9. data/lib/gloo_lang/app/mode.rb +27 -0
  10. data/lib/gloo_lang/app/settings.rb +186 -0
  11. data/lib/gloo_lang/convert/converter.rb +35 -0
  12. data/lib/gloo_lang/convert/string_to_datetime.rb +21 -0
  13. data/lib/gloo_lang/convert/string_to_decimal.rb +20 -0
  14. data/lib/gloo_lang/convert/string_to_integer.rb +20 -0
  15. data/lib/gloo_lang/core/baseo.rb +30 -0
  16. data/lib/gloo_lang/core/dictionary.rb +181 -0
  17. data/lib/gloo_lang/core/error.rb +61 -0
  18. data/lib/gloo_lang/core/event_manager.rb +44 -0
  19. data/lib/gloo_lang/core/factory.rb +210 -0
  20. data/lib/gloo_lang/core/gloo_system.rb +266 -0
  21. data/lib/gloo_lang/core/heap.rb +52 -0
  22. data/lib/gloo_lang/core/here.rb +36 -0
  23. data/lib/gloo_lang/core/it.rb +36 -0
  24. data/lib/gloo_lang/core/literal.rb +30 -0
  25. data/lib/gloo_lang/core/obj.rb +303 -0
  26. data/lib/gloo_lang/core/obj_finder.rb +30 -0
  27. data/lib/gloo_lang/core/op.rb +40 -0
  28. data/lib/gloo_lang/core/parser.rb +59 -0
  29. data/lib/gloo_lang/core/pn.rb +188 -0
  30. data/lib/gloo_lang/core/tokens.rb +165 -0
  31. data/lib/gloo_lang/core/verb.rb +86 -0
  32. data/lib/gloo_lang/exec/action.rb +48 -0
  33. data/lib/gloo_lang/exec/dispatch.rb +40 -0
  34. data/lib/gloo_lang/exec/exec_env.rb +74 -0
  35. data/lib/gloo_lang/exec/runner.rb +45 -0
  36. data/lib/gloo_lang/exec/script.rb +49 -0
  37. data/lib/gloo_lang/exec/stack.rb +78 -0
  38. data/lib/gloo_lang/expr/expression.rb +118 -0
  39. data/lib/gloo_lang/expr/l_boolean.rb +36 -0
  40. data/lib/gloo_lang/expr/l_decimal.rb +39 -0
  41. data/lib/gloo_lang/expr/l_integer.rb +37 -0
  42. data/lib/gloo_lang/expr/l_string.rb +58 -0
  43. data/lib/gloo_lang/expr/op_div.rb +22 -0
  44. data/lib/gloo_lang/expr/op_minus.rb +22 -0
  45. data/lib/gloo_lang/expr/op_mult.rb +22 -0
  46. data/lib/gloo_lang/expr/op_plus.rb +24 -0
  47. data/lib/gloo_lang/help/app/application.txt +22 -0
  48. data/lib/gloo_lang/help/app/configuration.txt +7 -0
  49. data/lib/gloo_lang/help/app/default_help.txt +14 -0
  50. data/lib/gloo_lang/help/app/logging.txt +16 -0
  51. data/lib/gloo_lang/help/core/color.txt +31 -0
  52. data/lib/gloo_lang/help/core/error.txt +33 -0
  53. data/lib/gloo_lang/help/core/events.txt +21 -0
  54. data/lib/gloo_lang/help/core/gloo_system.txt +57 -0
  55. data/lib/gloo_lang/help/core/here.txt +30 -0
  56. data/lib/gloo_lang/help/core/it.txt +23 -0
  57. data/lib/gloo_lang/help/core/ops.txt +16 -0
  58. data/lib/gloo_lang/help/core/pathname.txt +29 -0
  59. data/lib/gloo_lang/help/objs/basic/alias.txt +36 -0
  60. data/lib/gloo_lang/help/objs/basic/boolean.txt +28 -0
  61. data/lib/gloo_lang/help/objs/basic/container.txt +33 -0
  62. data/lib/gloo_lang/help/objs/basic/decimal.txt +28 -0
  63. data/lib/gloo_lang/help/objs/basic/integer.txt +27 -0
  64. data/lib/gloo_lang/help/objs/basic/script.txt +29 -0
  65. data/lib/gloo_lang/help/objs/basic/string.txt +28 -0
  66. data/lib/gloo_lang/help/objs/basic/text.txt +27 -0
  67. data/lib/gloo_lang/help/objs/basic/untyped.txt +22 -0
  68. data/lib/gloo_lang/help/objs/cli/banner.txt +49 -0
  69. data/lib/gloo_lang/help/objs/cli/bar.txt +37 -0
  70. data/lib/gloo_lang/help/objs/cli/colorize.txt +33 -0
  71. data/lib/gloo_lang/help/objs/cli/confirm.txt +26 -0
  72. data/lib/gloo_lang/help/objs/cli/menu.txt +44 -0
  73. data/lib/gloo_lang/help/objs/cli/menu_item.txt +26 -0
  74. data/lib/gloo_lang/help/objs/cli/pastel.txt +43 -0
  75. data/lib/gloo_lang/help/objs/cli/prompt.txt +27 -0
  76. data/lib/gloo_lang/help/objs/cli/select.txt +34 -0
  77. data/lib/gloo_lang/help/objs/ctrl/each.txt +48 -0
  78. data/lib/gloo_lang/help/objs/ctrl/repeat.txt +38 -0
  79. data/lib/gloo_lang/help/objs/data/markdown.txt +25 -0
  80. data/lib/gloo_lang/help/objs/data/mysql.txt +40 -0
  81. data/lib/gloo_lang/help/objs/data/query.txt +37 -0
  82. data/lib/gloo_lang/help/objs/data/sqlite.txt +26 -0
  83. data/lib/gloo_lang/help/objs/data/table.txt +46 -0
  84. data/lib/gloo_lang/help/objs/dev/git_repo.txt +36 -0
  85. data/lib/gloo_lang/help/objs/dev/stats.txt +36 -0
  86. data/lib/gloo_lang/help/objs/dt/date.txt +23 -0
  87. data/lib/gloo_lang/help/objs/dt/datetime.txt +24 -0
  88. data/lib/gloo_lang/help/objs/dt/time.txt +23 -0
  89. data/lib/gloo_lang/help/objs/ror/erb.txt +37 -0
  90. data/lib/gloo_lang/help/objs/ror/eval.txt +24 -0
  91. data/lib/gloo_lang/help/objs/snd/play.txt +23 -0
  92. data/lib/gloo_lang/help/objs/snd/say.txt +28 -0
  93. data/lib/gloo_lang/help/objs/system/file.txt +48 -0
  94. data/lib/gloo_lang/help/objs/system/ssh_exec.txt +30 -0
  95. data/lib/gloo_lang/help/objs/system/system.txt +32 -0
  96. data/lib/gloo_lang/help/objs/web/http_get.txt +35 -0
  97. data/lib/gloo_lang/help/objs/web/http_post.txt +34 -0
  98. data/lib/gloo_lang/help/objs/web/json.txt +34 -0
  99. data/lib/gloo_lang/help/objs/web/slack.txt +33 -0
  100. data/lib/gloo_lang/help/objs/web/teams.txt +30 -0
  101. data/lib/gloo_lang/help/objs/web/uri.txt +38 -0
  102. data/lib/gloo_lang/help/verbs/alert.txt +33 -0
  103. data/lib/gloo_lang/help/verbs/beep.txt +25 -0
  104. data/lib/gloo_lang/help/verbs/cls.txt +24 -0
  105. data/lib/gloo_lang/help/verbs/context.txt +43 -0
  106. data/lib/gloo_lang/help/verbs/create.txt +33 -0
  107. data/lib/gloo_lang/help/verbs/execute.txt +27 -0
  108. data/lib/gloo_lang/help/verbs/help.txt +34 -0
  109. data/lib/gloo_lang/help/verbs/if.txt +37 -0
  110. data/lib/gloo_lang/help/verbs/list.txt +34 -0
  111. data/lib/gloo_lang/help/verbs/load.txt +38 -0
  112. data/lib/gloo_lang/help/verbs/move.txt +42 -0
  113. data/lib/gloo_lang/help/verbs/put.txt +38 -0
  114. data/lib/gloo_lang/help/verbs/quit.txt +25 -0
  115. data/lib/gloo_lang/help/verbs/run.txt +41 -0
  116. data/lib/gloo_lang/help/verbs/save.txt +26 -0
  117. data/lib/gloo_lang/help/verbs/show.txt +30 -0
  118. data/lib/gloo_lang/help/verbs/tell.txt +34 -0
  119. data/lib/gloo_lang/help/verbs/unless.txt +38 -0
  120. data/lib/gloo_lang/help/verbs/version.txt +32 -0
  121. data/lib/gloo_lang/help/verbs/wait.txt +29 -0
  122. data/lib/gloo_lang/objs/basic/alias.rb +78 -0
  123. data/lib/gloo_lang/objs/basic/boolean.rb +120 -0
  124. data/lib/gloo_lang/objs/basic/container.rb +76 -0
  125. data/lib/gloo_lang/objs/basic/decimal.rb +76 -0
  126. data/lib/gloo_lang/objs/basic/integer.rb +73 -0
  127. data/lib/gloo_lang/objs/basic/script.rb +99 -0
  128. data/lib/gloo_lang/objs/basic/string.rb +77 -0
  129. data/lib/gloo_lang/objs/basic/text.rb +87 -0
  130. data/lib/gloo_lang/objs/basic/untyped.rb +41 -0
  131. data/lib/gloo_lang/objs/cli/banner.rb +108 -0
  132. data/lib/gloo_lang/objs/cli/bar.rb +133 -0
  133. data/lib/gloo_lang/objs/cli/colorize.rb +73 -0
  134. data/lib/gloo_lang/objs/cli/confirm.rb +96 -0
  135. data/lib/gloo_lang/objs/cli/menu.rb +206 -0
  136. data/lib/gloo_lang/objs/cli/menu_item.rb +95 -0
  137. data/lib/gloo_lang/objs/cli/pastel.rb +97 -0
  138. data/lib/gloo_lang/objs/cli/prompt.rb +110 -0
  139. data/lib/gloo_lang/objs/cli/select.rb +126 -0
  140. data/lib/gloo_lang/objs/ctrl/each.rb +279 -0
  141. data/lib/gloo_lang/objs/ctrl/repeat.rb +108 -0
  142. data/lib/gloo_lang/objs/data/markdown.rb +84 -0
  143. data/lib/gloo_lang/objs/data/mysql.rb +192 -0
  144. data/lib/gloo_lang/objs/data/query.rb +176 -0
  145. data/lib/gloo_lang/objs/data/sqlite.rb +159 -0
  146. data/lib/gloo_lang/objs/data/table.rb +140 -0
  147. data/lib/gloo_lang/objs/dev/git.rb +140 -0
  148. data/lib/gloo_lang/objs/dev/stats.rb +120 -0
  149. data/lib/gloo_lang/objs/dt/date.rb +50 -0
  150. data/lib/gloo_lang/objs/dt/datetime.rb +62 -0
  151. data/lib/gloo_lang/objs/dt/time.rb +50 -0
  152. data/lib/gloo_lang/objs/ror/erb.rb +116 -0
  153. data/lib/gloo_lang/objs/ror/eval.rb +107 -0
  154. data/lib/gloo_lang/objs/snd/play.rb +48 -0
  155. data/lib/gloo_lang/objs/snd/say.rb +98 -0
  156. data/lib/gloo_lang/objs/system/file_handle.rb +138 -0
  157. data/lib/gloo_lang/objs/system/ssh_exec.rb +126 -0
  158. data/lib/gloo_lang/objs/system/system.rb +136 -0
  159. data/lib/gloo_lang/objs/web/http_get.rb +159 -0
  160. data/lib/gloo_lang/objs/web/http_post.rb +183 -0
  161. data/lib/gloo_lang/objs/web/json.rb +135 -0
  162. data/lib/gloo_lang/objs/web/slack.rb +130 -0
  163. data/lib/gloo_lang/objs/web/teams.rb +117 -0
  164. data/lib/gloo_lang/objs/web/uri.rb +148 -0
  165. data/lib/gloo_lang/persist/file_loader.rb +191 -0
  166. data/lib/gloo_lang/persist/file_saver.rb +49 -0
  167. data/lib/gloo_lang/persist/file_storage.rb +45 -0
  168. data/lib/gloo_lang/persist/line_splitter.rb +81 -0
  169. data/lib/gloo_lang/persist/persist_man.rb +120 -0
  170. data/lib/gloo_lang/utils/format.rb +21 -0
  171. data/lib/gloo_lang/utils/stats.rb +205 -0
  172. data/lib/gloo_lang/utils/words.rb +19 -0
  173. data/lib/gloo_lang/verbs/alert.rb +79 -0
  174. data/lib/gloo_lang/verbs/beep.rb +40 -0
  175. data/lib/gloo_lang/verbs/cls.rb +37 -0
  176. data/lib/gloo_lang/verbs/context.rb +62 -0
  177. data/lib/gloo_lang/verbs/create.rb +68 -0
  178. data/lib/gloo_lang/verbs/execute.rb +56 -0
  179. data/lib/gloo_lang/verbs/help.rb +264 -0
  180. data/lib/gloo_lang/verbs/if.rb +92 -0
  181. data/lib/gloo_lang/verbs/list.rb +98 -0
  182. data/lib/gloo_lang/verbs/load.rb +45 -0
  183. data/lib/gloo_lang/verbs/move.rb +89 -0
  184. data/lib/gloo_lang/verbs/put.rb +94 -0
  185. data/lib/gloo_lang/verbs/quit.rb +40 -0
  186. data/lib/gloo_lang/verbs/run.rb +75 -0
  187. data/lib/gloo_lang/verbs/save.rb +39 -0
  188. data/lib/gloo_lang/verbs/show.rb +64 -0
  189. data/lib/gloo_lang/verbs/tell.rb +79 -0
  190. data/lib/gloo_lang/verbs/unless.rb +92 -0
  191. data/lib/gloo_lang/verbs/version.rb +37 -0
  192. data/lib/gloo_lang/verbs/wait.rb +42 -0
  193. metadata +191 -3
@@ -0,0 +1,108 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A looping construct...do something x times.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class Repeat < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'repeat'.freeze
12
+ KEYWORD_SHORT = 'repeat'.freeze
13
+ TIMES = 'times'.freeze
14
+ INDEX = 'index'.freeze
15
+ DO = 'do'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the URI from the child object.
33
+ # Returns nil if there is none.
34
+ #
35
+ def times
36
+ o = find_child TIMES
37
+ return o ? o.value : 0
38
+ end
39
+
40
+ #
41
+ # Run the do script once.
42
+ #
43
+ def run_do
44
+ o = find_child DO
45
+ return unless o
46
+
47
+ GlooLang::Exec::Dispatch.message 'run', o
48
+ end
49
+
50
+ #
51
+ # Set the index of the current iteration.
52
+ #
53
+ def set_index( index )
54
+ o = find_child INDEX
55
+ return unless o
56
+
57
+ o.set_value index
58
+ end
59
+
60
+ # ---------------------------------------------------------------------
61
+ # Children
62
+ # ---------------------------------------------------------------------
63
+
64
+ #
65
+ # Does this object have children to add when an object
66
+ # is created in interactive mode?
67
+ # This does not apply during obj load, etc.
68
+ #
69
+ def add_children_on_create?
70
+ return true
71
+ end
72
+
73
+ #
74
+ # Add children to this object.
75
+ # This is used by containers to add children needed
76
+ # for default configurations.
77
+ #
78
+ def add_default_children
79
+ fac = $engine.factory
80
+ fac.create_int TIMES, 0, self
81
+ fac.create_int INDEX, 0, self
82
+ fac.create_script DO, '', self
83
+ end
84
+
85
+ # ---------------------------------------------------------------------
86
+ # Messages
87
+ # ---------------------------------------------------------------------
88
+
89
+ #
90
+ # Get a list of message names that this object receives.
91
+ #
92
+ def self.messages
93
+ return super + [ 'run' ]
94
+ end
95
+
96
+ #
97
+ # Run the repeat loop.
98
+ #
99
+ def msg_run
100
+ times.times do |index|
101
+ set_index index
102
+ run_do
103
+ end
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,84 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # Markdown data.
5
+ #
6
+ require 'tty-markdown'
7
+ require 'tty-pager'
8
+
9
+ module GlooLang
10
+ module Objs
11
+ class Markdown < GlooLang::Core::Obj
12
+
13
+ KEYWORD = 'markdown'.freeze
14
+ KEYWORD_SHORT = 'md'.freeze
15
+
16
+ #
17
+ # The name of the object type.
18
+ #
19
+ def self.typename
20
+ return KEYWORD
21
+ end
22
+
23
+ #
24
+ # The short name of the object type.
25
+ #
26
+ def self.short_typename
27
+ return KEYWORD_SHORT
28
+ end
29
+
30
+ #
31
+ # Set the value with any necessary type conversions.
32
+ #
33
+ def set_value( new_value )
34
+ self.value = new_value.to_s
35
+ end
36
+
37
+ #
38
+ # Does this object support multi-line values?
39
+ # Initially only true for scripts.
40
+ #
41
+ def multiline_value?
42
+ return false
43
+ end
44
+
45
+ #
46
+ # Get the number of lines of text.
47
+ #
48
+ def line_count
49
+ return value.split( "\n" ).count
50
+ end
51
+
52
+ # ---------------------------------------------------------------------
53
+ # Messages
54
+ # ---------------------------------------------------------------------
55
+
56
+ #
57
+ # Get a list of message names that this object receives.
58
+ #
59
+ def self.messages
60
+ return super + %w[show page]
61
+ end
62
+
63
+ #
64
+ # Show the markdown data in the terminal.
65
+ #
66
+ def msg_show
67
+ puts TTY::Markdown.parse self.value
68
+ end
69
+
70
+ #
71
+ # Show the markdown data in the terminal, paginated.
72
+ #
73
+ def msg_page
74
+ return unless self.value
75
+
76
+ md = TTY::Markdown.parse self.value
77
+ # pager = TTY::Pager::SystemPager.new command: 'less -R'
78
+ pager = TTY::Pager.new
79
+ pager.page( md )
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,192 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A MySQL database connection.
5
+ #
6
+ #
7
+ # https://github.com/brianmario/mysql2
8
+ # https://www.rubydoc.info/gems/mysql2/0.2.3/Mysql2/Client
9
+ #
10
+ # Connection Parameters
11
+ # user = opts[:username]
12
+ # pass = opts[:password]
13
+ # host = opts[:host] || 'localhost'
14
+ # port = opts[:port] || 3306
15
+ # database = opts[:database]
16
+ # socket = opts[:socket]
17
+ # flags = opts[:flags] || 0
18
+ #
19
+ require 'mysql2'
20
+
21
+ module GlooLang
22
+ module Objs
23
+ class Mysql < GlooLang::Core::Obj
24
+
25
+ KEYWORD = 'mysql'.freeze
26
+ KEYWORD_SHORT = 'mysql'.freeze
27
+
28
+ HOST = 'host'.freeze
29
+ DB = 'database'.freeze
30
+ USER = 'username'.freeze
31
+ PASSWD = 'password'.freeze
32
+
33
+ #
34
+ # The name of the object type.
35
+ #
36
+ def self.typename
37
+ return KEYWORD
38
+ end
39
+
40
+ #
41
+ # The short name of the object type.
42
+ #
43
+ def self.short_typename
44
+ return KEYWORD_SHORT
45
+ end
46
+
47
+ # ---------------------------------------------------------------------
48
+ # Children
49
+ # ---------------------------------------------------------------------
50
+
51
+ #
52
+ # Does this object have children to add when an object
53
+ # is created in interactive mode?
54
+ # This does not apply during obj load, etc.
55
+ #
56
+ def add_children_on_create?
57
+ return true
58
+ end
59
+
60
+ #
61
+ # Add children to this object.
62
+ # This is used by containers to add children needed
63
+ # for default configurations.
64
+ #
65
+ def add_default_children
66
+ fac = $engine.factory
67
+ fac.create_string HOST, nil, self
68
+ fac.create_string DB, nil, self
69
+ fac.create_string USER, nil, self
70
+ fac.create_string PASSWD, nil, self
71
+ end
72
+
73
+ # ---------------------------------------------------------------------
74
+ # Messages
75
+ # ---------------------------------------------------------------------
76
+
77
+ #
78
+ # Get a list of message names that this object receives.
79
+ #
80
+ def self.messages
81
+ return super + [ 'verify' ]
82
+ end
83
+
84
+ #
85
+ # SSH to the host and execute the command, then update result.
86
+ #
87
+ def msg_verify
88
+ return unless connects?
89
+
90
+ $engine.heap.it.set_to true
91
+ end
92
+
93
+ # ---------------------------------------------------------------------
94
+ # DB functions (all database connections)
95
+ # ---------------------------------------------------------------------
96
+
97
+ #
98
+ # Open a connection and execute the SQL statement.
99
+ # Return the resulting data.
100
+ #
101
+ def query( sql, params = nil )
102
+ h = {
103
+ host: host_value,
104
+ database: db_value,
105
+ username: user_value,
106
+ password: passwd_value
107
+ }
108
+ client = Mysql2::Client.new( h )
109
+ return client.query( sql ) unless params
110
+
111
+ pst = client.prepare( sql )
112
+ return pst.execute( *params )
113
+ end
114
+
115
+ # ---------------------------------------------------------------------
116
+ # Private functions
117
+ # ---------------------------------------------------------------------
118
+
119
+ private
120
+
121
+ #
122
+ # Get the host from the child object.
123
+ # Returns nil if there is none.
124
+ #
125
+ def host_value
126
+ o = find_child HOST
127
+ return nil unless o
128
+
129
+ o = GlooLang::Objs::Alias.resolve_alias( o )
130
+ return o.value
131
+ end
132
+
133
+ #
134
+ # Get the Database name from the child object.
135
+ # Returns nil if there is none.
136
+ #
137
+ def db_value
138
+ o = find_child DB
139
+ return nil unless o
140
+
141
+ o = GlooLang::Objs::Alias.resolve_alias( o )
142
+ return o.value
143
+ end
144
+
145
+ #
146
+ # Get the Username from the child object.
147
+ # Returns nil if there is none.
148
+ #
149
+ def user_value
150
+ o = find_child USER
151
+ return nil unless o
152
+
153
+ o = GlooLang::Objs::Alias.resolve_alias( o )
154
+ return o.value
155
+ end
156
+
157
+ #
158
+ # Get the Password name from the child object.
159
+ # Returns nil if there is none.
160
+ #
161
+ def passwd_value
162
+ o = find_child PASSWD
163
+ return nil unless o
164
+
165
+ o = GlooLang::Objs::Alias.resolve_alias( o )
166
+ return o.value
167
+ end
168
+
169
+ #
170
+ # Try the connection and make sure it works.
171
+ # Returns true if we can establish a connection.
172
+ #
173
+ def connects?
174
+ begin
175
+ h = {
176
+ host: host_value,
177
+ database: db_value,
178
+ username: user_value,
179
+ password: passwd_value
180
+ }
181
+ Mysql2::Client.new( h )
182
+ rescue => e
183
+ $engine.err e.message
184
+ $engine.heap.it.set_to false
185
+ return false
186
+ end
187
+ return true
188
+ end
189
+
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,176 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A SQL database query.
5
+ # Relies on a database connection object.
6
+ #
7
+
8
+ module GlooLang
9
+ module Objs
10
+ class Query < GlooLang::Core::Obj
11
+
12
+ KEYWORD = 'query'.freeze
13
+ KEYWORD_SHORT = 'sql'.freeze
14
+
15
+ DB = 'database'.freeze
16
+ SQL = 'sql'.freeze
17
+ RESULT = 'result'.freeze
18
+ PARAMS = 'params'.freeze
19
+
20
+ DB_MISSING_ERR = 'The database connection is missing!'.freeze
21
+
22
+ #
23
+ # The name of the object type.
24
+ #
25
+ def self.typename
26
+ return KEYWORD
27
+ end
28
+
29
+ #
30
+ # The short name of the object type.
31
+ #
32
+ def self.short_typename
33
+ return KEYWORD_SHORT
34
+ end
35
+
36
+ # ---------------------------------------------------------------------
37
+ # Children
38
+ # ---------------------------------------------------------------------
39
+
40
+ #
41
+ # Does this object have children to add when an object
42
+ # is created in interactive mode?
43
+ # This does not apply during obj load, etc.
44
+ #
45
+ def add_children_on_create?
46
+ return true
47
+ end
48
+
49
+ #
50
+ # Add children to this object.
51
+ # This is used by containers to add children needed
52
+ # for default configurations.
53
+ #
54
+ def add_default_children
55
+ fac = $engine.factory
56
+ fac.create_alias DB, nil, self
57
+ fac.create_string SQL, nil, self
58
+ fac.create_can RESULT, self
59
+ end
60
+
61
+ # ---------------------------------------------------------------------
62
+ # Messages
63
+ # ---------------------------------------------------------------------
64
+
65
+ #
66
+ # Get a list of message names that this object receives.
67
+ #
68
+ def self.messages
69
+ return super + [ 'run' ]
70
+ end
71
+
72
+ #
73
+ # SSH to the host and execute the command, then update result.
74
+ #
75
+ def msg_run
76
+ db = db_obj
77
+ unless db
78
+ $engine.err DB_MISSING_ERR
79
+ return
80
+ end
81
+
82
+ result = db.query( sql_value, param_array )
83
+ process_result result
84
+ end
85
+
86
+ # ---------------------------------------------------------------------
87
+ # Private functions
88
+ # ---------------------------------------------------------------------
89
+
90
+ private
91
+
92
+ #
93
+ # Get the database connection.
94
+ #
95
+ def db_obj
96
+ o = find_child DB
97
+ return GlooLang::Objs::Alias.resolve_alias( o )
98
+ end
99
+
100
+ #
101
+ # Get the SQL from the child object.
102
+ # Returns nil if there is none.
103
+ #
104
+ def sql_value
105
+ o = find_child SQL
106
+ return nil unless o
107
+
108
+ return o.value
109
+ end
110
+
111
+ #
112
+ # Do something with the result of the SQL Query call.
113
+ # If there's a result container, we'll create objects in it.
114
+ # If not, we'll just show the output in the console.
115
+ #
116
+ def process_result( data )
117
+ r = find_child RESULT
118
+ if r
119
+ update_result_contaier data
120
+ else
121
+ show_result data
122
+ end
123
+ end
124
+
125
+ #
126
+ # Get the arrya of parameters.
127
+ # If there is no PARAM container of if it is empty,
128
+ # we'll return a nil value.
129
+ #
130
+ def param_array
131
+ o = find_child PARAMS
132
+ return nil unless o
133
+
134
+ return nil if o.child_count.zero?
135
+
136
+ params = []
137
+ o.children.each do |p|
138
+ params << p.value
139
+ end
140
+
141
+ return params
142
+ end
143
+
144
+ #
145
+ # Show the result of the query in the console.
146
+ #
147
+ def show_result( data )
148
+ return if data.nil?
149
+
150
+ data.each_with_index do |row, i|
151
+ # Show header for the first row
152
+ puts row.map { |k, _| k }.join( " \t " ).white if i.zero?
153
+
154
+ # Show the row data
155
+ puts row.map { |_, v| v }.join( " \t " )
156
+ end
157
+ end
158
+
159
+ #
160
+ # Update the result container with the data from the query.
161
+ #
162
+ def update_result_contaier( data )
163
+ r = find_child RESULT
164
+ r = GlooLang::Objs::Alias.resolve_alias( r )
165
+ data.each_with_index do |row, i|
166
+ can = r.find_add_child( i.to_s, 'can' )
167
+ row.each do |k, v|
168
+ o = can.find_add_child( k, 'untyped' )
169
+ o.set_value v
170
+ end
171
+ end
172
+ end
173
+
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,159 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A Sqlite3 database connection.
5
+ #
6
+ # https://www.rubydoc.info/gems/sqlite3/1.3.11
7
+ # https://www.devdungeon.com/content/ruby-sqlite-tutorial
8
+ #
9
+ # db.results_as_hash = true
10
+ # Set results to return as Hash object.
11
+ # This is slower but offers a huge convenience.
12
+ # Consider turning it off for high performance situations.
13
+ # Each row will have the column name as the hash key.
14
+ #
15
+ # # Alternatively, to only get one row and discard the rest,
16
+ # replace `db.query()` with `db.get_first_value()`.
17
+ #
18
+ require 'sqlite3'
19
+
20
+ module GlooLang
21
+ module Objs
22
+ class Sqlite < GlooLang::Core::Obj
23
+
24
+ KEYWORD = 'sqlite'.freeze
25
+ KEYWORD_SHORT = 'sqlite'.freeze
26
+
27
+ DB = 'database'.freeze
28
+ DEFAULT_DB = 'test.db'.freeze
29
+
30
+ DB_REQUIRED_ERR = 'The database name is required!'.freeze
31
+ DB_NOT_FOUND_ERR = 'The database file was not found!'.freeze
32
+
33
+ #
34
+ # The name of the object type.
35
+ #
36
+ def self.typename
37
+ return KEYWORD
38
+ end
39
+
40
+ #
41
+ # The short name of the object type.
42
+ #
43
+ def self.short_typename
44
+ return KEYWORD_SHORT
45
+ end
46
+
47
+ # ---------------------------------------------------------------------
48
+ # Children
49
+ # ---------------------------------------------------------------------
50
+
51
+ #
52
+ # Does this object have children to add when an object
53
+ # is created in interactive mode?
54
+ # This does not apply during obj load, etc.
55
+ #
56
+ def add_children_on_create?
57
+ return true
58
+ end
59
+
60
+ #
61
+ # Add children to this object.
62
+ # This is used by containers to add children needed
63
+ # for default configurations.
64
+ #
65
+ def add_default_children
66
+ fac = $engine.factory
67
+ fac.create_string DB, DEFAULT_DB, self
68
+ end
69
+
70
+ # ---------------------------------------------------------------------
71
+ # Messages
72
+ # ---------------------------------------------------------------------
73
+
74
+ #
75
+ # Get a list of message names that this object receives.
76
+ #
77
+ def self.messages
78
+ return super + [ 'verify' ]
79
+ end
80
+
81
+ #
82
+ # Verify access to the Sqlite database specified.
83
+ #
84
+ def msg_verify
85
+ name = db_value
86
+ if name.empty?
87
+ $engine.err DB_REQUIRED_ERR
88
+ $engine.heap.it.set_to false
89
+ return
90
+ end
91
+
92
+ unless File.exist? name
93
+ $engine.err DB_NOT_FOUND_ERR
94
+ $engine.heap.it.set_to false
95
+ return
96
+ end
97
+
98
+ return unless connects?
99
+
100
+ $engine.heap.it.set_to true
101
+ end
102
+
103
+ # ---------------------------------------------------------------------
104
+ # DB functions (all database connections)
105
+ # ---------------------------------------------------------------------
106
+
107
+ #
108
+ # Open a connection and execute the SQL statement.
109
+ # Return the resulting data.
110
+ #
111
+ def query( sql, params = nil )
112
+ name = db_value
113
+ unless name
114
+ $engine.err DB_REQUIRED_ERR
115
+ return
116
+ end
117
+
118
+ db = SQLite3::Database.open name
119
+ db.results_as_hash = true
120
+ return db.query( sql, params )
121
+ end
122
+
123
+ # ---------------------------------------------------------------------
124
+ # Private functions
125
+ # ---------------------------------------------------------------------
126
+
127
+ private
128
+
129
+ #
130
+ # Get the Database file from the child object.
131
+ # Returns nil if there is none.
132
+ #
133
+ def db_value
134
+ o = find_child DB
135
+ return nil unless o
136
+
137
+ return o.value
138
+ end
139
+
140
+ #
141
+ # Try the connection and make sure it works.
142
+ # Returns true if we can connect and do a query.
143
+ #
144
+ def connects?
145
+ begin
146
+ db = SQLite3::Database.open db_value
147
+ sql = "SELECT COUNT(name) FROM sqlite_master WHERE type='table'"
148
+ db.get_first_value sql
149
+ rescue => e
150
+ $engine.err e.message
151
+ $engine.heap.it.set_to false
152
+ return false
153
+ end
154
+ return true
155
+ end
156
+
157
+ end
158
+ end
159
+ end