Class: DotenvToCI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DotenvToCI

Returns a new instance of DotenvToCI.



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

def initialize(options)
  @dotenvs = options[:dotenvs]
  @provider = options[:provider]
  @token = options[:token]
  @vcs = options[:vcs]
  @username = options[:username]
  @project = options[:project]
  @verbose = options[:verbose]

  load_from_env

  if verbose
    puts "Config: #{@token}"
  end
end

Instance Attribute Details

#dotenvsObject

Returns the value of attribute dotenvs.



7
8
9
# File 'lib/dotenv_to_ci.rb', line 7

def dotenvs
  @dotenvs
end

#projectObject

Returns the value of attribute project.



12
13
14
# File 'lib/dotenv_to_ci.rb', line 12

def project
  @project
end

#providerObject

Returns the value of attribute provider.



8
9
10
# File 'lib/dotenv_to_ci.rb', line 8

def provider
  @provider
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/dotenv_to_ci.rb', line 9

def token
  @token
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/dotenv_to_ci.rb', line 11

def username
  @username
end

#vcsObject

Returns the value of attribute vcs.



10
11
12
# File 'lib/dotenv_to_ci.rb', line 10

def vcs
  @vcs
end

#verboseObject

Returns the value of attribute verbose.



13
14
15
# File 'lib/dotenv_to_ci.rb', line 13

def verbose
  @verbose
end

Instance Method Details

#load_from_envObject



31
32
33
34
35
# File 'lib/dotenv_to_ci.rb', line 31

def load_from_env
  if @provider == "circleci"
    @token ||= ENV["CIRCLECI_TOKEN"]
  end
end

#parse_envs(dotenv_paths) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/dotenv_to_ci.rb', line 47

def parse_envs(dotenv_paths)
  envs = Dotenv.parse(dotenv_paths)

  if verbose
    puts "ENVS: #{envs}" 
  end

  return envs
end

#run!Object



37
38
39
40
41
# File 'lib/dotenv_to_ci.rb', line 37

def run!
  verify_options!
  envs = parse_envs(@dotenvs)
  send_to_circleci(envs)
end

#send_to_circleci(envs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dotenv_to_ci.rb', line 57

def send_to_circleci(envs)
  envs.each do |k,v|
    payload = {
      name: k,
      value: v 
    }

    if verbose
      puts "Setting: #{payload}"
    end

    resp = RestClient::Request.execute(
      method: :post,
      url: "https://circleci.com/api/v1.1/project/#{vcs}/#{username}/#{project}/envvar?circle-token=#{@token}",
      payload: payload.to_json,
      headers: { 'Content-Type' => 'application/json' }
    )
  end

  puts "Successfully added: #{envs.keys.join(', ')}"
end

#verify_options!Object



43
44
45
# File 'lib/dotenv_to_ci.rb', line 43

def verify_options!
  
end