Class: Chagall::Settings

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chagall/settings.rb

Constant Summary collapse

CHAGALL_PROJECTS_FOLDER =
"~/projects"
OPTIONS =
[
  {
    key: :log_level,
    flags: [ "--log-level" ],
    description: "Log level",
    type: :string,
    default: "info",
    environment_variable: "CHAGALL_LOG_LEVEL"
  },
  {
    key: :skip_uncommit,
    flags: [ "--skip-uncommit" ],
    description: "Skip uncommitted changes check",
    type: :boolean,
    default: false,
    environment_variable: "CHAGALL_SKIP_UNCOMMIT"
  },
  {
    key: :server,
    flags: [ "-s", "--server" ],
    description: "Server to deploy to",
    type: :string,
    required: true,
    environment_variable: "CHAGALL_SERVER"
  },
  {
    key: :name,
    flags: [ "-n", "--name" ],
    description: "Project name",
    type: :string,
    default: Pathname.new(Dir.pwd).basename.to_s,
    environment_variable: "CHAGALL_NAME"
  },
  {
    key: :release,
    flags: [ "--release" ],
    description: "Release tag",
    required: true,
    default: `git rev-parse --short HEAD`.strip,
    type: :string,
    environment_variable: "CHAGALL_RELEASE"
  },
  {
    key: :dry_run,
    type: :boolean,
    default: false,
    flags: [ "-d", "--dry-run" ],
    environment_variable: "CHAGALL_DRY_RUN",
    description: "Dry run"
  },
  {
    key: :remote,
    flags: [ "-r", "--remote" ],
    description: "Deploy remotely (build on remote server)",
    type: :boolean,
    default: false,
    environment_variable: "CHAGALL_REMOTE"
  },
  {
    key: :compose_files,
    flags: [ "-c", "--compose-files" ],
    description: "Comma separated list of compose files",
    type: :array,
    required: true,
    environment_variable: "CHAGALL_COMPOSE_FILES",
    proc: ->(value) { value.split(",") }
  },
  {
    key: :target,
    type: :string,
    default: "production",
    flags: [ "--target" ],
    environment_variable: "CHAGALL_TARGET",
    description: "Target"
  },
  {
    key: :dockerfile,
    type: :string,
    flags: [ "-f", "--dockerfile" ],
    default: "Dockerfile",
    environment_variable: "CHAGALL_DOCKERFILE",
    description: "Dockerfile"
  },
  {
    key: :projects_folder,
    type: :string,
    default: CHAGALL_PROJECTS_FOLDER,
    flags: [ "-p", "--projects-folder" ],
    environment_variable: "CHAGALL_PROJECTS_FOLDER",
    description: "Projects folder"
  },
  {
    key: :cache_from,
    type: :string,
    default: "tmp/.buildx-cache",
    flags: [ "--cache-from" ],
    environment_variable: "CHAGALL_CACHE_FROM",
    description: "Cache from"
  },
  {
    key: :cache_to,
    type: :string,
    default: "tmp/.buildx-cache-new",
    flags: [ "--cache-to" ],
    environment_variable: "CHAGALL_CACHE_TO",
    description: "Cache to"
  },
  {
    key: :keep_releases,
    type: :integer,
    default: 3,
    flags: [ "-k", "--keep-releases" ],
    environment_variable: "CHAGALL_KEEP_RELEASES",
    description: "Keep releases",
    proc: ->(value) { Integer(value) }
  },
  {
    key: :ssh_args,
    type: :string,
    default: "-o StrictHostKeyChecking=no",
    environment_variable: "CHAGALL_SSH_ARGS",
    flags: [ "--ssh-args" ],
    description: "SSH arguments"
  },
  {
    key: :docker_context,
    type: :string,
    flags: [ "--docker-context" ],
    environment_variable: "CHAGALL_DOCKER_CONTEXT",
    default: ".",
    description: "Docker context"
  },
  {
    key: :platform,
    type: :string,
    flags: [ "--platform" ],
    environment_variable: "CHAGALL_PLATFORM",
    default: "linux/x86_64",
    description: "Platform"
  }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#missing_compose_filesObject

Returns the value of attribute missing_compose_files.



10
11
12
# File 'lib/chagall/settings.rb', line 10

def missing_compose_files
  @missing_compose_files
end

#missing_optionsObject

Returns the value of attribute missing_options.



10
11
12
# File 'lib/chagall/settings.rb', line 10

def missing_options
  @missing_options
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/chagall/settings.rb', line 10

def options
  @options
end

Class Method Details

.[](key) ⇒ Object



159
160
161
# File 'lib/chagall/settings.rb', line 159

def [](key)
  instance.options[key]
end

.configure(argv) ⇒ Object



155
156
157
# File 'lib/chagall/settings.rb', line 155

def configure(argv)
  instance.configure(argv)
end

Instance Method Details

#configure(parsed_options) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/chagall/settings.rb', line 164

def configure(parsed_options)
  @options = parsed_options
  @missing_options = []
  @missing_compose_files = []

  validate_options
end

#image_tagObject



203
204
205
# File 'lib/chagall/settings.rb', line 203

def image_tag
  @image_tag ||= "#{options[:name]}:#{options[:release]}"
end

#project_folder_pathObject



207
208
209
# File 'lib/chagall/settings.rb', line 207

def project_folder_path
  @project_folder_path ||= "#{options[:projects_folder]}/#{options[:name]}"
end

#validate_optionsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/chagall/settings.rb', line 172

def validate_options
  error_message_string = "\n"

  OPTIONS.each do |option|
    @missing_options << option if option[:required] && @options[option[:key]].to_s.empty?
  end

  if @missing_options.any?
    error_message_string += "  Missing required options: #{@missing_options.map { |o| o[:key] }.join(', ')}\n"
    error_message_string += "    These can be set via:\n"
    error_message_string += "      - CLI arguments (#{@missing_options.map { |o| o[:flags] }.join(', ')})\n"
    error_message_string += "      - Environment variables (#{@missing_options.map do |o|
      o[:environment_variable] || o[:env_name]
    end.join(', ')})\n"
    error_message_string += "      - chagall.yml file\n"
  end

  if @options[:compose_files]
    @options[:compose_files].each do |file|
      unless File.exist?(file)
        @missing_compose_files << file
        error_message_string += "  Missing compose file: #{file}\n"
      end
    end
  end

  return unless @missing_options.any? || @missing_compose_files.any?

  raise Chagall::SettingsError, error_message_string unless @options[:dry_run]
end