Class: Securial::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/securial/cli.rb

Overview

Command-line interface for the Securial gem.

This class provides the command-line functionality for Securial, enabling users to create new Rails applications with Securial pre-installed and configured. It handles command parsing, flag processing, and orchestrates the setup of new applications.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(argv) ⇒ Integer

Entry point for the CLI application.

Creates a new CLI instance and delegates to its start method.

Parameters:

  • argv (Array<String>)

    command line arguments

Returns:

  • (Integer)

    exit status code (0 for success, non-zero for errors)



36
37
38
# File 'lib/securial/cli.rb', line 36

def self.start(argv)
  new.start(argv)
end

Instance Method Details

#add_securial_gem(app_name) ⇒ void (private)

This method returns an undefined value.

Adds the Securial gem to the application’s Gemfile.

Parameters:

  • app_name (String)

    name of the Rails application



197
198
199
200
201
# File 'lib/securial/cli.rb', line 197

def add_securial_gem(app_name)
  puts "📦  Adding Securial gem to Gemfile"
  gemfile_path = File.join(app_name, "Gemfile")
  File.open(gemfile_path, "a") { |f| f.puts "\ngem 'securial'" }
end

#create_option_parserOptionParser (private)

Creates and configures the option parser.

Sets up the command-line options and their handlers.

Returns:

  • (OptionParser)

    configured option parser instance



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/securial/cli.rb', line 128

def create_option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: securial [options] <command> [command options]\n\n"

    opts.separator ""
    opts.separator "Commands:"
    opts.separator "    new APP_NAME [rails_options...]    # Create a new Rails app with Securial pre-installed"
    opts.separator ""
    opts.separator "Options:"

    opts.on("-v", "--version", "Show Securial version") do
      show_version
      exit(0)
    end

    opts.on("-h", "--help", "Show this help message") do
      puts opts
      exit(0)
    end
  end
end

#create_rails_app(app_name, rails_options) ⇒ Integer (private)

Creates a new Rails application.

Parameters:

  • app_name (String)

    name of the Rails application to create

  • rails_options (Array<String>)

    options to pass to ‘rails new’

Returns:

  • (Integer)

    command exit status



187
188
189
190
# File 'lib/securial/cli.rb', line 187

def create_rails_app(app_name, rails_options)
  rails_command = ["rails", "new", app_name, *rails_options]
  run(rails_command)
end

#handle_commands(argv) ⇒ Integer (private)

Processes commands from the command line arguments.

Identifies the command (e.g., ‘new’) and delegates to the appropriate handler method for that command.

Parameters:

  • argv (Array<String>)

    command line arguments

Returns:

  • (Integer)

    exit status code (0 for success, non-zero for errors)



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/securial/cli.rb', line 89

def handle_commands(argv)
  cmd = argv.shift

  case cmd
  when "new"
    handle_new_command(argv)
  else
    puts create_option_parser
    1
  end
end

#handle_flags(argv) ⇒ nil, Integer (private)

Processes option flags from the command line arguments.

Parses options like –version and –help, executing their actions if present and removing them from the argument list.

Parameters:

  • argv (Array<String>)

    command line arguments

Returns:

  • (nil, Integer)

    nil to continue processing, or exit code



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/securial/cli.rb', line 68

def handle_flags(argv)
  parser = create_option_parser

  begin
    parser.order!(argv)
    nil # Continue to command handling
  rescue OptionParser::InvalidOption => e
    warn "ERROR: Illegal option(s): #{e.args.join(' ')}"
    puts parser
    1
  end
end

#handle_new_command(argv) ⇒ Integer (private)

Handles the ‘new’ command for creating Rails applications.

Validates that an application name is provided, then delegates to securial_new to create the application with Securial.

Parameters:

  • argv (Array<String>)

    remaining command line arguments

Returns:

  • (Integer)

    exit status code (0 for success, non-zero for errors)



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/securial/cli.rb', line 109

def handle_new_command(argv)
  app_name = argv.shift

  if app_name.nil?
    puts "ERROR: Please provide an app name."
    puts create_option_parser
    return 1
  end

  securial_new(app_name, argv)
  0
end

#install_gems(app_name) ⇒ Integer (private)

Installs gems for the application using Bundler.

Parameters:

  • app_name (String)

    name of the Rails application

