Class: Sumcli::Commands::Add::Service

Inherits:
Sumcli::Command show all
Defined in:
lib/sumcli/commands/add/service.rb,
lib/sumcli/commands/add/service/postgres.rb

Defined Under Namespace

Classes: Postgres

Constant Summary collapse

DOCKER_FILE =
'docker-compose.yml'
TEMPLATES_PATH =
File.expand_path('../../templates/add/service', __dir__)
CONFIGS_PATH =
'config'

Instance Method Summary collapse

Methods inherited from Sumcli::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(name, version, options) ⇒ Service

Returns a new instance of Service.



14
15
16
17
18
# File 'lib/sumcli/commands/add/service.rb', line 14

def initialize(name, version, options)
  @name = name
  @version = version
  @options = options
end

Instance Method Details

#add_postgresObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sumcli/commands/add/service.rb', line 57

def add_postgres
  ver = '10'
  response = ask_to_set(ver) if ver != @version && !@version.nil?
  ver = @version if response

  content = 
"  db:\n    image: 'postgres:\#{ver}'\n    ports:\n      - '5432'\n"

  generator.safe_inject_into_file DOCKER_FILE, content, after: "services:\n"
  copy_database_files
end

#add_redisObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sumcli/commands/add/service.rb', line 74

def add_redis
  content = 
"  redis:\n    image: 'redis:\#{@version}'\n    ports:\n      - '6379'\n"

  generator.safe_inject_into_file DOCKER_FILE, content, after: "services:\n"
end

#ask_to_set(ver) ⇒ Object



90
91
92
93
# File 'lib/sumcli/commands/add/service.rb', line 90

def ask_to_set(ver)
  prompt.yes?("The default version of #{@name} is #{ver}." + 
    "\n\nDo you want to add version #{@version}?")
end

#copy_database_filesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sumcli/commands/add/service.rb', line 29

def copy_database_files
  generator.inject_into_file('Gemfile', after: "# GEMS\n\n") do "    gem 'pg', '~> 1.1'\n    gem \"otr-activerecord\", '~> 1.3.0'\n    RUBY\n  end\n  generator.copy_file(\"\#{TEMPLATES_PATH}/database.yml\", \"\#{CONFIGS_PATH}/database.yml\")\n  generator.copy_file(\"\#{TEMPLATES_PATH}/database.yml.ctmpl\", \"\#{CONFIGS_PATH}/database.yml.ctmpl\")\n  generator.copy_file(\"\#{TEMPLATES_PATH}/database.rb\", \"\#{CONFIGS_PATH}/initializers/database.rb\")\n  generator.inject_into_file(\"\#{CONFIGS_PATH}/application.rb\", after: \"require_rel '../api'\\n\") do\n    'require_rel \\'initializers\\''\n  end\n  generator.inject_into_file('Rakefile', after: \"# TASKS\\n\") do <<~RUBY.strip\n    load \"tasks/otr-activerecord.rake\"\n\n    namespace :db do\n      task :environment do\n        require_relative 'config/environment'\n      end\n    end\n    RUBY\n  end\nend\n"

#execute(input: $stdin, output: $stdout) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/sumcli/commands/add/service.rb', line 20

def execute(input: $stdin, output: $stdout)
  if !service_exists?
    self.send("add_#{@name}")
    output.puts "\n  #{@name} added to docker-compose"
  else
    output.puts "\n  #{@name} already added to docker-compose"
  end
end

#require_exists?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sumcli/commands/add/service.rb', line 53

def require_exists?
  File.readlines(APPLICATION_FILE).grep(/#{INITIALIZERS_REQUIRE}/).any?
end

#service_exists?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/sumcli/commands/add/service.rb', line 86

def service_exists?
  File.readlines(DOCKER_FILE).grep(/#{@name}/).any?
end