Class: AppCommand::Switch

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Defined in:
lib/routes/switch.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/routes/switch.rb', line 5

def execute

    begin

        @opts = command_options
        @args = arguments

        opts_validate
        opts_routing

    rescue => e

        Blufin::Terminal::print_exception(e)

    end

end

#opts_routingObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/routes/switch.rb', line 27

def opts_routing

    config_file = File.expand_path(App::CONFIG_FILE)

    # Bomb-out if file went missing...
    Blufin::Terminal::error("File not found: #{Blufin::Terminal::format_invalid(config_file)}") unless Blufin::Files::file_exists(config_file)

    # Create profile options.
    profiles = []
    App::AWSProfile::get_profile_names.each do |profile|
        option = {
            :text  => profile,
            :value => profile
        }
        # Uncomment this if you want to disable current profile.
        # option[:disabled] = 'Current Profile' if profile == App::AWSProfile::get_profile_name
        profiles << option
    end

    # Display current profile.
    Blufin::Terminal::info("Current profile: #{Blufin::Terminal::format_highlight(App::AWSProfile::get_profile_name)}")

    # Prompt for new profile.
    new_profile = Blufin::Terminal::prompt_select('Select a profile', profiles)

    # Replace profile in config file.
    Blufin::Files::write_line_to_file(config_file, "DefaultProfile: #{new_profile}", /^\s*DefaultProfile:/, false, true)

    # Display new profile confirmation.
    Blufin::Terminal::success("Your new active profile is: #{Blufin::Terminal::format_highlight(new_profile)}", "All #{Blufin::Terminal::format_command('awx')} commands will now run against this profile.")

    # Re-link configuration file.
    if App::is_albert_mac
        config_file    = App::CONFIG_FILE
        symlinked_file = Blufin::Config::get['CustomOptions']['ConfigFilePath']
        Blufin::Terminal::error("File not found: #{Blufin::Terminal::format_invalid(symlinked_file)}", "A symlink to #{Blufin::Terminal::format_directory(config_file)} was broken and could not be re-created.") unless Blufin::Files::file_exists(symlinked_file)
        res    = []
        res[0] = Blufin::Terminal::execute("cp #{File.expand_path(config_file)} #{File.expand_path(symlinked_file)} ")
        res[1] = Blufin::Terminal::execute("ln -sfv #{File.expand_path(symlinked_file)} #{File.expand_path(config_file)}")
        puts
        puts "\x1B[38;5;240m     File successfully copied/symlinked.\x1B[0m" if res[0] && res[1]
        puts "\x1B[38;5;196m     Something went wrong. The cp/symlink command(s) did not return successful exit code(s).\x1B[0m" if !res[0] || !res[1]
        puts
    end

end

#opts_validateObject



23
24
25
# File 'lib/routes/switch.rb', line 23

def opts_validate

end