-
Notifications
You must be signed in to change notification settings - Fork 83
/
vagrant.lua
141 lines (126 loc) · 4.1 KB
/
vagrant.lua
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
local matchers = require('matchers')
local path = require('path')
local parser = clink.arg.new_parser
local boxes = matchers.create_dirs_matcher(clink.get_env("userprofile") .. "/.vagrant.d/boxes/*")
local function is_empty(s)
return s == nil or s == ''
end
local function find_vagrantfile(start_dir)
local vagrantfile_name = clink.get_env("VAGRANT_VAGRANTFILE")
if is_empty(vagrantfile_name) then vagrantfile_name = "Vagrantfile" end
local function has_vagrantfile(dir)
return #clink.find_files(dir .. "./" .. vagrantfile_name .. "*") > 0
end
if not start_dir or start_dir == '.' then start_dir = clink.get_cwd() end
if has_vagrantfile(start_dir) then return io.open(start_dir.."\\"..vagrantfile_name) end
local parent_path = path.pathname(start_dir)
if parent_path ~= start_dir then return find_vagrantfile(parent_path) end
end
local function get_vagrantfile()
local vagrant_cwd = clink.get_env("VAGRANT_CWD")
if not is_empty(vagrant_cwd) then
return find_vagrantfile(vagrant_cwd)
else
return find_vagrantfile()
end
end
local function delete_ruby_comment(line)
if line == nil then return nil end
local index = string.find(line, '#')
if index and index > 0 then
return string.sub(line, 0, index-1)
end
return line
end
local get_provisions = function ()
local vagrant_file = get_vagrantfile()
if vagrant_file == nil then return {} end
local provisions = {}
for line in vagrant_file:lines() do
line = delete_ruby_comment(line)
if not is_empty(line) then
local provision_name = line:match('.vm.provision[ \r\t]+[\"|\']([A-z]+[A-z0-9|-]*)[\"|\']')
if not is_empty(provision_name) then
table.insert(provisions, provision_name)
end
end
end
vagrant_file:close()
return provisions
end
local vagrant_parser = parser({
"box" .. parser({
"add" .. parser(
"--checksum",
"--checksum-type" .. parser({"md5", "sha1", "sha256"}),
"-c", "--clean",
"-f", "--force",
"--insecure",
"--cacert",
"--cert",
"--provider"
),
"list" .. parser("-i", "--box-info"),
"outdated"..parser("--global", "-h", "--help"),
"remove" .. parser({boxes}),
"repackage" .. parser({boxes}),
"update"
}),
"connect",
"destroy" .. parser("-f", "--force"),
"global-status",
"halt" .. parser("-f", "--force"),
"init" .. parser({boxes}, {}, "--output"),
"package" .. parser("--base", "--output", "--include", "--vagrantfile"),
"plugin" .. parser({
"install" .. parser(
"--entry-point",
"--plugin-prerelease",
"--plugin-source",
"--plugin-version"
),
"license",
"list",
"uninstall",
"update" .. parser(
"--entry-point",
"--plugin-prerelease",
"--plugin-source",
"--plugin-version"
)
}),
"provision" .. parser("--provision-with" .. parser({get_provisions}), "--no-parallel", "--parallel"),
"reload" .. parser("--provision-with" .. parser({get_provisions}), "--no-parallel", "--parallel"),
"resume",
"ssh" .. parser("-c", "--command", "-p", "--plain") ,
"ssh-config",
"snapshot" .. parser({
"push",
"pop" .. parser(
"--provision",
"--no-provision",
"--no-delete"),
"save",
"restore" .. parser(
"--provision",
"--no-provision"),
"list",
"delete"}),
"status",
"suspend",
"up" .. parser(
"--provision",
"--no-provision",
"--provision-with" .. parser({get_provisions}),
"--destroy-on-error",
"--no-destroy-on-error",
"--parallel",
"--no-parallel",
"--provider"
)
}, "-h", "--help", "-v", "--version")
local help_parser = parser({
"help" .. parser(vagrant_parser:flatten_argument(1))
})
clink.arg.register_parser("vagrant", vagrant_parser)
clink.arg.register_parser("vagrant", help_parser)