#!/usr/bin/env ruby ######################################################### ### <%= @vars[:cmdname] %> ####### ## This is the main executable for <%= @vars[:cmdname] %>. ## It's job is to load and execute the application. ######################################################### require 'rbcli' ######################### ## Configuration Block ## ######################### Rbcli::Configurate.me do ## Script Name -- (Required) -- This line identifies the tool's executable on the command line. scriptname __FILE__.split('/')[-1] ## Version Number -- (Required) version '0.1.0' ## Description -- (Requierd) -- A description that will appear when the user looks at the help with -h. This can be as long as needed. description %q{<%= @vars[:description] %>} ## Log Target -- (Optional) -- Set the target for logs. log_target nil ## Log Level -- (Optional) -- Set the default log_level for users. log_level nil ## Config Userfile -- (Optional) -- Set location of user's config file. config_userfile '~/.<%= @vars[:cmdname] %>', merge_defaults: true, required: false ## Config Defaults -- (Optional, Multiple) -- Load a YAML file as part of the default config. #config_defaults 'defaults.yml' ## Config Default -- (Optional, Multiple) -- Specify an individual configuration parameter and set a default value. config_default :myopt, description: 'Testing this', default: true ## Autoupdate, Github -- (Optional) -- Check for updates to this application at a GitHub repo. #autoupdate github_repo: '/', access_token: nil, enterprise_hostname: nil, force_update: false ## Autoupdate, Rubygems.org -- (Optional) -- Check for updates to this application on Rubygems.org #autoupdate gem: '', force_update: false ## Option -- (Optional, Multiple) -- Add a global CLI Option option :name, 'Give me your name', type: :string, default: 'Foo', required: false, permitted: ['Jack', 'Jill'] end ############################### ## Hooks Configuration Block ## ############################### Rbcli::Configurate.hooks do ## Default Action -- (Optional) -- The default code to execute when no subcommand is given. default_action do |opts| puts "Hello, #{opts[:name]}." puts "To see the help, use -h" end ## Pre-Execution Hook -- (Optional) -- Allows providing a block of code that runs _before_ all commands # pre_hook do |opts| # puts 'This is a pre-command hook. It executes before the command.' # end ## Post-Execution Hook -- (Optional) -- Allows providing a block of code that runs _after_ all commands # post_hook do |opts| # puts 'This is a post-command hook. It executes after the command.' # end ## First-Run Hook -- (Optional) -- Allows providing a block of code that executes the first time that the application is run on a given system. # first_run halt_after_running: false do # puts "This is the first time the mytool command is run! Don't forget to generate a config file with the `-g` option before continuing." # end end ############################### ## State Configuration Block ## ############################### Rbcli::Configurate.storage do ### # Local State Storage ### ## Local State -- (Optional) -- Creates a hash that is automatically saved to a file locally for state persistance. It is accessible to all commands at Rbcli.localstate[:yourkeyhere] #local_state '/var/mytool/localstate', force_creation: true, halt_on_error: true ## Remote State -- (Optional) -- Creates a hash that is automatically saved to a DynamoDB table. It is recommended to keep halt_on_error=true when using a shared state. Locking can be one of (:manual :auto :none). #remote_state_dynamodb table_name: 'mytable', region: 'us-east-1', force_creation: true, halt_on_error: true, locking: :auto end ######################### ## Command Declaration ## ######################### class Test < Rbcli::Command # Declare a new command by subclassing Rbcli::Command description 'This is a short description.' # (Required) Short description for the global help usage 'This is some really long usage text description!' # (Required) Long description for the command-specific help parameter :force, 'Force testing', type: :boolean, default: false, required: false # (Optional, Multiple) Add a command-specific CLI parameter. Can be called multiple times config_defaults 'defaults.yml' # (Optional, Multiple) Load a YAML file as part of the default config. This can be called multiple times, and the YAML files will be merged. User config is generated from these config_default :myopt2, description: 'Testing this again', default: true # (Optional, Multiple) Specify an individual configuration parameter and set a default value. These will also be included in generated user config extern path: 'env | grep "^__PARAMS\|^__ARGS\|^__GLOBAL\|^__CONFIG"', envvars: {MYVAR: 'some_value'} # (Required unless `action` defined) Runs a given application, with optional environment variables, when the user runs the command. extern envvars: {MY_OTHER_VAR: 'another_value'} do |params, args, global_opts, config| # Alternate usage. Supplying a block instead of a path allows us to modify the command based on the arguments and configuration supplied by the user. "echo #{params[:force].to_s}__YESSS!!!" end action do |params, args, global_opts, config| # (Required unless `extern` defined) Block to execute if the command is called. Rbcli::log.info { 'These logs can go to STDERR, STDOUT, or a file' } # Example log. Interface is identical to Ruby's logger puts "\nArgs:\n#{args}" # Arguments that came after the command on the CLI (i.e.: `mytool test bar baz` will yield args=['bar', 'baz']) puts "Params:\n#{params}" # Parameters, as described through the option statements above puts "Global opts:\n#{global_opts}" # Global Parameters, as descirbed in the Configurate section puts "Config:\n#{config}" # Config file values puts "LocalState:\n#{Rbcli.local_state}" # Local persistent state storage (when available) -- if unsure use Rbcli.local_state.nil? puts "RemoteState:\n#{Rbcli.remote_state}" # Remote persistent state storage (when available) -- if unsure use Rbcli.remote_state.nil? puts "\nDone!!!" end end ##################### ## Parse Statement ## ##################### Rbcli.parse # Parse CLI and execute