Class: Ghsync::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Config

Organization config

"name": "org",
"exclude": ["repo"],
"base_path": "org" #optional

Repository config

"owner": "owner",
"name": "repo",
"base_path": "repo" #optional



19
20
21
22
23
24
25
26
# File 'lib/ghsync/config.rb', line 19

def initialize(config)
  @organizations = config["organizations"] || []
  @repositories = config["repositories"] || []
  @username = config["username"]
  @password = config["password"]
  @base_path = config["base_path"]
  import_from_pra if config["use_pra"]
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/ghsync/config.rb', line 5

def base_path
  @base_path
end

#organizationsObject (readonly)

Returns the value of attribute organizations.



5
6
7
# File 'lib/ghsync/config.rb', line 5

def organizations
  @organizations
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/ghsync/config.rb', line 5

def password
  @password
end

#repositoriesObject (readonly)

Returns the value of attribute repositories.



5
6
7
# File 'lib/ghsync/config.rb', line 5

def repositories
  @repositories
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/ghsync/config.rb', line 5

def username
  @username
end

Class Method Details

.config_pathObject



61
62
63
64
65
# File 'lib/ghsync/config.rb', line 61

def self.config_path
  if File.exists?(File.join(self.users_home_directory, '.ghsync', 'config.json'))
    return File.join(self.users_home_directory, '.ghsync', 'config.json')
  end
end

.create(import_from_pra: true) ⇒ Object



28
29
# File 'lib/ghsync/config.rb', line 28

def self.create(import_from_pra: true)
end

.import_from_praObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/ghsync/config.rb', line 31

def self.import_from_pra
  pra_config = Config.parse_pra_config_file
  github_config = pra_config["pull_sources"].select {|source| source["type"] == "github"}.first
  unless github_config.nil?
    @organizations += github_config["config"]["organizations"]
    @repositories += github_config["config"]["repositories"]
    @username ||= github_config["config"]["username"]
    @password ||= github_config["config"]["password"]
  end
end

.json_parse(content) ⇒ Object



79
80
81
# File 'lib/ghsync/config.rb', line 79

def self.json_parse(content)
  return JSON.parse(content)
end

.load_configObject



42
43
44
# File 'lib/ghsync/config.rb', line 42

def self.load_config
  return self.new(self.parse_config_file)
end

.parse_config_fileObject



46
47
48
# File 'lib/ghsync/config.rb', line 46

def self.parse_config_file
  self.json_parse(self.read_config_file(config_path))
end

.parse_pra_config_fileObject



50
51
52
# File 'lib/ghsync/config.rb', line 50

def self.parse_pra_config_file
  self.json_parse(self.read_config_file(pra_config_path))
end

.pra_config_pathObject



67
68
69
70
71
72
73
# File 'lib/ghsync/config.rb', line 67

def self.pra_config_path
  if File.exists?(File.join(self.users_home_directory, '.pra', 'config.json'))
    return File.join(self.users_home_directory, '.pra', 'config.json')
  else
    return File.join(self.users_home_directory, '.pra.json')
  end
end

.read_config_file(path) ⇒ Object



54
55
56
57
58
59
# File 'lib/ghsync/config.rb', line 54

def self.read_config_file(path)
  file = File.open(path, "r")
  contents = file.read
  file.close
  return contents
end

.users_home_directoryObject



75
76
77
# File 'lib/ghsync/config.rb', line 75

def self.users_home_directory
  return ENV['HOME']
end