Commit bfbf61e4 authored by Matija Čupić's avatar Matija Čupić
Browse files

Merge branch 'ashmckenzie/fix-broken-gdk-init' into 'main'

Fix broken gdk init in origin/main

See merge request gitlab-org/gitlab-development-kit!2022
parents 94bd1c7f 5b380455
Loading
Loading
Loading
Loading
+90 −18
Original line number Diff line number Diff line
@@ -17,24 +17,30 @@ module GDK
  ROOT_CHECK_FILE = '.gdk-install-root'
  DEFAULT_INIT_DIRECTORY = File.join(Dir.pwd, 'gitlab-development-kit')

  def self.launcher_main
    if print_version?
      puts VERSION

      return true
  module CommandBasic
    class Main
      def initialize(args)
        @args = args
      end

    unless gdk_dir?
      warn_not_gdk_dir

      return false
    end
      def run
        return GDK::CommandBasic::Version.new.run if gdk_version?
        return GDK::CommandBasic::Init.new(args[1..]).run if gdk_init?

    load(File.join(gdk_root, 'lib/gdk.rb'))
        if gdk_dir?
          require(File.join(gdk_root, 'lib/gdk.rb'))
          GDK.main
        else
          warn_not_gdk_dir
          false
        end
      end

  def self.warn_not_gdk_dir
      private

      attr_reader :args

      def warn_not_gdk_dir
        warn <<~NOT_A_GDK_DIR

          The current working directory is not inside a gitlab-development-kit
@@ -46,19 +52,24 @@ module GDK
        NOT_A_GDK_DIR
      end

  def self.gdk_root
      def gdk_root
        @gdk_root ||= find_root(Dir.pwd)
      end

  def self.gdk_dir?
      def gdk_dir?
        !gdk_root.nil?
      end

  def self.print_version?
    %w[version --version].include?(ARGV.first) && !gdk_dir?
      def gdk_version?
        # If gdk_dir? == true, fall through to allow lib/gdk.rb to handle
        %w[version --version].include?(args.first) && !gdk_dir?
      end

  def self.find_root(current)
      def gdk_init?
        args.first == 'init'
      end

      def find_root(current)
        if File.exist?(File.join(current, 'GDK_ROOT'))
          File.realpath(current)
        elsif File.realpath(current) == '/'
@@ -69,4 +80,65 @@ module GDK
      end
    end

exit(GDK.launcher_main)
    class Version
      def run
        puts GDK::VERSION
        true
      end
    end

    class Init
      def initialize(args)
        @args = args
      end

      def run
        warn("INFO: 'gdk init' is deprecated and will be removed in a future update.")

        if show_help?
          puts('Usage: gdk init [DIR]')

          return true
        end

        directory = new_gdk_directory
        if new_gdk_directory_invalid?(directory)
          warn("ERROR: The proposed new GDK directory '#{directory}' is invalid because it begins with a dash.")

          return false
        end

        if clone_gdk(directory)
          puts("INFO: Successfully git cloned the GDK into '#{directory}'.")
          true
        else
          warn("ERROR: An error occurred while attempting to git clone the GDK into '#{directory}'.")
          false
        end
      end

      private

      attr_reader :args

      def show_help?
        args.count > 1 || (args & %w[-help --help]).any?
      end

      def new_gdk_directory_invalid?(directory)
        directory.start_with?('-')
      end

      def new_gdk_directory
        args.count == 1 ? args.first : GDK::DEFAULT_INIT_DIRECTORY
      end

      def clone_gdk(directory)
        cmd = "git clone https://gitlab.com/gitlab-org/gitlab-development-kit.git #{directory}"
        system(*cmd)
      end
    end
  end
end

exit(GDK::CommandBasic::Main.new(ARGV).run)
+1 −1
Original line number Diff line number Diff line
# frozen_string_literal: true

module GDK
  GEM_VERSION = '0.2.13'
  GEM_VERSION = '0.2.14'
  VERSION = "GitLab Development Kit #{GEM_VERSION}"
end
+2 −0
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ module GDK
      exit(GDK::Command::Restart.new.run(ARGV))
    when 'stop'
      exit(GDK::Command::Stop.new.run(ARGV))
    when /-{0,2}version/
      GDK::Command::Version.new.run(ARGV)
    when /-{0,2}help/, '-h', nil
      GDK::Command::Help.new.run(ARGV)
    else
+0 −2
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ module GDK
    autoload :Env, 'gdk/command/env'
    autoload :Doctor, 'gdk/command/doctor'
    autoload :Help, 'gdk/command/help'
    autoload :Init, 'gdk/command/init'
    autoload :Install, 'gdk/command/install'
    autoload :MeasureBase, 'gdk/command/measure_base'
    autoload :MeasureUrl, 'gdk/command/measure_url'
@@ -38,7 +37,6 @@ module GDK
      'diff-config' => -> { GDK::Command::DiffConfig },
      'doctor' => -> { GDK::Command::Doctor },
      'env' => -> { GDK::Command::Env },
      'init' => -> { GDK::Command::Init },
      'install' => -> { GDK::Command::Install },
      'help' => -> { GDK::Command::Help },
      'measure' => -> { GDK::Command::MeasureUrl },

lib/gdk/command/init.rb

deleted100644 → 0
+0 −61
Original line number Diff line number Diff line
# frozen_string_literal: true

module GDK
  module Command
    # Handles `gdk init` command execution
    #
    # @deprecated GDK init command has been deprecated should be removed in a future update
    class Init < BaseCommand
      DEFAULT_INIT_DIRECTORY = 'gitlab-development-kit'

      def run(args = [])
        GDK::Output.info("'gdk init' is deprecated and will be removed in a future update.")

        if show_help?(args)
          GDK::Output.puts('Usage: gdk init [DIR]')

          return true
        end

        directory = new_gdk_directory(args)
        if new_gdk_directory_invalid?(directory)
          GDK::Output.error('The GDK directory cannot start with a dash.')

          return false
        end

        if clone_gdk(directory)
          GDK::Output.success("Successfully git cloned the GDK into '#{directory}'.")

          true
        else
          GDK::Output.error("An error occurred while attempting to git clone the GDK into '#{directory}'.")

          false
        end
      end

      private

      def show_help?(args)
        args.count > 1 || (args & %w[-help --help]).any?
      end

      def new_gdk_directory_invalid?(directory)
        directory.start_with?('-')
      end

      def new_gdk_directory(args)
        args.count == 1 ? args.first : DEFAULT_INIT_DIRECTORY
      end

      def clone_gdk(directory)
        cmd = "git clone https://gitlab.com/gitlab-org/gitlab-development-kit.git #{directory}"
        sh = Shellout.new(cmd)
        sh.stream

        sh.success?
      end
    end
  end
end
Loading