Code Monkey home page Code Monkey logo

blockinfile's Introduction

blockinfile

Command line utility (CLI) that will insert/update/remove a block of multi-line text surrounded by customizable marker lines.

Inspiration

If this looks similar to Ansible's ansible.builtin.blockinfile, it should. This CLI is based on it and supports a subset of the Ansible blockinfile flags. This CLI was created for scenarios when you wanted Ansible's blockinfile function, but did not want to be dependent on installing Ansible.

Installation

go get github.com/dustinsand/blockinfile

CLI arguments

Argument Comments
config File with blockinfile configuration parameters.

Configuration File Parameters

Parameter Choices Comments
backup true/false Default: false Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
block text The text to insert inside the marker lines.
indent Default: 0 The number of spaces to indent the block. Indent must be >= 0.
insertafter text If specified and no begin/ending marker lines are found, the block will be inserted after the last match of specified text. If specified regular expression has no matches, EOF will be used instead.
insertbefore text If specified and no begin/ending marker lines are found, the block will be inserted before the last match of specified text. If specified regular expression has no matches, the block will be inserted at the end of the file.
marker Default: "# {mark} MANAGED BLOCK" The marker line template. {mark} will be replaced with the values in marker_begin (default="BEGIN") and marker_end (default="END").
markerbegin Default: "BEGIN" This will be inserted at {mark} in the opening block marker.
markerend Default: "END" This will be inserted at {mark} in the closing block marker.
path (required) text The file to modify. If the file does not exist, it will be created.
state true/false Default: true Whether the block should be there or not.

Examples

Example 1 - Replace block with new text.

/tmp/example1.txt

line 1
line 2
# BEGIN MANAGED BLOCK
old block 1
old block 2
# END MANAGED BLOCK
line 3
line 4

/tmp/blockinfile1.yml

path: /tmp/example1.txt
block: |-
  new block 1
  new block 2
indent: 2

blockinfile --config /tmp/blockinfile1.yml

Would update /tmp/example1.txt with

line 1
line 2
  # BEGIN MANAGED BLOCK
  new block 1
  new block 2
  # END MANAGED BLOCK
line 3
line 4

Example 2 - Add block to file.

/tmp/example2.txt

line 1
line 2
line 3
line 4

/tmp/blockinfile2.yml

path: /tmp/example2.txt
block: |-
  new block 1
  new block 2
indent: 2
insertbefore: "line 1"

blockinfile --config /tmp/blockinfile2.yml

Would update /tmp/example2.txt with

  # BEGIN MANAGED BLOCK
  new block 1
  new block 2
  # END MANAGED BLOCK
line 1
line 2
line 3
line 4

blockinfile's People

Contributors

dustinsand avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.