Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to print a command help info #10

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ RUN apk add --no-cache \
git \
openssh-client \
libc6-compat \
libstdc++
libstdc++ \
curl

RUN curl -L https://github.com/LinuxSuRen/http-downloader/releases/download/v0.0.67/hd-linux-amd64.tar.gz | tar xzv hd && \
mv hd /usr/bin/hd && \
hd fetch --reset

COPY entrypoint.sh /entrypoint.sh
COPY --from=builder /workspace/yaml-readme /usr/bin/yaml-readme
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Flags:
| `parentname` | The parent directory name. For example, `items/good.yaml`, the parent name is `items`. |
| `fullpath` | The related file path of each items. |

### Available functions

| Name | Usage |
|--------------------|---|
| `printHelp` | `{{printHelp 'hd'}}` |

### Ignore particular items

In case you want to ignore some particular items, you can put a key `ignore` with value `true`. Let's see the following sample:
Expand Down
24 changes: 14 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ inputs:
required: false
push:
description: 'Indicate if you want to push the changes automatically'
default: true
default: 'true'
required: true
tool:
description: 'The tool name which you want to install'
required: false
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.pattern }}
- ${{ inputs.username }}
- ${{ inputs.org }}
- ${{ inputs.repo }}
- ${{ inputs.sortby }}
- ${{ inputs.groupby }}
- ${{ inputs.output }}
- ${{ inputs.template }}
- ${{ inputs.push }}
- --pattern=${{ inputs.pattern }}
- --username=${{ inputs.username }}
- --org=${{ inputs.org }}
- --repo=${{ inputs.repo }}
- --sortby=${{ inputs.sortby }}
- --groupby=${{ inputs.groupby }}
- --output=${{ inputs.output }}
- --template=${{ inputs.template }}
- --push=${{ inputs.push }}
- --tool=${{ inputs.tool }}
59 changes: 53 additions & 6 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
#!/bin/sh

yaml-readme -p "$1" --sort-by "$5" --group-by "$6" --template "$8" > "$7"
while [ $# -gt 0 ]; do
case "$1" in
--pattern=*)
pattern="${1#*=}"
;;
--username=*)
username="${1#*=}"
;;
--org=*)
org="${1#*=}"
;;
--repo=*)
repo="${1#*=}"
;;
--sortby=*)
sortby="${1#*=}"
;;
--groupby=*)
groupby="${1#*=}"
;;
--output=*)
output="${1#*=}"
;;
--template=*)
template="${1#*=}"
;;
--push=*)
push="${1#*=}"
;;
--tool=*)
tool="${1#*=}"
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift
done

if [ "$9" = "true" ]
if [ "$tool" != "" ]
then
git config --local user.email "LinuxSuRen@users.noreply.github.com"
git config --local user.name "rick"
echo "start to install tool $tool"
hd i "$tool"
fi

yaml-readme -p "$pattern" --sort-by "$sortby" --group-by "$groupby" --template "$template" > "$output"

if [ "$push" = "true" ]
then
git config --local user.email "${username}@users.noreply.github.com"
git config --local user.name "${username}"
git add .

git commit -m "Auto commit by rick's bot, ci skip"
git push https://${2}:${GH_TOKEN}@github.com/${3}/${4}.git
git commit -m "Auto commit by bot, ci skip"
git push https://${username}:${GH_TOKEN}@github.com/${org}/${repo}.git
fi
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"html/template"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
Expand Down Expand Up @@ -106,7 +107,20 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {

// generate readme file
var tpl *template.Template
if tpl, err = template.New("readme").Parse(readmeTpl); err != nil {
if tpl, err = template.New("readme").Funcs(template.FuncMap{
"printHelp": func(cmd string) (output string) {
var err error
var data []byte
if data, err = exec.Command(cmd, "--help").Output(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "failed to run command", cmd)
} else {
output = fmt.Sprintf(`%s
%s
%s`, "```shell", string(data), "```")
}
return
},
}).Parse(readmeTpl); err != nil {
return
}

Expand Down