Returns:

  • (Integer)

    command exit status



208
209
210
# File 'lib/securial/cli.rb', line 208

def install_gems(app_name)
  run("bundle install", chdir: app_name)
end

#install_securial(app_name) ⇒ Integer (private)

Installs and configures Securial in the application.

Parameters:

  • app_name (String)

    name of the Rails application

Returns:

  • (Integer)

    command exit status



217
218
219
220
221
# File 'lib/securial/cli.rb', line 217

def install_securial(app_name)
  puts "🔧  Installing Securial"
  run("bin/rails generate securial:install", chdir: app_name)
  run("bin/rails db:migrate", chdir: app_name)
end

#mount_securial_engine(app_name) ⇒ void (private)

This method returns an undefined value.

Mounts the Securial engine in the application’s routes.

Parameters:

  • app_name (String)

    name of the Rails application



228
229
230
231
232
233
234
235
236
# File 'lib/securial/cli.rb', line 228

def mount_securial_engine(app_name)
  puts "🔗  Mounting Securial engine in routes"
  routes_path = File.join(app_name, "config/routes.rb")
  routes = File.read(routes_path)
  updated = routes.sub("Rails.application.routes.draw do") do |match|
    "#{match}\n  mount Securial::Engine => '/securial'"
  end
  File.write(routes_path, updated)
end

This method returns an undefined value.

Prints final setup instructions after successful installation.

Parameters:

  • app_name (String)

    name of the Rails application



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/securial/cli.rb', line 243

def print_final_instructions(app_name)
  puts <<~INSTRUCTIONS
    🎉  Securial has been successfully installed in your Rails app!
    ✅  Your app is ready at: ./#{app_name}

    ➡️  Next steps:
        cd #{app_name}
    ⚙️  Optional: Configure Securial in config/initializers/securial.rb
        rails server
  INSTRUCTIONS
end

#run(command, chdir: nil) ⇒ Integer (private)

Runs a system command with optional directory change.

Executes the provided command, optionally in a different directory, and handles success/failure conditions.

Parameters:

  • command (String, Array<String>)

    command to run

  • chdir (String, nil) (defaults to: nil)

    directory to change to before running command

Returns:

  • (Integer)

    command exit status code (0 for success)

Raises:

  • (SystemExit)

    if the command fails



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/securial/cli.rb', line 265

def run(command, chdir: nil)
  puts "#{command.inspect}"
  result =
    if chdir
      Dir.chdir(chdir) { system(*command) }
    else
      system(*command)
    end

  unless result
    abort("❌ Command failed: #{command}")
  end
  0
end

#securial_new(app_name, rails_options) ⇒ void (private)

This method returns an undefined value.

Creates a new Rails application with Securial pre-installed.

Orchestrates the process of creating a Rails application, adding the Securial gem, installing dependencies, and configuring the application.

Parameters:

  • app_name (String)

    name of the Rails application to create

  • rails_options (Array<String>)

    options to pass to ‘rails new’



170
171
172
173
174
175
176
177
178
179
# File 'lib/securial/cli.rb', line 170

def securial_new(app_name, rails_options)
  puts "🏗️  Creating new Rails app: #{app_name}"

  create_rails_app(app_name, rails_options)
  add_securial_gem(app_name)
  install_gems(app_name)
  install_securial(app_name)
  mount_securial_engine(app_name)
  print_final_instructions(app_name)
end

#show_versionvoid (private)

This method returns an undefined value.

Displays the current Securial version.



154
155
156
157
158
159
# File 'lib/securial/cli.rb', line 154

def show_version
  require "securial/version"
  puts "Securial v#{Securial::VERSION}"
rescue LoadError
  puts "Securial version information not available."
end

#start(argv) ⇒ Integer

Processes command line arguments and executes the appropriate action.

This method handles both option flags (like –version) and commands (like ‘new’), delegating to specialized handlers and returning the appropriate exit code.

Parameters:

  • argv (Array<String>)

    command line arguments

Returns:

  • (Integer)

    exit status code (0 for success, non-zero for errors)



49
50
51
52
53
54
55
56
# File 'lib/securial/cli.rb', line 49

def start(argv)
  # Process options and exit if a flag was handled
  result = handle_flags(argv)
  exit(result) if result

  # Otherwise handle commands
  exit(handle_commands(argv))
end