mysticonfig 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +32 -0
- data/lib/mysticonfig/utils.rb +91 -0
- data/lib/mysticonfig/version.rb +4 -0
- data/lib/mysticonfig.rb +69 -0
- data/mysticonfig.gemspec +30 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8c5f9c4fb90ae354a44fa7482acf2fc3e3350735b3c11056041f27b6d521232c
|
4
|
+
data.tar.gz: af6ce7b30d055dbbd2607d45d22cfda485e73ce373fa06837c3b901ae562cc7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e8acfcf0d72f0542f291e143411462892dcfdf0d0380711665e457b9b24305053d385dc568b3eeee3ce7998fd97582b5fefe0718aa7f3ef1f406cbf277d6212f
|
7
|
+
data.tar.gz: 4b6c5f9f00cfa2dc809fae164e347e8833dca0a580d4d95a32d3b1e7a14c11303b23aa2256fc452721350a90631155c6155948d19143439c6bee0db089152118
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Saran Tanpituckpong
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# mysticonfig
|
2
|
+
[](./LICENSE)
|
3
|
+
[](https://rubygems.org/gems/mysticonfig)
|
4
|
+
[](https://rubygems.org/gems/mysticonfig)
|
5
|
+
[](https://gemnasium.com/github.com/gluons/mysticonfig)
|
6
|
+
[](https://travis-ci.org/gluons/mysticonfig)
|
7
|
+
<br><br>
|
8
|
+
<p align="center">
|
9
|
+
<strong>🔮 The configuration loader for wizard.</strong>
|
10
|
+
</p>
|
11
|
+
<br>
|
12
|
+
|
13
|
+
A library to load `.appnamerc`, `.appnamerc.json` or `.appnamerc.yaml` config file easily.
|
14
|
+
|
15
|
+
> Gem's name inspired by [cosmiconfig](https://github.com/davidtheclark/cosmiconfig)
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
```bash
|
20
|
+
gem install mysticonfig
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'mysticonfig'
|
27
|
+
|
28
|
+
loader = Mysticonfig::Loader.new 'appname'
|
29
|
+
config = loader.load # Automatically detect and load config
|
30
|
+
json_config = loader.load_json # Only load config from JSON file
|
31
|
+
yaml_config = loader.load_yaml # Only load config from YAML file (.yaml or .yml)
|
32
|
+
```
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Mystic Config
|
6
|
+
module Mysticonfig
|
7
|
+
##
|
8
|
+
# Utilies
|
9
|
+
module Utils
|
10
|
+
##
|
11
|
+
# Determine whether the given file is valid JSON file.
|
12
|
+
def self.json_file?(file)
|
13
|
+
return false if file.nil?
|
14
|
+
|
15
|
+
JSON.parse(File.read(file))
|
16
|
+
true
|
17
|
+
rescue
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Determine whether the given file is valid YAML file.
|
23
|
+
def self.yaml_file?(file)
|
24
|
+
return false if file.nil? || json_file?(file)
|
25
|
+
|
26
|
+
result = YAML.safe_load(File.read(file))
|
27
|
+
result.is_a?(Hash)
|
28
|
+
rescue
|
29
|
+
false
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# Generate config filenames from given app name.
|
34
|
+
def self.generate_config_filenames(appname)
|
35
|
+
return nil if appname.nil?
|
36
|
+
|
37
|
+
{
|
38
|
+
plain: ".#{appname}rc",
|
39
|
+
json: ".#{appname}rc.json",
|
40
|
+
yaml: [".#{appname}rc.yaml", ".#{appname}rc.yml"]
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Load JSON config file.
|
46
|
+
def self.load_json(config_file)
|
47
|
+
return {} if config_file.nil? || !File.exist?(config_file)
|
48
|
+
|
49
|
+
JSON.parse(File.read(config_file))
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Load YAML config file.
|
54
|
+
def self.load_yaml(config_file)
|
55
|
+
return {} if config_file.nil? || !File.exist?(config_file)
|
56
|
+
|
57
|
+
YAML.safe_load(File.read(config_file))
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# Load config file with automatic file type detection.
|
62
|
+
def self.load_auto(config_file)
|
63
|
+
return {} if config_file.nil? || !File.exist?(config_file)
|
64
|
+
|
65
|
+
if json_file?(config_file)
|
66
|
+
load_json(config_file)
|
67
|
+
elsif yaml_file?(config_file)
|
68
|
+
load_yaml(config_file)
|
69
|
+
else
|
70
|
+
{}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Look up for an existent config file.
|
76
|
+
def self.lookup_file(config_file, dir = Dir.pwd)
|
77
|
+
dir = File.realpath dir
|
78
|
+
current_filepath = File.expand_path(config_file, dir)
|
79
|
+
return current_filepath if File.exist?(current_filepath)
|
80
|
+
|
81
|
+
# Stop on home directory or root directory.
|
82
|
+
home_path = File.expand_path '~'
|
83
|
+
is_home = dir == home_path
|
84
|
+
is_root = dir == '/'
|
85
|
+
return nil if is_home || is_root
|
86
|
+
|
87
|
+
upper_path = File.realpath('..', dir)
|
88
|
+
lookup_file(config_file, upper_path) # Traverse up
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/mysticonfig.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative 'mysticonfig/utils'
|
2
|
+
|
3
|
+
##
|
4
|
+
# Mystic Config
|
5
|
+
module Mysticonfig
|
6
|
+
##
|
7
|
+
# Configuration loader.
|
8
|
+
class Loader
|
9
|
+
def initialize(appname)
|
10
|
+
@filenames = Utils.generate_config_filenames appname
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Find and load config file.
|
15
|
+
def load
|
16
|
+
config_file = find_file @filenames
|
17
|
+
|
18
|
+
Utils.load_auto config_file
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Find and load JSON config file.
|
23
|
+
def load_json
|
24
|
+
json_config_file = Utils.lookup_file @filenames[:json]
|
25
|
+
|
26
|
+
Utils.load_json json_config_file
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Find and load YAML config file.
|
31
|
+
def load_yaml
|
32
|
+
yaml_config_files = @filenames[:yaml]
|
33
|
+
yaml_config_file = nil
|
34
|
+
|
35
|
+
yaml_config_files.each do |file|
|
36
|
+
yaml_config_file = Utils.lookup_file file
|
37
|
+
return Utils.load_yaml(yaml_config_file) unless yaml_config_file.nil?
|
38
|
+
end
|
39
|
+
|
40
|
+
{} # Return empty hash when can't load config file
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# rubocop:disable Metrics/MethodLength
|
46
|
+
|
47
|
+
##
|
48
|
+
# Find an existent config file from all posible files.
|
49
|
+
def find_file(filenames)
|
50
|
+
return nil if filenames.nil?
|
51
|
+
|
52
|
+
filenames.each_value do |value|
|
53
|
+
if value.is_a? Array
|
54
|
+
value.each do |file|
|
55
|
+
file_path = Utils.lookup_file file
|
56
|
+
return file_path unless file_path.nil?
|
57
|
+
end
|
58
|
+
else
|
59
|
+
file_path = Utils.lookup_file value
|
60
|
+
return file_path unless file_path.nil?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
# rubocop:enable Metrics/MethodLength
|
68
|
+
end
|
69
|
+
end
|
data/mysticonfig.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path('./lib', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
$LOAD_PATH.push lib unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'mysticonfig/version'
|
6
|
+
|
7
|
+
DESCRIPTION = 'The configuration loader to load rc config file.'.freeze
|
8
|
+
|
9
|
+
Gem::Specification.new do |s|
|
10
|
+
s.name = 'mysticonfig'
|
11
|
+
s.version = Mysticonfig::VERSION
|
12
|
+
s.summary = 'The configuration loader for wizard.'
|
13
|
+
s.description = DESCRIPTION
|
14
|
+
s.files = ['mysticonfig.gemspec', 'LICENSE', 'Gemfile']
|
15
|
+
s.files += Dir['*.md', 'lib/**/*']
|
16
|
+
s.required_ruby_version = '>= 2.1.0'
|
17
|
+
s.author = 'Saran Tanpituckpong'
|
18
|
+
s.license = 'MIT'
|
19
|
+
s.homepage = 'https://github.com/gluons/mysticonfig'
|
20
|
+
s.metadata = {
|
21
|
+
'bug_tracker_uri' => 'https://github.com/gluons/mysticonfig/issues',
|
22
|
+
'homepage_uri' => 'https://gluons.github.io/mysticonfig/'
|
23
|
+
}
|
24
|
+
|
25
|
+
s.add_runtime_dependency 'json', '~> 2.1'
|
26
|
+
|
27
|
+
s.add_development_dependency 'rake', '~> 12.3'
|
28
|
+
s.add_development_dependency 'rspec', '~> 3.7'
|
29
|
+
s.add_development_dependency 'rubocop', '~> 0.52.1'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysticonfig
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Saran Tanpituckpong
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.52.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.52.1
|
69
|
+
description: The configuration loader to load rc config file.
|
70
|
+
email:
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- Gemfile
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
- lib/mysticonfig.rb
|
79
|
+
- lib/mysticonfig/utils.rb
|
80
|
+
- lib/mysticonfig/version.rb
|
81
|
+
- mysticonfig.gemspec
|
82
|
+
homepage: https://github.com/gluons/mysticonfig
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata:
|
86
|
+
bug_tracker_uri: https://github.com/gluons/mysticonfig/issues
|
87
|
+
homepage_uri: https://gluons.github.io/mysticonfig/
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.1.0
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.7.5
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: The configuration loader for wizard.
|
108
|
+
test_files: []
|