#!/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 ## ######################### # This block is where rbcli is configured. # Any options marked as Optional can be commented out. # Options marked as Multiple can be declared multiple times. ######################### Rbcli::Configurate.me do #### # GENERAL ### # These parameters are for genreal information about your application. ### ## Script Name -- (Required) -- This line identifies the tool's executable on the command line. # To change it, rename this file to the command you want and this will pick up that name automatically. # You can change it manually if needed, but this should work for most cases. 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] %>} #### # LOGGING ### # These parameters set the default logging parameters for your applicaiton. Note that a user # can override these settings by modifying their local configuration. # If either option is set to nil, or they are not provided, logging is disabled. ### ## Log Target -- (Optional) -- Set the target for logs. # Valid values are nil, 'STDOUT', 'STDERR', or a file path (as strings). # Default is disabled (nil). log_target nil ## Log Level -- (Optional) -- Set the default log_level for users. # Valid values are nil, 0-5, or DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN. # Default is disabled (nil). log_level nil #### # USER CONFIGURATION MANAGEMENT ### # RBCli allows you to create a configuration file that your users can modify. # Users will be able to generate their own file by running your tool with the `-g` option. # To disable this feature, comment out the `config_userfile` line below. ### ## Config Userfile -- (Optional) -- Set location of user's config file. # # config_userfile , merge_defaults: , required: ', merge_defaults: true, required: false ## Config Defaults -- (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 files. # TODO: This line is not needed when using RBCli in standard mode. YAML files go in the `user_configs` directory instead. #config_defaults 'defaults.yml' ## Config Default -- (Optional, Multiple) -- Specify an individual configuration parameter and set a default value. # These will be included in generated user config. config_default :myopt, description: 'Testing this', default: true #### # AUTOUPDATE ### # RBCli can notify users when you have an update to your application. # This requires your application to be published either on Rubygems.org, Github, or Github Enterprise. # Note that only one can be enabled at a time. # For more details on each integration, see below. ### ## Autoupdate, Github -- (Optional) -- Check for updates to this application at a GitHub repo. # The repo should use version number tags in accordance to Github's best practices: https://help.github.com/articles/creating-releases/ # # Note that the `access_token` can be overridden by the user via their configuration file, so it can be left as `nil`, # which will require your users to enter their github tokens to use this feature. # For instructions on generating a new access token, see: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ # The token is not needed if using a public repo. # # The `enterprise_hostname` setting allows you to point RBCli at a local GitHub Enterprise server. # # Setting `force_update: true` will halt execution if an update is available, forcing the user to update. # #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 #### # Command Line Options ### # Here you can set global options for users on the command line. # RBCli supports commands with syntax as follows: # toolname [options] command [parameters] [lineitem] # # Here you are defining the [options]. The parameters and lineitms (subcommands) are # defined under their command blocks. # # The following types are supported: `:string`, `:boolean` or `:flag`, `:integer`, and `:float` # # If a default value is not set, it will default to `nil`. # # To specify multiple options, simply copy the line and modify as desired. # # Once parsed, option values will be placed in a hash where they can be accessed via their names as shown above where # they are made available to your hooks and commands. ### ## 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 ## ############################### # Here you can set hooks that will be run at specified points in the execution chain. # Global CLI options are made available to many of the hooks, but command parameters and lineitems are not. ############################### Rbcli::Configurate.hooks do ## Default Action -- (Optional) -- The default code to execute when no subcommand is given. # If not present, the help is shown (same as -h) 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. # If `halt_after_running` is set to `true` then parsing will not continue after this code is executed. All subsequent runs will not execute this code. # This feature is dependent on the Local State Storage system, and will not run without it. 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 ## ############################### # The state-related componets # are configured here. ############################### Rbcli::Configurate.storage do ### # Local State Storage ### # Local state storage creates a hash that is automatically saved to a file locally for state persistance. # It is accessible to all commands at: Rbcli.localstate[:yourkeyhere] # # Note that every update to the top level of this hash will trigger a save, and updating nested hashes will not. # If you need to update nested hashes, you can trigger a save manually by calling `Rbcli.localstate.commit`. # Please see the documentation for full usage details. ### #local_state '/var/mytool/localstate', force_creation: true, halt_on_error: true # (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] ### # Remote State Storage ### # Remote state storage creates a hash that is automatically saved to a remote database for state persistence. # This state can be made unique to each user, or can be shared across different users of the tool. # # When sharing, locking should be set to `true`. Note that RBCli uses lazy-loaded, meaning the lock will # only be acquired when absolutely needed. This behavior can be overridden manually if desired. # For full usage details, see the documentation. ### #remote_state_dynamodb table_name: 'mytable', region: 'us-east-1', force_creation: true, halt_on_error: true, locking: :auto # (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) -- see the README for details end ######################### ## Command Declaration ## ######################### # With rbcli, commands are declared by subclassing # from Rbcli::Command. The name of the class will be # the command that is available to the user. ######################### 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 ## ##################### # When this statement is called, the CLI will be # parsed and code executed. ##################### Rbcli.parse # Parse CLI and execute