module WdiTf::Cli

Public Instance Methods

get_cli_command(args: ARGV) click to toggle source
# File lib/wdi_tf/cli.rb, line 40
def get_cli_command(args: ARGV)
    verbose("Get the Terraform command to run from the command line ARGVs.")
    
    cmd = ARGV.shift
    cmd ||= "plan"
    
    if not @@allowed_cmds.include?(cmd)
        error(msg: "Unknown command '#{ cmd }'.  Accepted commands: #{ @@allowed_cmds.join(', ') }")
    end
    
    return cmd
end
load_opts(args: ARGV) click to toggle source
# File lib/wdi_tf/cli.rb, line 16
        def load_opts(args: ARGV)
            Trollop.options(args) do
                banner <<-EOS.gsub(/^ {20}/, '')
                    tf: Wrapper for Terraform
                    
                    Usage:
                      tf [options] COMMAND
                    
                    Commands:
                      clean     - cleans the workspace and returns it back to the original state.
                      plan      - runs 'terraform plan' on the workspace. (default)
                      apply     - runs 'terraform apply' on the workspace.
                      destroy   - runs 'teraform destroy' on the workspace.
                    
                    Options:
                EOS
                
                opt :debug, 'Puts the wrapper into debug mode', type: TrueClass, default: nil
                opt :verbose, 'Display more information to STDOUT', type: TrueClass, default: nil
                
                stop_on_unknown
            end
        end
start() click to toggle source
# File lib/wdi_tf/cli.rb, line 53
def start
    opts = load_opts
    WdiTf::Funcs.set_verbose = opts[:verbose]
    
    cmd = get_cli_command
    
    @main = WdiTf::Main.new()
    @main.clean_workspace()
    
    if not cmd == "clean"
        info("Deploying the Terraform project.")

        if not File.exists?(@@project_file)
            error(msg: "Unable to locate your Terraform project file.", code: 1)
        end
        
        @project = WdiTf::Project.new(file: @@project_file)
        
        source = @project.load_source_files()
        variables = @project.load_vars_info()
        
        @main.generate_vars_file(file: @@variables_file, vars: variables)
        @main.build_workspace(files: source)
        @main.run_terraform(command: cmd, variables: @@variables_file)
        
        if opts[:debug]
            verbose("Leaving the workspace as is; in debug mode.")
        else
            @main.clean_workspace()
        end
    end
    
    info("Done!")
end