class Envirobly::Cli::Main

def deploy(environ_name = nil)

def deploy(environ_name = nil)
  commit = Envirobly::Git::Commit.new options.commit
  unless commit.exists?
    say_error "Commit '#{commit.ref}' doesn't exist in this repository"
    exit 1
  end
  Envirobly::AccessToken.new(shell:).require!
  deployment = Envirobly::Deployment.new(
    account_id: options.account_id,
    region: options.region,
    project_id: options.project_id,
    project_name: options.project_name,
    environ_name: environ_name.presence || commit.current_branch,
    commit:,
    shell:
  )
  deployment.perform(dry_run: options.dry_run)
end

def exec(service_name, *command)

def exec(service_name, *command)
  Envirobly::ContainerShell.new(service_name, options).exec(command)
end

def instance_types(region = nil)

def instance_types(region = nil)
  default_region = Envirobly::Defaults::Region.new(shell:)
  region = region.presence || default_region.require_if_none
  api = Envirobly::Api.new
  table_data = api.list_instance_types(region).object.map do |item|
    [
      item["code"],
      item["vcpu"],
      Envirobly::Numeric.new(item["memory"], short: true),
      Envirobly::Numeric.new(item["monthly_price"]),
      item["group"]
    ]
  end
  print_table [ [ "Name", "vCPU", "Memory (GB)", "Monthly price ($)", "Group" ] ] +
    table_data, borders: true
end

def pull(region, bucket, ref, path)

def pull(region, bucket, ref, path)
  Envirobly::Duration.measure("Build context download took %s") do
    Envirobly::Aws::S3.new(region:, bucket:).pull ref, path
  end
end

def rsync(source, destination)

def rsync(source, destination)
  service_name = nil
  [ source, destination ].each do |path|
    if path =~ /\A([a-z0-9\-_]+):/i
      service_name = $1
      break
    end
  end
  Envirobly::ContainerShell.new(service_name, options).rsync(source, destination)
end

def set_default_account

def set_default_account
  Envirobly::Defaults::Account.new(shell:).require_value
end

def set_default_region

def set_default_region
  Envirobly::Defaults::Region.new(shell:).require_value
end

def signin

def signin
  access_token = Envirobly::AccessToken.new(shell:)
  access_token.set
end

def signout

def signout
  Envirobly::AccessToken.destroy
  say "You've signed out."
  say "This didn't delete the access token itself."
  say "You can sign in again with `envirobly signin`."
end

def validate

def validate
  Envirobly::AccessToken.new(shell:).require!
  configs = Envirobly::Config.new
  api = Envirobly::Api.new
  params = { validation: configs.to_params }
  response = api.validate_shape params
  if response.object.fetch("valid")
    puts "Config is valid #{green_check}"
  else
    display_config_errors response.object.fetch("errors")
    exit 1
  end
end

def version

def version
  if options.pure
    puts Envirobly::VERSION
  else
    puts "envirobly CLI v#{Envirobly::VERSION}"
  end
end