|
| 1 | +#! /usr/bin/env ruby |
| 2 | + |
| 3 | +require_relative "./sample" |
| 4 | + |
| 5 | +require "gli" |
| 6 | +require "highline" |
| 7 | +include GLI::App |
| 8 | + |
| 9 | +program_desc "Manage Google Cloud Storage buckets" |
| 10 | + |
| 11 | +desc "Your Google Cloud project ID" |
| 12 | +arg_name "PROJECT_ID" |
| 13 | +flag %w[ p project-id ] |
| 14 | + |
| 15 | +pre do |global_options, command, options, arguments| |
| 16 | + # Store the current command so commands may reference it |
| 17 | + @command = command |
| 18 | +end |
| 19 | + |
| 20 | +# Print help documentation |
| 21 | +def print_help |
| 22 | + commands[:help].execute({},{}, @command.name_for_help) |
| 23 | +end |
| 24 | + |
| 25 | +# If the provided value is not present, prompt the user to enter a value. |
| 26 | +# If the user does not enter a value, display help output for this command. |
| 27 | +def get variable_description, value = nil |
| 28 | + if value.to_s.empty? |
| 29 | + cli = HighLine.new |
| 30 | + value = cli.ask "Enter #{variable_description}:" |
| 31 | + |
| 32 | + if value.to_s.empty? |
| 33 | + puts "Missing #{variable_description}" |
| 34 | + puts |
| 35 | + print_help |
| 36 | + exit 1 |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + value |
| 41 | +end |
| 42 | + |
| 43 | +desc "List all buckets in the authenticated project" |
| 44 | +command :list do |c| |
| 45 | + c.action do |globals, options, args| |
| 46 | + project_id = get "Google Cloud project ID", globals["project-id"] |
| 47 | + |
| 48 | + list_buckets project_id |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +desc "Create a new bucket with the given name" |
| 53 | +arg_name "BUCKET_NAME" |
| 54 | +command :create do |c| |
| 55 | + c.action do |globals, options, args| |
| 56 | + project_id = get "Google Cloud project ID", globals["project-id"] |
| 57 | + bucket_name = get "Google Cloud Storage bucket name", args.first |
| 58 | + |
| 59 | + create_bucket project_id, bucket_name |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +desc "Delete the specified bucket." |
| 64 | +arg_name "BUCKET_NAME" |
| 65 | +command :delete do |c| |
| 66 | + c.action do |globals, options, args| |
| 67 | + project_id = get "Google Cloud project ID", globals["project-id"] |
| 68 | + bucket_name = get "Google Cloud Storage bucket name", args.first |
| 69 | + |
| 70 | + delete_bucket project_id, bucket_name |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +exit run(ARGV) |
0 commit comments