Class: Skylight::CLI::Merger

Inherits:
Thor::Group show all
Includes:
Helpers
Defined in:
lib/skylight/cli/merger.rb

Constant Summary collapse

STRINGS =
{
  get_token: "get your merge token from `https://www.skylight.io/merging`",
  unlisted: "My app isn't listed here :("
}.freeze

Instance Attribute Summary

Attributes included from Thor::Base

#args, #options, #parent_options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Thor::Group

class_options_help, desc, get_options_from_invocations, handle_argument_error, help, invocation_blocks, invocations, invoke, invoke_from_option, printable_commands, remove_invocation

Methods included from Thor::Base

included, #initialize, register_klass_file, shell, shell=, subclass_files, subclasses

Class Method Details



10
11
12
# File 'lib/skylight/cli/merger.rb', line 10

def self.banner
  "#{basename} skylight merge MERGE_TOKEN"
end

Instance Method Details

#ask_for_child_appObject



68
69
70
71
# File 'lib/skylight/cli/merger.rb', line 68

def ask_for_child_app
  say "Please specify the child app to be merged into the parent app as an Environment.", :green
  @child_app = ask_for_app(children, &method(:format_component))
end

#ask_for_child_envObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/skylight/cli/merger.rb', line 77

def ask_for_child_env
  say "\nWhat environment is the child app?\n" \
        "In many cases, this will be equivalent to the Rails " \
        "environment, i.e., `development`.",
      :green

  say "1. development"
  say "2. staging"
  say "3. [choose a different environment not listed here]"

  i = ask("\nWhich number?").chomp.to_i

  @child_env =
    case i
    when 1
      "development"
    when 2
      "staging"
    when 3
      specify_child_env
    else
      say("\nEh? Please enter 1, 2, or 3.", :red)
      ask_for_child_env
    end
end

#ask_for_parent_appObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/skylight/cli/merger.rb', line 49

def ask_for_parent_app
  @parents ||=
    begin
      a = (@apps + [{ name: STRINGS[:unlisted], components: [], unlisted: true }])
      a.each_with_object({}).with_index { |(app, h), i| h[i + 1] = wrap_hash(app) }
    end

  say "\nLet's begin!\n\n" \
        "Please specify the \"parent\" app.\n" \
        "In most cases, this will be the production app handling web requests.",
      :green

  @parent_app = ask_for_app(@parents)
end

#confirm_childObject



73
74
75
# File 'lib/skylight/cli/merger.rb', line 73

def confirm_child
  say "\nOk! The child app is: #{set_color(format_component(@child_app), :yellow)}", :green
end

#confirm_child_envObject



103
104
105
# File 'lib/skylight/cli/merger.rb', line 103

def confirm_child_env
  say "\nOk! The child environment will be: #{set_color(@child_env, :yellow)}"
end

#confirm_everythingObject



107
108
109
110
# File 'lib/skylight/cli/merger.rb', line 107

def confirm_everything
  say "\nOk! Now we're going to merge `#{set_color(format_component(@child_app), :yellow)}` " \
        "into `#{set_color(@parent_app.name, :green)}` as `#{set_color(@child_env, :yellow)}`."
end

#confirm_parentObject



64
65
66
# File 'lib/skylight/cli/merger.rb', line 64

def confirm_parent
  say "\nOk! The parent app is: #{@parent_app.name}", :green
end

#do_confirmObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/skylight/cli/merger.rb', line 112

def do_confirm
  proceed = ask("Proceed? [Y/n]", :yellow).chomp

  case proceed.upcase
  when "Y", ""
    do_merge
  when "N"
    done!(success: true, message: "Ok, come back any time.")
  else
    say("Please respond 'Y' to merge or 'n' to cancel.", :red)
    do_confirm
  end
end

#fetch_appsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/skylight/cli/merger.rb', line 29

def fetch_apps
  say "\nFetching your apps from skylight.io..."
  @apps = api.fetch_mergeable_apps(@merge_token).body

  if @apps.count < 2
    done!(
      message: "It does not appear that you are the owner of enough apps (there's nothing we can merge).",
      success: false
    )
  end
rescue Skylight::Api::Unauthorized
  done!(
    success: false,
    message:
      "Provided merge token is invalid.\n" \
        "Please #{STRINGS[:get_token]}" \
        "and run `skylight merge <merge token>` again."
  )
end


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/skylight/cli/merger.rb', line 126

def print_new_config_instructions
  say "\nSuccess!\n", :green

  say "=======================================================\n", :yellow

  say "IMPORTANT!\n" \
        "If you use a config/skylight.yml file to configure Skylight:\n",
      :yellow

  say "The #{@child_env} environment for the #{@parent_app.name} app\n" \
        "will now connect using the default authentication token for the app.\n" \
        "Remove any environment-specific `authentication` configs from the\n" \
        "#{@parent_app.name} #{@child_env} environment.\n",
      :yellow

  say "If you're running in Rails and your Rails environment exactly matches `#{@child_env}`,\n" \
        "we will automatically detect and report that environment when your agent connects.\n" \
        "Otherwise, you should set `env: '#{@child_env}'` as environment-specific configuration for\n" \
        "#{@child_env}'s Rails environment. For example:\n" \
        "```yml\n" \
        "staging:\n" \
        "  env: staging-42\n" \
        "```\n",
      :yellow

  say "=======================================================\n", :yellow

  say "IMPORTANT!\n" \
        "If you configure Skylight using environment variables:\n",
      :yellow

  say "Deploy the latest agent before updating your environment variables.\n", :yellow

  say "The #{@child_env} environment for the #{@parent_app.name} app\n" \
        "will now connect using the default authentication token for the app.\n" \
        "Set `SKYLIGHT_AUTHENTICATION` in the #{@child_env} environment to the\n" \
        "#{@parent_app.name} app's authentication token.\n",
      :yellow

  say "If you're running in Rails and your Rails environment exactly matches `#{@child_env}`,\n" \
        "we will automatically detect and report that environment when your agent connects.\n" \
        "Otherwise, you should set `SKYLIGHT_ENV=#{@child_env}` when running in this environment.\n",
      :yellow

  say "=======================================================", :yellow

  done!
end

#welcomeObject



21
22
23
24
25
26
27
# File 'lib/skylight/cli/merger.rb', line 21

def welcome
  say "\nHello! Welcome to the `skylight merge` CLI!\n", :green

  say "This CLI is for Skylight users who already have Skylight Environments set up\n" \
        "using the legacy method of creating a separate Skylight app per environment.\n" \
        "Use this CLI to merge legacy environment apps into their parent apps as Environments."
end