-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_server_commands
executable file
·106 lines (83 loc) · 2.61 KB
/
generate_server_commands
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'fileutils'
def snake_case(str)
return str.downcase if str =~ /\A[A-Z]+\z/
str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z])([A-Z])/, '\1_\2')
.downcase
end
def make_identifier(str)
str.split(/[_-]/).collect(&:capitalize).join
end
def file_header(package_name)
<<~ENDFUNC
// Code generated by generate_server_commands; DO NOT EDIT.
package #{package_name}
import "context"
import "path"
ENDFUNC
end
def test_header(package_name)
<<~ENDFUNC
// Code generated by generate_server_commands; DO NOT EDIT.
package #{package_name}
import (
"path"
"testing"
"gotest.tools/v3/assert"
)
ENDFUNC
end
def make_server_identifier(command)
# FIXME: This is hacky. Sorry.
return make_identifier("#{command}_for_server") if command.include?('console')
make_identifier("#{command}_server")
end
def generate_server_command(command, text)
identifier = make_server_identifier(command)
<<~ENDFUNC
// #{identifier} #{text}
func (c *Client) #{identifier}(ctx context.Context, identifier string) (*Server, error) {
return apiPost[Server](ctx, c, path.Join(serverAPIPath, identifier, "#{command}"), nil)
}
ENDFUNC
end
def generate_server_command_test(command)
identifier = make_server_identifier(command)
<<~ENDFUNC
func Test#{identifier}(t *testing.T) {
instance := testModify[Server, string](
t,
(*Client).#{identifier},
"srv-lv426",
"server",
"POST",
path.Join("servers", "srv-lv426", "#{command}"),
"",
)
assert.Equal(t, instance.ID, "srv-lv426")
}
ENDFUNC
end
commands = {
'start' => 'will issue a start request for the server to become active.',
'stop' => 'will issue a stop request for the server to become inactve.',
'reboot' => 'issues a "soft" reboot to the server, however the OS make ignore it. The console remains connected.',
'reset' => 'issues a "hard" reboot to the server which cannot be ignored by the OS. The console remains connected.',
'shutdown' => 'will issue a safe shutdown request to the server.',
'activate_console' => 'will issue a request to enable the graphical console for an existing server.'
}
File.open('server_commands.go', 'w') do |f|
f << file_header('brightbox')
commands.each do |k, v|
f << generate_server_command(k, v)
end
end
File.open('server_commands_test.go', 'w') do |f|
f << test_header('brightbox')
commands.each_key do |k|
f << generate_server_command_test(k)
end
end
exec('gofmt', '-w', 'server_commands.go', 'server_commands_test.go')