#!/usr/bin/env ruby

# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))

require 'gitlab/cng/cli'

Signal.trap('INT') do
  warn("\n#{caller.join("\n")}: interrupted")
  exit(1)
end

module Cng
  class << self
    def run
      Gitlab::Cng::Helpers::Output.force_color! if force_color?

      Gitlab::Cng::CLI.start
    rescue Gitlab::Cng::CLI::Error => e
      puts "ERROR: #{e.message}"
      exit 1
    end

    private

    def force_color?
      ENV["CNG_FORCE_COLOR"]&.match?(/^(true|yes|1)$/i) || %w[--force-color --force-color=true].any? do |arg|
        ARGV.include?(arg)
      end
    end
  end
end

Cng.run
