Skip to content

Commit

Permalink
feat: add support for customizing input and output columns
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Jul 28, 2022
1 parent 0bdd8ef commit 1060d69
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Setup go
uses: actions/setup-go@v3
with:
go-version: '1.17.5'
go-version: '1.18.3'

- uses: actions/cache@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v2
with:
go_version: "1.17.5"
go_version: "1.18.3"
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: '1.17.5'
go-version: '1.18.3'

- uses: actions/cache@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: "1.17.5"
go-version: "1.18.3"

- uses: actions/cache@v3
with:
Expand Down Expand Up @@ -112,7 +112,7 @@ jobs:
- name: Setup go
uses: actions/setup-go@v3
with:
go-version: '1.17.5'
go-version: '1.18.3'

- uses: actions/cache@v3
with:
Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ Add the `Inputs` and/or `Outputs` [`H2` header](https://github.com/adam-p/markdo

<!-- AUTO-DOC-INPUT:START - Do not remove or modify this section -->

| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|---------------|--------|----------|----------------|---------------------------------------------|
| action | string | false | `"action.yml"` | Path to the action.yml file<br> |
| col\_max\_width | string | false | `"1000"` | Max width of a column<br> |
| col\_max\_words | string | false | `"5"` | Max number of words per<br>line in a column |
| output | string | false | `"README.md"` | Path to the output file<br> |
| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|----------------|--------|----------|----------------|---------------------------------------------|
| action | string | false | `"action.yml"` | Path to the action.yml file<br> |
| col_max_width | string | false | `"1000"` | Max width of a column<br> |
| col_max_words | string | false | `"5"` | Max number of words per<br>line in a column |
| input_columns | string | false | | List of Input columns |
| output | string | false | `"README.md"` | Path to the output file<br> |
| output_columns | string | false | | List of Output columns |

<!-- AUTO-DOC-INPUT:END -->

Expand Down Expand Up @@ -103,26 +105,24 @@ Run
go install github.com/tj-actions/auto-doc
```

### Usage
### Synopsis

Run
Auto generate documentation for your github action.

```shell script
auto_doc --help
```
auto-doc [flags]
```

```shell script
Auto generate documentation for your github action.

Usage:
auto-doc [flags]
### Options

Flags:
--action string action config file (default "action.yml")
--colMaxWidth string Max width of a column (default "1000")
--colMaxWords string Max number of words per line in a column (default "5")
-h, --help help for auto-doc
--output string Output file (default "README.md")
```
--action string action config file (default "action.yml")
--colMaxWidth string Max width of a column (default "1000")
--colMaxWords string Max number of words per line in a column (default "5")
-h, --help help for auto-doc
--inputColumns stringArray list of input column names (default [Input,Type,Required,Default,Description])
--output string Output file (default "README.md")
--outputColumns stringArray list of output column names (default [Output,Type,Description])
```

* Free software: [Apache License 2.0](LICENSE)
Expand Down
26 changes: 24 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,37 @@ inputs:
description: 'Path to the output file'
required: false
default: README.md
input_columns:
description: 'List of Input columns'
required: false
output_columns:
description: 'List of Output columns'
required: false

runs:
using: 'composite'
steps:
- run: |
EXTRA_ARGS=""
for input_column in "${{ join(inputs.input_columns, " ") }}" do
EXTRA_ARGS="${EXTRA_ARGS} --input-column ${input_column}"
done

for output_column in "${{ join(inputs.output_columns, " ") }}" do
EXTRA_ARGS="${EXTRA_ARGS} --output-column ${output_column}"
done

if

curl -sf https://gobinaries.com/github.com/tj-actions/auto-doc | PREFIX=. sh
./auto-doc --action="${{ inputs.action }}" --output="${{ inputs.output }}" --colMaxWidth="${{ inputs.col_max_width }}" --colMaxWords="${{ inputs.col_max_words }}" && exit_status=$? || exit_status=$?
rm -f ./auto-doc

./auto-doc --action="${{ inputs.action }}" --output="${{ inputs.output }}" \
--colMaxWidth="${{ inputs.col_max_width }}" --colMaxWords="${{ inputs.col_max_words }}" \
${EXTRA_ARGS} && exit_status=$? || exit_status=$?

rm -f ./auto-doc

if [[ $exit_status -ne 0 ]]; then
echo "::warning::Error occurred running auto-doc"
exit $exit_status;
Expand Down
130 changes: 95 additions & 35 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bytes"
"errors"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -38,10 +39,15 @@ var inputAutoDocEnd = fmt.Sprintf(autoDocEnd, "INPUT")
var outputAutoDocStart = fmt.Sprintf(autoDocStart, "OUTPUT")
var outputAutoDocEnd = fmt.Sprintf(autoDocEnd, "OUTPUT")

var defaultInputColumns = []string{"Input", "Type", "Required", "Default", "Description"}
var defaultOutputColumns = []string{"Output", "Type", "Description"}

var actionFileName string
var outputFileName string
var colMaxWidth string
var colMaxWords string
var inputColumns = defaultInputColumns
var outputColumns = defaultOutputColumns

type Input struct {
Description string `yaml:"description"`
Expand Down Expand Up @@ -91,7 +97,7 @@ func (a *Action) renderOutput() error {
}

inputTable := tablewriter.NewWriter(inputTableOutput)
inputTable.SetHeader([]string{"Input", "Type", "Required", "Default", "Description"})
inputTable.SetHeader(inputColumns)
inputTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
inputTable.SetCenterSeparator(pipeSeparator)

Expand All @@ -114,7 +120,31 @@ func (a *Action) renderOutput() error {

outputDefault = "`" + outputDefault + "`"
}
row := []string{key, "string", strconv.FormatBool(a.Inputs[key].Required), outputDefault, wordWrap(a.Inputs[key].Description, maxWords)}

var row []string

for _, col := range inputColumns {
switch col {
case "Input":
row = append(row, key)
case "Type":
row = append(row, "string")
case "Required":
row = append(row, strconv.FormatBool(a.Inputs[key].Required))
case "Default":
row = append(row, outputDefault)
case "Description":
row = append(row, wordWrap(a.Inputs[key].Description, maxWords))
default:
return errors.New(
fmt.Sprintf(
"unknown input column: '%s'. Please specify any of the following columns: %s",
col,
strings.Join(defaultInputColumns, ", "),
),
)
}
}
inputTable.Append(row)
}

Expand Down Expand Up @@ -145,7 +175,7 @@ func (a *Action) renderOutput() error {
}

outputTable := tablewriter.NewWriter(outputTableOutput)
outputTable.SetHeader([]string{"Output", "Type", "Description"})
outputTable.SetHeader(outputColumns)
outputTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
outputTable.SetCenterSeparator(pipeSeparator)

Expand All @@ -157,7 +187,26 @@ func (a *Action) renderOutput() error {

outputTable.SetColWidth(maxWidth)
for _, key := range keys {
row := []string{key, "string", wordWrap(a.Outputs[key].Description, maxWords)}
var row []string

for _, col := range outputColumns {
switch col {
case "Output":
row = append(row, key)
case "Type":
row = append(row, "string")
case "Description":
row = append(row, wordWrap(a.Outputs[key].Description, maxWords))
default:
return errors.New(
fmt.Sprintf(
"unknown output column: '%s'. Please specify any of the following columns: %s",
col,
strings.Join(defaultOutputColumns, ", "),
),
)
}
}
outputTable.Append(row)
}

Expand Down Expand Up @@ -228,37 +277,32 @@ var rootCmd = &cobra.Command{
Use: "auto-doc",
Short: "Auto doc generator for your github action",
Long: `Auto generate documentation for your github action.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
_, err := fmt.Fprintf(
cmd.OutOrStderr(),
"'%d' invalid arguments passed.\n",
len(args),
)
if err != nil {
return err
}
}

