فهرست منبع

feat(git): Add the git configuration

- Add default user 'jerem'
- Add system git configuration
- Add global git configuration for 'jeremy'
- Add file git configuration for 'jeremy'

Signed-off-by: Jeremy MAURO <jeremy.mauro@gmail.com>
Jeremy MAURO 2 سال پیش
والد
کامیت
ee85f448ab

+ 8 - 1
base.lock.json

@@ -87,7 +87,14 @@
     }
   },
   "default_attributes": {
-
+    "my_workstation": {
+      "default_user": {
+        "jeremy": {
+          "home": "/home/jeremy",
+          "shell": "/bin/bash"
+        }
+      }
+    }
   },
   "override_attributes": {
 

+ 6 - 0
base.rb

@@ -19,3 +19,9 @@ run_list [
 # cookbook 'example_cookbook', path: '../cookbooks/example_cookbook'
 cookbook 'etckeeper', git: 'https://github.com/jmauro/etckeeper-cookbook', branch: 'master-next'
 cookbook 'git', git: 'https://github.com/jmauro/git', branch: 'config_file_scope'
+
+# Global attributes
+default['my_workstation']['default_user']['jeremy'] = {
+  'home': '/home/jeremy',
+  'shell': '/bin/bash',
+}

+ 45 - 0
cookbooks/workstation/attributes/_git.rb

@@ -0,0 +1,45 @@
+default['workstation']['_git']['system'] = {
+  'color.ui': true,
+  'color.diff': 'auto',
+  'color.status': 'auto',
+  'color.branch': 'auto',
+  'color.interactive': 'auto',
+  'status.submodulesummary': true,
+  'fetch.recursesubmodules': true,
+  'rebase.stat': true,
+  'help.autocorrect': '10',
+  'commit.verbose': true,
+  'alias.gol': %Q{log --graph --pretty=format:'%C(yellow)%h%C(green)%d%Creset %s %C(white) %an, %ar%Creset'},
+  'alias.commit': '--signoff',
+  'alias.com': 'commit --signoff',
+  'alias.coma': 'commit --signoff --amend',
+  'alias.push': '--verbose --tags',
+  'alias.pull': '--verbose',
+  'alias.co': 'checkout',
+  'alias.br': 'branch --all --verbose',
+  'alias.s': 'status --short --branch',
+  'include.path': '~/.gitconfig',
+  'user.email': "jeremy@#{node['hostname']}",
+  'user.name': 'Jeremy from WSL2',
+}
+
+# We don't use hash because keys such as "includeif.gitdir" are not uniq
+default['workstation']['_git']['global'] = {
+  'core.sshcommand': '/usr/sbin/ssh-ident',
+  'user.email': 'jeremy.mauro@gmail.com',
+  'user.name': 'Jeremy MAURO',
+  'includeif.gitdir:~/repo/aviatrix/.path': '~/.gitconfig-work',
+  'includeif.gitdir:~/repo/personal/.path': '~/.gitconfig-personal',
+  'url.git@github.com': '.insteadof=https://github.com/',
+}
+
+default['workstation']['_git']['file_~/.gitconfig-personal'] = {
+  'user.email': 'jeremy.mauro@gmail.com',
+  'user.name': 'Jeremy MAURO',
+  'user.signingkey': 'FA2D8E280A6DD533',
+}
+
+default['workstation']['_git']['file_~/.gitconfig-work'] = {
+  'user.email': 'jmauro@aviatrix.com',
+  'user.name': 'Jeremy MAURO',
+}

+ 40 - 0
cookbooks/workstation/recipes/_git.rb

@@ -0,0 +1,40 @@
+#
+# Cookbook:: workstation
+# Recipe:: _git
+#
+# Copyright:: 2022, The Authors, All Rights Reserved.
+
+node['my_workstation']['default_user'].each do |username, properties|
+  user username do
+    properties.each do |property, value|
+      send(property, value)
+    end
+    manage_home true
+  end
+
+  user_repo_directory = ::File.join(properties['home'], 'repo')
+
+  directory user_repo_directory do
+    owner username
+    group username
+    recursive true
+  end
+
+  include_recipe 'git'
+
+  node['workstation']['_git'].each do |scope_git, git_options|
+    if scope_git.match(/^file_/)
+      gitconfig_file = $' # String after the match (ref: https://ruby-doc.org/core-2.5.1/Regexp.html#class-Regexp-label-Special+global+variables)
+      scope_git = 'file'
+    end
+    git_options.each do |k, v|
+      git_config k do
+        value v.to_s
+        scope scope_git
+        user username if %w{global file}.include?(scope_git)
+        config_file gitconfig_file if scope_git == 'file'
+      end
+    end
+  end
+end
+

+ 6 - 2
cookbooks/workstation/recipes/default.rb

@@ -13,12 +13,16 @@ end
 
 # Configure repo
 include_recipe 'workstation::_apt'
-# Setup podman
-include_recipe 'workstation::_podman'
+
+# Setup 'git'
+include_recipe 'workstation::_git'
 
 # Setup etckeeper
 include_recipe 'etckeeper'
 include_recipe 'etckeeper::commit'
 
+# Setup podman
+include_recipe 'workstation::_podman'
+
 # Install 'systemd-genie'
 include_recipe 'workstation::_systemd-genie'

+ 29 - 0
cookbooks/workstation/spec/unit/recipes/_git_spec.rb

@@ -0,0 +1,29 @@
+#
+# Cookbook:: workstation
+# Spec:: _git
+#
+# Copyright:: 2022, The Authors, All Rights Reserved.
+
+require 'spec_helper'
+
+describe 'workstation::_git' do
+  context 'When all attributes are default, on Ubuntu 20.04' do
+    # for a complete list of available platforms and versions see:
+    # https://github.com/chefspec/fauxhai/blob/main/PLATFORMS.md
+    platform 'ubuntu', '20.04'
+
+    it 'converges successfully' do
+      expect { chef_run }.to_not raise_error
+    end
+  end
+
+  context 'When all attributes are default, on CentOS 8' do
+    # for a complete list of available platforms and versions see:
+    # https://github.com/chefspec/fauxhai/blob/main/PLATFORMS.md
+    platform 'centos', '8'
+
+    it 'converges successfully' do
+      expect { chef_run }.to_not raise_error
+    end
+  end
+end

+ 16 - 0
cookbooks/workstation/test/integration/default/_git_test.rb

@@ -0,0 +1,16 @@
+# Chef InSpec test for recipe workstation::_git
+
+# The Chef InSpec reference, with examples and extensive documentation, can be
+# found at https://docs.chef.io/inspec/resources/
+
+unless os.windows?
+  # This is an example test, replace with your own test.
+  describe user('root'), :skip do
+    it { should exist }
+  end
+end
+
+# This is an example test, replace it with your own test.
+describe port(80), :skip do
+  it { should_not be_listening }
+end