github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/docs/remote-docs.sh (about)

     1  #!/usr/bin/env bash
     2  # Assemble remote man pages for darwin or windows from markdown files
     3  set -e
     4  
     5  PLATFORM=$1                         ## linux, windows or darwin
     6  TARGET=${2}                         ## where to output files
     7  SOURCES=${@:3}                      ## directories to find markdown files
     8  
     9  PODMAN=${PODMAN:-bin/podman-remote} ## location overridden for testing
    10  
    11  function usage() {
    12      echo >&2 "$0 PLATFORM TARGET SOURCES..."
    13      echo >&2 "PLATFORM: Is either linux, darwin or windows."
    14      echo >&2 "TARGET: Is the directory where files will be staged. eg, docs/build/remote/linux"
    15      echo >&2 "SOURCES: Are the directories of source files. eg, docs/source/markdown"
    16  }
    17  
    18  function fail() {
    19      echo >&2 -e "$(dirname $0): $@\n"
    20      exit 1
    21  }
    22  
    23  case $PLATFORM in
    24  darwin|linux)
    25      PUBLISHER=man_fn
    26      ext=1
    27      ;;
    28  windows)
    29      PUBLISHER=html_fn
    30      ext=1.md
    31      ;;
    32  -help)
    33      usage
    34      exit 0
    35      ;;
    36  *) fail '"linux", "darwin" and "windows" are the only supported platforms.' ;;
    37  esac
    38  
    39  if [[ -z $TARGET ]]; then
    40      fail 'TARGET directory is required'
    41  fi
    42  
    43  if [[ -z $SOURCES ]]; then
    44      fail 'At least one SOURCE directory is required'
    45  fi
    46  
    47  if [[ ! -x $PODMAN ]]; then
    48      fail "$PODMAN does not exist"
    49  fi
    50  
    51  ## man_fn copies the man page or link to flattened directory
    52  function man_fn() {
    53      local page=$1
    54      local file=$(basename $page)
    55      local dir=$(dirname $page)
    56  
    57      if [[ ! -f $page ]]; then
    58          page=$dir/links/${file%.*}.$ext
    59      fi
    60      install $page $TARGET/${file%%.*}.1
    61  }
    62  
    63  ## html_fn converts the markdown page or link to HTML
    64  function html_fn() {
    65      local markdown=$1
    66      local file=$(basename $markdown)
    67      local dir=$(dirname $markdown)
    68  
    69      if [[ ! -f $markdown ]]; then
    70          local link=$(sed -e 's?.so man1/\(.*\)?\1?' <$dir/links/${file%.md})
    71          markdown=$dir/$link.md
    72      fi
    73      pandoc --ascii --lua-filter=docs/links-to-html.lua -o $TARGET/${file%%.*}.html $markdown
    74  }
    75  
    76  # Run 'podman help' (possibly against a subcommand, e.g. 'podman help image')
    77  # and return a list of each first word under 'Available Commands', that is,
    78  # the command name but not its description.
    79  function podman_commands() {
    80      $PODMAN help "$@" |\
    81          awk '/^Available Commands:/{ok=1;next}/^Options:/{ok=0}ok { print $1 }' |\
    82          grep .
    83  }
    84  
    85  function podman_all_commands(){
    86      for cmd in $(podman_commands "$@") ; do
    87  		echo $@ $cmd
    88          podman_all_commands $@ $cmd
    89  	done
    90  }
    91  
    92  ## pub_pages finds and publishes the remote manual pages
    93  function pub_pages() {
    94      local source=$1
    95      local publisher=$2
    96      for f in $(ls $source/podman-remote*); do
    97          $publisher $f
    98      done
    99  
   100  
   101      while IFS= read -r cmd; do
   102          file="podman-${cmd// /-}"
   103  
   104          # Source dir may have man (.1) files (linux/darwin) or .1.md (windows)
   105          # but the links subdir will always be .1 (man) files
   106          if [ -f $source/$file.$ext -o -f $source/links/$file.1 ]; then
   107              $publisher $(echo $source/$file.$ext)
   108          else
   109              # This is worth failing CI for
   110              fail "no doc file nor link $source/$file.$ext for 'podman $cmd'"
   111          fi
   112      done <<< "$(podman_all_commands)"
   113  }
   114  
   115  ## sed syntax is different on darwin and linux
   116  ## sed --help fails on mac, meaning we have to use mac syntax
   117  function sed_os(){
   118      if sed --help > /dev/null 2>&1 ; then
   119          $(sed -i "$@")
   120      else
   121          $(sed -i "" "$@")
   122      fi
   123  }
   124  
   125  ## rename renames podman-remote.ext to podman.ext, and fixes up contents to reflect change
   126  function rename (){
   127      if [[ "$PLATFORM" != linux ]]; then
   128          local remote=$(echo $TARGET/podman-remote.*)
   129          local ext=${remote##*.}
   130          mv $remote $TARGET/podman.$ext
   131  
   132          sed_os "s/podman\\\*-remote/podman/g" $TARGET/podman.$ext
   133          sed_os "s/A\ remote\ CLI\ for\ Podman\:\ //g" $TARGET/podman.$ext
   134          case $PLATFORM in
   135          darwin|linux)
   136              sed_os "s/Podman\\\*-remote/Podman\ for\ Mac/g" $TARGET/podman.$ext
   137          ;;
   138          windows)
   139              sed_os "s/Podman\\\*-remote/Podman\ for\ Windows/g" $TARGET/podman.$ext
   140          ;;
   141          esac
   142      fi
   143  }
   144  
   145  ## walk the SOURCES for markdown sources
   146  mkdir -p $TARGET
   147  for s in $SOURCES; do
   148      if [[ -d $s ]]; then
   149          pub_pages $s $PUBLISHER
   150      else
   151          echo >&2 "Warning: $s does not exist"
   152      fi
   153  done
   154  rename