Skip to content

Commit

Permalink
Merge 3d60605 into 8d207ed
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Dec 23, 2021
2 parents 8d207ed + 3d60605 commit 0329609
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 89 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Run auto-doc using the current working directory
run:
make test

- name: Run auto-doc using the current working directory
run:
make run
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ terraform.rc

# Dependency directories (remove the comment below to include it)
vendor/


bin/
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ help:
.PHONY: clean
clean: ## Clean binary file
@echo "Cleaning binary..."
@rm -f auto_doc
@rm -rf bin

guard-%: ## Checks that env var is set else exits with non 0 mainly used in CI;
@if [ -z '${${*}}' ]; then echo 'Environment variable $* not set' && exit 1; fi

.PHONY: build
build: ## Compile go modules
build: clean ## Compile go modules
@echo "Compiling *.go..."
@go build -o auto_doc *.go
@go build -o ./bin/auto_doc *.go

.PHONY: run
run: build guard-OUTPUT guard-ACTION ## Execute binary
@echo "Running auto doc..."
@./auto_doc --action=$(ACTION) --output=$(OUTPUT)
@./bin/auto_doc --action=$(ACTION) --output=$(OUTPUT)
@$(MAKE) clean

.PHONY: test
test: clean
@go test ./...

.PHONY: format
format: ## Format go modules
@go fmt ./...
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ Auto generate documentation from actions.yml like [this](#inputs) by simply addi

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

| INPUT | REQUIRED | DEFAULT | DESCRIPTION |
|--------|----------|------------|-----------------------------|
| action | true | action.yml | Path to the action.yml file |
| output | true | README.md | Output file |
| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|---------------|--------|----------|------------|-----------------------------|
| action | string | false | action.yml | Path to the action.yml file |
| col_max_width | string | false | 1000 | Max width of a column |
| output | string | false | README.md | Path to the output file |

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

Expand Down
12 changes: 8 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ author: jackton1
inputs:
action:
description: 'Path to the action.yml file'
required: true
required: false
default: action.yml
col_max_width:
description: 'Max width of a column'
required: false
default: '1000'
output:
description: 'Output file'
required: true
description: 'Path to the output file'
required: false
default: README.md

runs:
using: 'composite'
steps:
- run: |
curl -sf https://gobinaries.com/github.com/tj-actions/auto-doc@1.1.5 | PREFIX=. sh
./auto-doc --action="${{ inputs.action }}" --output="${{ inputs.output }}"
./auto-doc --action="${{ inputs.action }}" --output="${{ inputs.output }}" --colMaxWidth="${{ inputs.col_max_width }}"
rm -f ./auto-doc
shell: bash
branding:
Expand Down
91 changes: 58 additions & 33 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
Expand All @@ -41,6 +39,7 @@ var outputAutoDocEnd = fmt.Sprintf(AutoDocEnd, "OUTPUT")

var actionFileName string
var outputFileName string
var colMaxWidth string

type Input struct {
Description string `yaml:"description"`
Expand All @@ -58,32 +57,34 @@ type Action struct {
Outputs map[string]Output `yaml:"outputs,omitempty"`
}

func (a *Action) getAction() *Action {
func (a *Action) getAction() error {
actionYaml, err := ioutil.ReadFile(actionFileName)
if err != nil {
cobra.CheckErr(err)
return err
}

err = yaml.Unmarshal(actionYaml, &a)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}

return a
return err
}

func (a *Action) renderOutput() {
func (a *Action) renderOutput() error {
var err error
maxWidth, err := strconv.Atoi(colMaxWidth)
if err != nil {
return err
}

inputTableOutput := &strings.Builder{}

if len(a.Inputs) > 0 {
_, err = fmt.Fprintln(inputTableOutput, inputAutoDocStart)
if err != nil {
cobra.CheckErr(err)
return err
}

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

Expand All @@ -93,26 +94,28 @@ func (a *Action) renderOutput() {
}
sort.Strings(keys)

inputTable.SetColWidth(maxWidth)

for _, key := range keys {
row := []string{key, strconv.FormatBool(a.Inputs[key].Required), a.Inputs[key].Default, a.Inputs[key].Description}
row := []string{key, "string", strconv.FormatBool(a.Inputs[key].Required), a.Inputs[key].Default, a.Inputs[key].Description}
inputTable.Append(row)
}

_, err = fmt.Fprintln(inputTableOutput)
if err != nil {
cobra.CheckErr(err)
return err
}

inputTable.Render()

_, err = fmt.Fprintln(inputTableOutput)
if err != nil {
cobra.CheckErr(err)
return err
}

_, err = fmt.Fprint(inputTableOutput, inputAutoDocEnd)
if err != nil {
cobra.CheckErr(err)
return err
}
}

Expand All @@ -121,7 +124,7 @@ func (a *Action) renderOutput() {
if len(a.Outputs) > 0 {
_, err = fmt.Fprintln(outputTableOutput, outputAutoDocStart)
if err != nil {
cobra.CheckErr(err)
return err
}

outputTable := tablewriter.NewWriter(outputTableOutput)
Expand All @@ -135,60 +138,60 @@ func (a *Action) renderOutput() {
}
sort.Strings(keys)

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

_, err = fmt.Fprintln(outputTableOutput)
if err != nil {
cobra.CheckErr(err)
return err
}

outputTable.Render()

_, err = fmt.Fprintln(outputTableOutput)
if err != nil {
cobra.CheckErr(err)
return err
}

_, err = fmt.Fprint(outputTableOutput, outputAutoDocEnd)
if err != nil {
cobra.CheckErr(err)
return err
}
}

input, err := ioutil.ReadFile(outputFileName)

if err != nil {
cobra.CheckErr(err)
return err
}

var output = []byte("")

hasInputsData, inputStartIndex, inputEndIndex := HasBytesInBetween(
hasInputsData, inputStartIndex, inputEndIndex := hasBytesInBetween(
input,
[]byte(InputsHeader),
[]byte(inputAutoDocEnd),
)

if hasInputsData {
inputsStr := fmt.Sprintf("%s\n\n%v", InputsHeader, inputTableOutput.String())
output = ReplaceBytesInBetween(input, inputStartIndex, inputEndIndex, []byte(inputsStr))
output = replaceBytesInBetween(input, inputStartIndex, inputEndIndex, []byte(inputsStr))
} else {
inputsStr := fmt.Sprintf("%s\n\n%v", InputsHeader, inputTableOutput.String())
output = bytes.Replace(input, []byte(InputsHeader), []byte(inputsStr), -1)
}

hasOutputsData, outputStartIndex, outputEndIndex := HasBytesInBetween(
hasOutputsData, outputStartIndex, outputEndIndex := hasBytesInBetween(
output,
[]byte(OutputsHeader),
[]byte(outputAutoDocEnd),
)

if hasOutputsData {
outputsStr := fmt.Sprintf("%s\n\n%v", OutputsHeader, outputTableOutput.String())
output = ReplaceBytesInBetween(output, outputStartIndex, outputEndIndex, []byte(outputsStr))
output = replaceBytesInBetween(output, outputStartIndex, outputEndIndex, []byte(outputsStr))
} else {
outputsStr := fmt.Sprintf("%s\n\n%v", OutputsHeader, outputTableOutput.String())
output = bytes.Replace(output, []byte(OutputsHeader), []byte(outputsStr), -1)
Expand All @@ -199,29 +202,45 @@ func (a *Action) renderOutput() {
cobra.CheckErr(err)
}
}

return nil
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "auto-doc",
Short: "Auto doc generator for your github action",
Long: `Auto generate documentation for your github action.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
_, err := fmt.Fprintf(
os.Stderr,
cmd.OutOrStderr(),
"'%d' invalid arguments passed.\n",
len(args),
)
if err != nil {
cobra.CheckErr(err)
return err
}
return
}

var action Action
action.getAction()
action.renderOutput()

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
},
}

Expand All @@ -245,9 +264,15 @@ func init() {
"README.md",
"Output file",
)
rootCmd.PersistentFlags().StringVar(
&colMaxWidth,
"colMaxWidth",
"1000",
"Column max width",
)
}

func HasBytesInBetween(value, start, end []byte) (found bool, startIndex int, endIndex int) {
func hasBytesInBetween(value, start, end []byte) (found bool, startIndex int, endIndex int) {
s := bytes.Index(value, start)

if s == -1 {
Expand All @@ -263,7 +288,7 @@ func HasBytesInBetween(value, start, end []byte) (found bool, startIndex int, en
return true, s, e + len(end)
}

func ReplaceBytesInBetween(value []byte, startIndex int, endIndex int, new []byte) []byte {
func replaceBytesInBetween(value []byte, startIndex int, endIndex int, new []byte) []byte {
t := make([]byte, len(value)+len(new))
w := 0

Expand Down
35 changes: 35 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)

func Test_rootCommand(t *testing.T) {
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"--action", "../test/action.yml", "--output", "../test/README.md"})
err := cmd.Execute()

if err != nil {
t.Fatal(err)
}

out, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
}

exp := fmt.Sprintln("Successfully generated documentation")

if string(out) != exp {
t.Fatalf(
"expected \"%s\" got \"%s\"",
exp,
string(out),
)
}
}

0 comments on commit 0329609

Please sign in to comment.