Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 1760e0a

Browse files
authoredAug 21, 2021
Merge pull request #1 from codenamephp/main
Initial release
2 parents 5afa8dc + 264e2c5 commit 1760e0a

25 files changed

+578
-4
lines changed
 

‎.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ jobs:
3838
- name: IntegrationTests
3939
env:
4040
KITCHEN_LOCAL_YAML: kitchen.dokken.yml
41-
run: chef exec kitchen test
41+
run: chef exec kitchen test -c 5

‎README.md

+140
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,142 @@
11
# Chef Cookbook
22
[![CI](https://github.com/codenamephp/chef.cookbook.gitClient/actions/workflows/ci.yml/badge.svg)](https://github.com/codenamephp/chef.cookbook.gitClient/actions/workflows/ci.yml)
3+
4+
## Usage
5+
6+
Just include this cookbook in a wrapper cookbook and use the resources as you see fit. Just make sure you install git before you use any of the resources that use it
7+
since they assume it is installed. You can of course use the `codenamephp_git_client_package` resource for this.
8+
9+
## Resources
10+
### package
11+
The `codenamephp_git_client_package` resource installs the package using a package manager.
12+
13+
#### Actions
14+
- `:install`: Installs the git client using a package manager.
15+
16+
#### Properties
17+
- `package_name`: The name of the package to install, defaults to `'git'`
18+
19+
#### Examples
20+
```ruby
21+
# Minimal properties
22+
codenamephp_git_client_package 'Install git'
23+
24+
# With custom package name
25+
codenamephp_git_client_package 'Install git' do
26+
package_name 'some package'
27+
end
28+
```
29+
30+
### config
31+
The `codenamephp_git_client_config` resource is used to set configurations through `git config`. It supports system wide, user global and per-repo configurations.
32+
33+
#### Actions
34+
- `set`: Sets the given config
35+
36+
#### Properties
37+
- `key`: The key of config to set, e.g. user.full_name, defaults to the resource name
38+
- `value`: The config value to set
39+
- `user`: The user to run the command as
40+
- `scope`: The scope to set the config for, one of `local global system`, defaults to global
41+
- `path`: The path to run the command in, needed when setting per-repo settings, defaults to empty string
42+
- `options`: Additional options that will be appended to the command, defaults to empty string
43+
44+
#### Exmaples
45+
```ruby
46+
# Minimal properties
47+
codenamephp_git_client_config 'user.full_name' do
48+
value 'Some User'
49+
user 'someuser'
50+
end
51+
52+
# All properties
53+
codenamephp_git_client_config 'Set some config' do
54+
key 'some config'
55+
value 'some value'
56+
user 'someuser'
57+
scope 'local'
58+
path '/some/path'
59+
options '--some-options'
60+
end
61+
```
62+
63+
### config_user
64+
The `codenamephp_git_client_config_user` is used to configure multiple values for a single user using a hash where the keys are the config keys and values the configs to be set for their keys.
65+
The configs are all set in the --global scope.
66+
Uses `codenamephp_git_client_config` internally.
67+
68+
#### Actions
69+
- `:set`: Sets the given configs for the given user
70+
71+
#### Properties
72+
- `user`: The user to execute the command as and to set the configs for
73+
- `configs`: A hash with the config keys and their values
74+
75+
#### Examples
76+
```ruby
77+
# Minimal properties
78+
codenamephp_git_client_config_user 'Set configs for user' do
79+
user 'some user'
80+
configs lazy { { 'user.full_name' => 'Test User', 'user.email' => 'test@test.de' } }
81+
end
82+
```
83+
84+
### config_users
85+
The `codenamephp_git_client_config_users` is used to configure multiple configs for multiple users at once. The configs are passed as a Hash with the username as key and another Hash with config name => config value pairs.
86+
Uses `codenamephp_git_client_config_user` internally.
87+
88+
#### Actions
89+
- `:set`: Sets the given configs for the given users
90+
91+
#### Properties
92+
- `users_configs`: A Hash with the username as key and another Hash with config name => config value pairs.
93+
94+
#### Examples
95+
```ruby
96+
# Minimal properties
97+
codenamephp_git_client_config_users 'Config user' do
98+
users_configs lazy {
99+
{
100+
'user1' => { :config1_user1 => 'value1_user1', 'config2_user1' => 'value2_user1' },
101+
'user2' => { :config1_user2 => 'value1_user2', 'config2_user2' => 'value2_user2' },
102+
}
103+
}
104+
end
105+
```
106+
107+
### config_users_from_data_bag
108+
The `config_users_from_data_bag` resource can be used to set git configurations from data bags. The configs have to be set in the user item. Example:
109+
110+
```json
111+
{
112+
"id": "user",
113+
"codenamephp": {
114+
"git_client": {
115+
"config": {
116+
"user.full_name": "Test User",
117+
"user.email": "test@test.de"
118+
}
119+
}
120+
}
121+
}
122+
```
123+
124+
Only users that have a non-empty config set are used.
125+
126+
#### Actions
127+
- `manage`: Manages the configs from the data bag
128+
129+
#### Properties
130+
- `data_bag_name`: The name of the data bag to search for users, defaults to `users`
131+
132+
#### Examples
133+
134+
```ruby
135+
# Minimal properties
136+
codenamephp_git_client_config_users_from_data_bag 'Manage git users'
137+
138+
# With custom data_bag_name
139+
codenamephp_git_client_config_users_from_data_bag 'Manage git users' do
140+
data_bag_name 'some_data_bag'
141+
end
142+
```

‎kitchen.yml

+37
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,40 @@ suites:
1919
inspec_tests:
2020
- test/smoke/default
2121
attributes:
22+
- name: config
23+
run_list:
24+
- recipe[test::default]
25+
- recipe[test::config]
26+
verifier:
27+
inspec_tests:
28+
- test/smoke/default
29+
- test/smoke/config
30+
attributes:
31+
- name: config_user
32+
run_list:
33+
- recipe[test::default]
34+
- recipe[test::config_user]
35+
verifier:
36+
inspec_tests:
37+
- test/smoke/default
38+
- test/smoke/config
39+
attributes:
40+
- name: config_users
41+
run_list:
42+
- recipe[test::default]
43+
- recipe[test::config_users]
44+
verifier:
45+
inspec_tests:
46+
- test/smoke/default
47+
- test/smoke/config
48+
attributes:
49+
- name: config_users_from_data_bag
50+
data_bags_path: "test/fixtures/data_bags"
51+
run_list:
52+
- recipe[test::default]
53+
- recipe[test::config_users_from_data_bag]
54+
verifier:
55+
inspec_tests:
56+
- test/smoke/default
57+
- test/smoke/config
58+
attributes:

‎metadata.rb

-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@
1111
source_url 'https://github.com/codenamephp/chef.cookbook.gitClient'
1212

1313
supports 'debian'
14-
15-
depends 'git', '~> 11.0'

‎resources/config.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
unified_mode true
2+
3+
property :key, String, name_property: true, description: 'The key of config to set, e.g. user.full_name'
4+
property :value, String, description: 'The value to set for the key'
5+
property :user, String, description: 'The user to run the command as'
6+
property :scope, String, equal_to: %w(local global system), default: 'global', description: 'If the value should be set system wide, global for the user or the local repo'
7+
property :path, [String, nil], description: 'The path to exectue the config command in when setting values for a repo'
8+
property :options, String, default: '', description: 'Additional options to add to the command, e.g. --add'
9+
10+
action :set do
11+
execute config_command do
12+
cwd new_resource.path
13+
user new_resource.user
14+
environment lazy { { 'USER' => new_resource.user, 'HOME' => ::Dir.home(new_resource.user) } }
15+
end
16+
end
17+
18+
action_class do
19+
def config_command
20+
format('git config --%<scope>s %<key>s "%<value>s" %<options>s', scope: new_resource.scope, key: new_resource.key, value: new_resource.value, options: new_resource.options).rstrip
21+
end
22+
end

‎resources/config_user.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
unified_mode true
2+
3+
property :user, String, description: 'The user to set the configs for'
4+
property :configs, Hash, description: 'Hash of config_key => config_value entries that will be set'
5+
6+
action :set do
7+
new_resource.configs.each do |config_name, config_value|
8+
codenamephp_git_client_config "Setting config #{config_name.to_s} for #{new_resource.user}" do
9+
key config_name.to_s
10+
value config_value.to_s
11+
scope 'global'
12+
user new_resource.user
13+
end
14+
end
15+
end

‎resources/config_users.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
unified_mode true
2+
3+
property :users_configs, Hash, description: 'Hash of all users and configs with the user as key and the configs as Hash'
4+
5+
action :set do
6+
new_resource.users_configs.each do |user, configs|
7+
codenamephp_git_client_config_user "Set configs for #{user}" do
8+
user user
9+
configs configs
10+
end
11+
end
12+
end
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
unified_mode true
2+
3+
property :data_bag_name, String, default: 'users', description: 'The databag to query for the users to manage'
4+
5+
action :manage do
6+
begin
7+
users_configs = search(new_resource.data_bag_name, 'codenamephp_git_client_config*:*')
8+
.to_h { |user| [user[:id], user.dig(:codenamephp, :git_client, :config) || {}] }
9+
.select { |_, configs| !configs.nil? && !configs.empty? }
10+
11+
codenamephp_git_client_config_users 'Config users from databag' do
12+
users_configs users_configs
13+
end
14+
rescue Net::HTTPServerException, Chef::Exceptions::InvalidDataBagPath
15+
log "Databag '#{new_resource.data_bag_name}' with query 'codenamephp_git_client_config:*' was not found" do
16+
level :debug
17+
end
18+
end
19+
end

‎resources/package.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
unified_mode true
2+
3+
property :package_name, String, default: 'git', description: 'The name of the package to install'
4+
5+
action :install do
6+
package new_resource.package_name
7+
end

‎spec/resources/config_spec.rb

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'spec_helper'
2+
3+
describe 'codenamephp_git_client_config' do
4+
platform 'debian' # https://github.com/chefspec/chefspec/issues/953
5+
6+
step_into :codenamephp_git_client_config
7+
8+
before(:example) do
9+
allow(Dir).to receive(:home).with('someuser').and_return('/home/someuser')
10+
end
11+
12+
context 'Install with minimal properties' do
13+
recipe do
14+
codenamephp_git_client_config 'user.full_name' do
15+
value 'Some User'
16+
user 'someuser'
17+
end
18+
end
19+
20+
it 'converges successfully' do
21+
expect { chef_run }.to_not raise_error
22+
end
23+
24+
it 'sets the config' do
25+
expect(chef_run).to run_execute('git config --global user.full_name "Some User"').with(
26+
cwd: nil,
27+
user: 'someuser',
28+
environment: { 'USER' => 'someuser', 'HOME' => '/home/someuser' }
29+
)
30+
end
31+
end
32+
33+
context 'Install with all properties' do
34+
recipe do
35+
codenamephp_git_client_config 'Set some config' do
36+
key 'some config'
37+
value 'some value'
38+
user 'someuser'
39+
scope 'local'
40+
path '/some/path'
41+
options '--some-options'
42+
end
43+
end
44+
45+
it 'converges successfully' do
46+
expect { chef_run }.to_not raise_error
47+
end
48+
49+
it 'sets the config' do
50+
expect(chef_run).to run_execute('git config --local some config "some value" --some-options').with(
51+
cwd: '/some/path',
52+
user: 'someuser',
53+
environment: { 'USER' => 'someuser', 'HOME' => '/home/someuser' }
54+
)
55+
end
56+
end
57+
end

‎spec/resources/config_user_spec.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'spec_helper'
2+
3+
describe 'codenamephp_git_client_config_user' do
4+
platform 'debian' # https://github.com/chefspec/chefspec/issues/953
5+
6+
step_into :codenamephp_git_client_config_user
7+
8+
context 'Install with minimal properties' do
9+
recipe do
10+
codenamephp_git_client_config_user 'Config user' do
11+
user 'some user'
12+
configs lazy { { :config1 => 'value1', 'config2' => 'value2' } }
13+
end
14+
end
15+
16+
it 'converges successfully' do
17+
expect { chef_run }.to_not raise_error
18+
end
19+
20+
it 'Will install git from package' do
21+
expect(chef_run).to set_codenamephp_git_client_config('Setting config config1 for some user').with(
22+
key: 'config1',
23+
value: 'value1',
24+
scope: 'global',
25+
user: 'some user'
26+
)
27+
expect(chef_run).to set_codenamephp_git_client_config('Setting config config2 for some user').with(
28+
key: 'config2',
29+
value: 'value2',
30+
scope: 'global',
31+
user: 'some user'
32+
)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)