Class: Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/setup.rb

Constant Summary collapse

CONFIG =
"#{user_home}/.gitlab_config"

Class Method Summary collapse

Class Method Details

.checkObject

This does a check to see if we have a server or token set and if we don’t start the configureation process



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/setup.rb', line 40

def self.check
  if File.exist?(CONFIG) && self.valid_json
    JSON.parse(File.read(CONFIG))
    params = JSON.parse(File.read(CONFIG))
    unless params["gitlab_server"] && params["gitlab_token"]
      puts Rainbow("It looks like either your server or token is not set properly. Do you wish to add them now? ").yellow
      answer = STDIN.gets.chomp.downcase
      if answer == "y" or answer == "yes"
        self.configure
      end
    end
  else
    puts Rainbow("You do not have a valid config file. Do you wish to create one now and configure it? ").yellow
    answer = STDIN.gets.chomp.downcase
    if answer == "y" or answer == "yes"
      self.configure
    else
      puts Rainbow("Ok, but nothing is going to work till you do....\n").yellow
    end
  end
end

.configureObject

This configures the config file for gitlab settings.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/setup.rb', line 63

def self.configure
  if File.exist?(CONFIG)
    JSON.parse(File.read(CONFIG))
    params = JSON.parse(File.read(CONFIG))
  else
    params = {}
  end
  puts Rainbow("What is the name of your gitlab server?\nExample: http[s]://server.domain.tld:\nServer: ").purple
  answer = STDIN.gets.chomp.downcase
  answer = "#{answer}/api/v3"
  params["gitlab_server"] = answer
  params["gitlab_server"]
  puts Rainbow("What is your token?\nExample: 3pe14gZfap:\nToken: ").purple
  params["gitlab_token"] = STDIN.gets.chomp
  puts Rainbow("Do you wish to do the Github token now too? ").purple
  answer = STDIN.gets.chomp.downcase

  if answer == "y" or answer == "yes"
    puts Rainbow("What is your Github token? ").purple
    params["github_token"] = STDIN.gets.chomp
  end

  save = JSON.generate(params)
  puts Rainbow("\n\nUpdating config file with the following:").purple
  puts Rainbow("Gitlab server: #{params["gitlab_server"]}").purple
  puts Rainbow("Gitlab token: #{params["gitlab_token"]}").purple

  if params["github_token"]
    puts Rainbow("Github token: #{params["github_token"]}").purple
  end

  puts Rainbow("\nIs this information correct? ").yellow
  answer = STDIN.gets.chomp.downcase
  if answer == "y" or answer == "yes"
    config_file = File.open(CONFIG, "w")
    config_file.write(save)
    config_file.close
    puts Rainbow("Configuration saved.\n\n").green
    exit
  else
    self.configure
  end
end

.get_github_tokenObject

Get the token in the file



147
148
149
150
151
152
153
154
155
156
# File 'lib/setup.rb', line 147

def self.get_github_token
  unless Setup.github_precheck
    puts Rainbow("\n\nWhoops! Looks like we have not setup a github token yet...\n").yellow
    Setup.github_configure
  else
    JSON.parse(File.read(CONFIG))
    params = JSON.parse(File.read(CONFIG))
    return params["github_token"]
  end
end

.get_gitlabserverObject

Get the gitlabserver in the file



159
160
161
162
163
164
165
166
167
168
# File 'lib/setup.rb', line 159

def self.get_gitlabserver
  unless Setup.precheck
    puts Rainbow("\n\nWhoops! Looks like we have not setup a config before...\n").yellow
    Setup.configure
  else
    JSON.parse(File.read(CONFIG))
    params = JSON.parse(File.read(CONFIG))
    return params["gitlab_server"]
  end
end

.get_tokenObject

Get the token in the file



135
136
137
138
139
140
141
142
143
144
# File 'lib/setup.rb', line 135

def self.get_token
  unless Setup.precheck
    puts Rainbow("\n\nWhoops! Looks like we have not setup a config before...\n").yellow
    Setup.configure
  else
    JSON.parse(File.read(CONFIG))
    params = JSON.parse(File.read(CONFIG))
    return params["gitlab_token"]
  end
end

.github_configureObject

This configures the github token



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/setup.rb', line 108

def self.github_configure
  if self.precheck && !self.github_precheck
    params = JSON.parse(File.read(CONFIG))
    puts Rainbow("What is your Github token?\nExample: 3pe14gZfap:\nToken: ").purple
    params["github_token"] = STDIN.gets.chomp
    save = JSON.generate(params)
    puts Rainbow("\n\nUpdating config file with the following:
          Github token: #{params["github_token"]} \n\n").purple
    puts Rainbow("Is this information correct? ").yellow
    answer = STDIN.gets.chomp.downcase
    if answer == "y" or answer == "yes"
      config_file = File.open(CONFIG, "w")
      config_file.write(save)
      config_file.close
      puts Rainbow("Configuration saved.\n\n").green
      exit
    end
  else
    puts "Whoa! Looks like we don't have configuration file yet. Do you want to create one now?"
    answer = STDIN.gets.chomp.downcase
    if answer == "y" or answer == "yes"
      self.configure
    end
  end
end

.github_precheckObject

Precheck to see if we have a token yet in the file



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/setup.rb', line 16

def self.github_precheck
  if File.exist?(CONFIG) && self.valid_json
    JSON.parse(File.read(CONFIG))
    params = JSON.parse(File.read(CONFIG))
    if params["github_token"]
      return true
    else
      return false
    end
  else
    return false
  end
end

.precheckObject

Prehceck to see if we dont have a file or vaild json in the file



31
32
33
34
35
36
37
# File 'lib/setup.rb', line 31

def self.precheck
  if !File.exist?(CONFIG) or !self.valid_json
    return false
  else
    return true
  end
end

.valid_jsonObject

This checks to see if the json we are getting from the file is valid or not.



8
9
10
11
12
13
# File 'lib/setup.rb', line 8

def self.valid_json
  JSON.parse(File.read(CONFIG))
  return true
rescue JSON::ParserError
  return false
end