-
Notifications
You must be signed in to change notification settings - Fork 61
/
phantom-dc
executable file
·143 lines (123 loc) · 3.82 KB
/
phantom-dc
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
142
#!/usr/bin/env ruby
# encoding: utf-8
# This file can be used to start Padrino,
# just execute it from the command line.
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("--git-clone <git-url>", String, "Add a data source by cloning from git") do |git_url|
options[:git_url] = git_url
end
opts.on("--prefix <prefix>", String, "Use a prefix when storing reps in the database") do |prefix|
options[:prefix] = prefix
end
opts.on("-h", "--help", "Display help dialogue") do
options[:help] = true
end
end.parse!
if not options[:verbose]
PADRINO_LOGGER = {
development: {stream: :null},
production: {stream: :null},
test: {stream: :null}
}
end
def require_boot
require File.expand_path("../config/boot.rb", __FILE__)
end
def usage
format = " %-20.20s%-40.40s\n"
puts ""
puts "Usage: phantom-dc [--verbose] [--help] <command> [<args>]"
puts ""
puts "Commands:"
printf format, "datasource", "manipulate (add or delete) a data source"
puts ""
end
def usage_datasource
format = " %-10.10s%-40.40s\n"
puts ""
puts "Usage: phantom-dc [<flags>] datasource <args>"
puts "Usage: phantom-dc [<flags>] datasource add [--git-clone <git-url>] [--prefix <prefix>] <name> <path> <yaml_subpath>"
puts "Usage: phantom-dc [<flags>] datasource delete <name>"
puts "Usage: phantom-dc [<flags>] datasource list"
puts ""
puts "Arguments:"
printf format, "add", "a new data source"
printf format, "delete", "delete a data source"
printf format, "list", "provide a list of data sources"
puts ""
end
if ARGV.length == 0
usage
exit
end
case ARGV[0]
when "datasource"
if options[:help]
usage_datasource
exit
end
case ARGV[1]
when "add"
if ARGV.length != 5
usage_datasource
exit
end
require_boot
if DataSource.find_by_name(ARGV[2]).nil?
prefix = options[:prefix] || ""
if options[:git_url]
require 'pathname'
full_path = Pathname.new(ARGV[3])
parent_path = full_path.parent
base_path = full_path.basename
Git.clone options[:git_url], base_path, path: parent_path
else
Git.open ARGV[3]
end
DataSource.create(name: ARGV[2], path: ARGV[3], yaml_subpath: ARGV[4], latest_commit: nil, prefix: prefix)
else
puts "Error: Data source with name " + ARGV[2] + " already exists!"
exit 1
end
puts "Data source created."
when "delete"
if ARGV.length != 3
usage_datasource
exit
end
require_boot
ds = DataSource.find_by(name: ARGV[2])
if ds.nil?
puts "Error: Data source with name '" + ARGV[2] + "' was not found."
exit 1
else
ds.destroy
puts "Data source deleted."
end
when "list"
require_boot
name_length = [(DataSource.all.map{ |ds| ds.name.length }.max.to_i + 2), 20].max
prfx_length = [(DataSource.all.map{ |ds| ds.prefix.length }.max.to_i + 2) || 0, 10].max
path_length = [(DataSource.all.map{ |ds| ds.path.length }.max.to_i + 2), 40].max
subp_length = [(DataSource.all.map{ |ds| ds.yaml_subpath.length }.max.to_i + 2), 40].max
comm_length = 42
format = "%-" + name_length.to_s + "." + name_length.to_s + "s%-" + prfx_length.to_s + "." + prfx_length.to_s + "s%-" + path_length.to_s + "." + path_length.to_s + "s%-" + subp_length.to_s + "." + subp_length.to_s + "s%-" + comm_length.to_s + "." + comm_length.to_s + "s\n"
separator = "=" * 600
puts ""
printf format, "name", "prefix", "path", "yaml_subpath", "latest commit"
printf format, separator, separator, separator, separator, separator
DataSource.all.each do |ds|
printf format, ds.name, ds.prefix, ds.path, ds.yaml_subpath, ds.latest_commit.to_s
end
puts ""
else
usage_datasource
end
else
usage
end