This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Tools

Available tool (embedded commands) in ops:

1 - base64

base64 utility acts as a base64 decoder when passed the --decode (or -d) flag and as a base64 encoder otherwise. As a decoder it only accepts raw base64 input and as an encoder it does not produce the framing lines.

Usage:
  ops -base64 [options] <string>

Options

-h, --help             Display this help message
-e, --encode <string>  Encode a string to base64
-d, --decode <string>  Decode a base64 string

Examples

Encoding

ops -base64 -e "OpenServerless is wonderful"

This will output:

T3BlblNlcnZlcmxlc3MgaXMgd29uZGVyZnVs

Decoding

ops -base64 -d "T3BlblNlcnZlcmxlc3MgaXMgd29uZGVyZnVs"

This will output:

OpenServerless is wonderful

2 - datefmt

Print date with different formats. If no time stamp or date strings are given, uses current time

Usage:
  ops -datefmt [options] [arguments]

Options

-h, --help		 print this help info
-t, --timestamp	 unix timestamp to format (default: current time)
-s, --str 	  	 date string to format
--if			 input format to use with input date string (via --str)
-f, --of		 output format to use (default: UnixDate)

Possible formats (they follows the standard naming of go time formats, with the addition of ‘Millisecond’ and ‘ms’):

  • Layout
  • ANSIC
  • UnixDate
  • RubyDate
  • RFC822
  • RFC822Z
  • RFC850
  • RFC1123
  • RFC1123Z
  • RFC3339
  • RFC3339Nano
  • Kitchen
  • Stamp
  • StampMilli
  • StampMicro
  • StampNano
  • DateTime
  • DateOnly
  • TimeOnly
  • Milliseconds
  • ms

Example

$ ops -datefmt -f DateTime
2024-08-11 03:00:34

3 - echoif

echoif is a utility that echoes the value of <a> if the exit code of the previous command is 0, echoes the value of <b> otherwise

Usage:
    ops -echoif <a> <b>

Example

  $( exit 1 ); ops -echoif "0" "1"
  1

or

  $( exit 0 ); ops -echoif "0" "1"
  0

4 - echoifempty

echoifempty is a utility that echoes the value of <a> if <str> is empty, echoes the value of <b> otherwise.

Usage:
    ops -echoifempty <str> <a> <b>

Example

  ops -echoifempty "not empty string" "string is empty" "string is not empty"

5 - echoifexists

echoifexists is a utility that echoes the value of <a> if <file> exists, echoes the value of <b> otherwise.

Usage:
    ops -echoifexists <file> <a> <b>

Example

  ops -echoifexists "exists" "doesn't exists"

6 - empty

empty creates an empty file - returns error if it already exists.

Usage:
    ops -empty <filename>

7 - executable

executable make a file executable: on Unix-like systems it will do a chmod u+x. On Windows systems it will rename the file to .exe if needed.

Usage:
    ops -executable <filename>

Example

ops -executable kind

8 - extract

Extract one single file from a .zip .tar, .tgz, .tar.gz, tar.bz2, tar.gz.

Usage:
    ops -extract file.(zip|tgz|tar[.gz|.bz2|.xz]) target

Example

Extract file named single.pdf from archive.zip archive.

ops -extract archive.zip single.pdf

9 - filetype

Show extension and MIME type of a file. Supported types are documented here

Usage:
    ops -filetype [-h] [-e] [-m] FILE

Options

-h  shows this help
-e  show file standard extension
-m  show file mime type

Examples

File Mime type

ops -filetype -m `which ops`

This will output the ops executable type: application/x-mach-binary or application/x-executable

10 - needupdate

Check if a semver version A > semver version B. Exits with 0 if greater, 1 otherwise.

Usage:
    ops -needupdate <versionA> <versionB>

Options

-h, --help		 print this help info

Examples

Update is needed

ops -needupdate 1.0.1 1.0.0; echo $?

This will output:

0

Update is not needed

ops -needupdate 1.0.0 1.0.1; echo $?

This will output:

1

11 - opspath

Join a relative path to the path from where ops was executed. This command is useful when creating custom tasks ( e.g. an ops plugin).

Usage:
    ops -opspath <path>

Options:

-h, --help  print this help info

Examples

You are executing in directory /home/user/my/custom/dir

ops -opspath my-file.txt

This will output:

/home/user/my/custom/dir/my-file.txt

12 - random

Generate random numbers, strings and uuids

Usage:
    ops -random [options]

Options

-h, --help  shows this help
-u, --uuid  generates a random uuid v4
--int  <max> [min] generates a random non-negative integer between min and max (default min=0)
--str  <len> [<characters>] generates an alphanumeric string of length <len> from the set of <characters> provided (default <characters>=a-zA-Z0-9)

Examples

Random uuid v4:

ops -random -u                 

This will output something like:

5b2c45ef-7d15-4a15-84c6-29144393b621

Random integer between max and min

ops -random --int 100 60

This will output something like:

78

13 - remove

Remove a file

Usage:
    ops -remove <filename>

14 - rename

Rename a file

Usage:
    ops -rename <source> <destination>

15 - retry

Usage:
    ops -retry [options] task [task options]

Options

-h, --help	Print help message
-t, --tries=#	Set max retries: Default 10
-m, --max=secs	Maximum time to run (set to 0 to disable): Default 60 seconds
-v, --verbose	Verbose output

Example

Retry two times to get the ops action list

ops -retry -t 2 ops action list

16 - sh

sh is the mvdan shell using the ops environment.

Without args, starts an interactive shell. Otherwise execute the script specified on command line.

Usage: 
    ops -sh [<script>|-h|--help]

17 - urlenc

urlencode parameters using the default & separator (or a specific one using -s flag). Optionally, encode the values retrieving them from environment variables.

Usage:
    ops -urlenc [-e] [-s <string>] [parameters]

Options

-e    Encode parameter values from environment variables
-h    Show help
-s string  Separator for concatenating the parameters (default "&")

Examples

ops -urlenc a=1 b=2

This will output:

a%3D1&b%3D2

18 - validate

Check if a value is valid according to the given constraints. If -e is specified, the value is retrieved from the environment variable with the given name.

Usage:
ops -validate [-e] [-m | -n | -r <regex>] <value> [<message>]

Options

-e    Retrieve value from the environment variable with the given name.
-h    Print this help message.
-m    Check if the value is a valid email address.
-n    Check if the value is a number.
-r string Check if the value matches the given regular expression.

Examples

Validate with regexp

Validate email

ops -validate -m example@gmail.com
ops -validate -r '^[a-z]+$' abc