<commit_before># Custom Resources

The most interesting topic after one has learned how to use Chef could be define a custom resources. Actually it is not hard to create a new resource using Chef.

Notice that this article only works after Chef 12.5, before that, we have to write some definitions instead of resources.

Firstly, create a new cookbook:

  ```console
$ chef generate cookbook /path/to/cookbooks/custom_resource
  ```

Then create a folder called `resources`, and put a file called `file_writer.rb` into it, with its content:

  ```ruby
# Specifies the name of the resource
# Otherwise, the resource could be referenced as <cookbook_name>_<resource_file_name>
resource_name :file_writer

# Specifies the default action
default_action :write 

# Define a property
property :user_name, String, default: 'World'

# Define a routine which checks and does something with the current state
load_current_value do 
  # Do nothing here.
end

# Define an action
action :write do
  file '/tmp/output.txt' do
    content 'Hello, %s!' % user_name
  end
end
  ```

Then write something to the `default.rb`:

  ```ruby
file_writer 'whatever' do
  user_name 'Flaregames'
end
  ```<commit_msg>Update custom-resources.md<commit_after># Custom Resources

The most interesting topic after one has learned how to use Chef could be define a custom resources. Actually it is not hard to create a new resource using Chef.

Notice that this article only works after Chef 12.5, before that, we have to write some definitions instead of resources.

Firstly, create a new cookbook:

  ```console
$ chef generate cookbook /path/to/cookbooks/custom_resource
  ```

Then create a folder called `resources`, and put a file called `file_writer.rb` into it, with its content:

  ```ruby
# Specifies the name of the resource
# Otherwise, the resource could be referenced as <cookbook_name>_<resource_file_name>
resource_name :file_writer

# Specifies the default action
default_action :write 

# Define a property
property :user_name, String, default: 'World'

# Define a routine which checks and does something with the current state
load_current_value do 
  # Do nothing here.
end

# Define an action
action :write do
  file '/tmp/output.txt' do
    content 'Hello, %s!' % user_name
  end
end
  ```

Then write something to the `default.rb`:

  ```ruby
file_writer 'whatever' do
  user_name 'xiGUAwanOU'
end
  ```

Done! Let's run the recipe, use the command:

  ```console
$ chef-client --local-mode --runlist 'recipe[custom_resource]'
  ```

There will be a file `/tmp/output.txt` created with content `Hello, xiGUAwanOU!`
{'fraction_non_alphanumeric': 0.07674597083653108, 'fraction_numerical': 0.0023023791250959325, 'mean_word_length': 4.364197530864198, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4221908', 'n_tokens_mistral': 772, 'n_tokens_neox': 733, 'n_words': 339}