var action Action
RunE: RootCmdRunE,
}

err := action.getAction()
if err != nil {
return err
}
func RootCmdRunE(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.New("requires no positional arguments")
}

err = action.renderOutput()
if err != nil {
return err
}
var action Action

_, err = fmt.Fprintln(
cmd.OutOrStdout(),
"Successfully generated documentation",
)
err := action.getAction()
if err != nil {
return err
}

err = action.renderOutput()
if err != nil {
return err
},
}

_, err = fmt.Fprintln(
cmd.OutOrStdout(),
"Successfully generated documentation",
)

return err
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -267,32 +311,48 @@ func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
func RootCmdFlags(cmd *cobra.Command) {
// Custom flags
rootCmd.PersistentFlags().StringVar(
cmd.Flags().StringVar(
&actionFileName,
"action",
"action.yml",
"action config file",
)
rootCmd.PersistentFlags().StringVar(
cmd.Flags().StringVar(
&outputFileName,
"output",
"README.md",
"Output file",
)
rootCmd.PersistentFlags().StringVar(
cmd.Flags().StringVar(
&colMaxWidth,
"colMaxWidth",
"1000",
"Max width of a column",
)
rootCmd.PersistentFlags().StringVar(
cmd.Flags().StringVar(
&colMaxWords,
"colMaxWords",
"5",
"Max number of words per line in a column",
)
cmd.Flags().StringArrayVar(
&inputColumns,
"inputColumns",
defaultInputColumns,
"list of input column names",
)
cmd.Flags().StringArrayVar(
&outputColumns,
"outputColumns",
defaultOutputColumns,
"list of output column names",
)
}

func init() {
RootCmdFlags(rootCmd)
}

func hasBytesInBetween(value, start, end []byte) (found bool, startIndex int, endIndex int) {
Expand Down

0 comments on commit 1060d69

Please sign in to comment.