-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.rb
executable file
·131 lines (107 loc) · 5.16 KB
/
cli.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env ruby
require 'thor'
require 'net/http'
require 'json'
require 'uri'
class CLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello, #{name}!"
end
desc "install CONTRACT_ID", "install a contract by querying the API with CONTRACT_ID"
def install(contract_id)
puts "Fetching contract information for ID: #{contract_id}"
# Define the API endpoint
api_url = URI("http://localhost:3000/api/v1/contracts/#{contract_id}")
begin
# Make the API request
response = Net::HTTP.get_response(api_url)
# Check if the response was successful
if response.is_a?(Net::HTTPSuccess)
# Parse the JSON response
contract_data = JSON.parse(response.body)
# Display the response
puts "Contract information:"
puts JSON.pretty_generate(contract_data)
# You can add additional processing here if needed
# For example, installing something based on the contract data
puts "Installation complete!"
else
puts "Error: API returned status code #{response.code}"
puts response.body
end
rescue => e
puts "Error: Failed to connect to the API - #{e.message}"
end
end
desc "project [PROJECT_ID]", "fetch contracts for a project or all projects"
def project(project_id = nil)
if project_id
puts "Fetching contracts for project ID: #{project_id}"
# Define the API endpoint for a specific project
api_url = URI("http://localhost:3000/api/v1/contracts/project/#{project_id}")
begin
# Make the API request
response = Net::HTTP.get_response(api_url)
# Check if the response was successful
if response.is_a?(Net::HTTPSuccess)
# Parse the JSON response
contracts_data = JSON.parse(response.body)
# Display only name, app_id and version for each contract
puts "Contracts for project #{project_id}:"
puts "---------------------------------"
if contracts_data.empty?
puts "No contracts found for this project."
else
contracts_data.each do |contract|
name = contract["name"] || "Unnamed contract"
app_id = contract["app_id"] || "N/A"
version = contract["version"] || "N/A"
puts "#{name} - App ID: #{app_id}, Version: #{version}"
end
puts "---------------------------------"
puts "Total contracts: #{contracts_data.length}"
end
puts "Fetch complete!"
else
puts "Error: API returned status code #{response.code}"
puts response.body
end
rescue => e
puts "Error: Failed to connect to the API - #{e.message}"
end
else
puts "Fetching projects summary..."
# Define the API endpoint for all projects
api_url = URI("http://localhost:3000/api/v1/projects")
begin
# Make the API request
response = Net::HTTP.get_response(api_url)
# Check if the response was successful
if response.is_a?(Net::HTTPSuccess)
# Parse the JSON response
projects_data = JSON.parse(response.body)
# Display only name, abbreviation and number of contracts for each project
puts "Projects summary:"
puts "-----------------"
projects_data.each do |project|
# Assuming the API returns an array of project objects with these fields
# Adjust field names if they're different in your actual API response
name = project["name"] || "Unnamed project"
abbr = project["abbreviation"] || "N/A"
contracts_count = project["contract_count"]
puts "#{name} (#{abbr}) - #{contracts_count} contracts"
end
puts "-----------------"
puts "Total projects: #{projects_data.length}"
else
puts "Error: API returned status code #{response.code}"
puts response.body
end
rescue => e
puts "Error: Failed to connect to the API - #{e.message}"
end
end
end
end
CLI.start(ARGV)