Class: Gitlab::Lint::Client::Args

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/lint/client/args.rb

Constant Summary collapse

API_PATH =
"/api/v4/ci/lint"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgs

Returns a new instance of Args.



18
19
20
# File 'lib/gitlab/lint/client/args.rb', line 18

def initialize()
    @version = Gitlab::Lint::Client::VERSION
end

Instance Attribute Details

#baseUrlObject (readonly)

Returns the value of attribute baseUrl.



13
14
15
# File 'lib/gitlab/lint/client/args.rb', line 13

def baseUrl
  @baseUrl
end

#pathToYamlFileObject (readonly)

Returns the value of attribute pathToYamlFile.



14
15
16
# File 'lib/gitlab/lint/client/args.rb', line 14

def pathToYamlFile
  @pathToYamlFile
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



15
16
17
# File 'lib/gitlab/lint/client/args.rb', line 15

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/gitlab/lint/client/args.rb', line 16

def url
  @url
end

Instance Method Details

#get(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab/lint/client/args.rb', line 22

def get(args)
    options = {}
    optparse = OptionParser.new do|opt|
        opt.banner = 'Usage: validate-gitlab-ci [options]'
    
        opt.on('-f', '--yaml=YAML-PATH', 'Path to .gitlab-ci.yml') { |o| options[:yamlFile] = o }
        opt.on('-u', '--base-url=BASE_URL', 'GitLab API url') { |o| options[:baseUrl] = o }

        options[:timeout] = 10
        opt.on('-t', '--timeout[=TIMEOUT]', Integer, 'Api timeout in seconds') { |o| options[:timeout] = o }
    
        opt.on('-v', '--version', 'Program version') { |o| options[:version] = version() }
    end
    
    begin
        optparse.parse!(args)
        
        if options[:version].nil?
            mandatory = [:yamlFile, :baseUrl]
            missing = mandatory.select{ |param| options[param].nil? }
            if not missing.empty?
                STDERR.puts "Required options #{missing[0]} are missing: #{missing.join(", ")}"
                puts optparse.help
                abort("Exiting due to error encountered while parsing arguments")
            end
        end

    rescue OptionParser::InvalidOption, OptionParser::MissingArgument => error
        STDERR.puts error
        puts optparse
        abort("Exiting due to error encountered while parsing arguments...")
    end

    self.validateUrl!(options[:baseUrl])
    self.validateYamlFile!(options[:yamlFile])

    @baseUrl = options[:baseUrl]
    @url = options[:baseUrl] + API_PATH
    @pathToYamlFile = options[:yamlFile]
    @timeout = options[:timeout]
end

#validateUrl!(url) ⇒ Object

Raises:

  • (URI::InvalidURIError)


64
65
66
67
68
69
# File 'lib/gitlab/lint/client/args.rb', line 64

def validateUrl!(url)
    uri = URI.parse(url)
    valid = uri.is_a?(URI::HTTPS) && !uri.host.nil?
  
    raise URI::InvalidURIError unless valid
end

#validateYamlFile!(path) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
# File 'lib/gitlab/lint/client/args.rb', line 71

def validateYamlFile!(path)
    raise ArgumentError unless path.chars.last(4).join == ".yml" or path.chars.last(5).join == ".yaml"
    raise IOError unless ::File.exist?(path)
    raise RuntimeError unless ::File.readable?(path)
end

#versionObject



77
78
79
80
81
# File 'lib/gitlab/lint/client/args.rb', line 77

def version()
    string = "GitLab Lint Client Version: #{@version}"
    puts string
    exit 0
end