Skip to content

Commit

Permalink
added outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhristof committed Dec 7, 2020
1 parent 9f98bd1 commit fede587
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 3 deletions.
87 changes: 87 additions & 0 deletions action/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package action

import (
"bytes"
"text/template"

"gopkg.in/yaml.v2"
)

type Input struct {
Description string `yaml:"description"`
Required bool `yaml:"required"`
Default string `yaml:"default"`
}

type Output struct {
Description string `yaml:"description"`
Value string `yaml:"value"`
}

type Config struct {
Description string `yaml:"description"`
Inputs map[string]Input `yaml:"inputs"`
Name string `yaml:"name"`
Outputs map[string]Output `yaml:"outputs"`
Runs struct {
Steps []struct {
ID string `yaml:"id"`
Run string `yaml:"run"`
Shell string `yaml:"shell"`
} `yaml:"steps"`
Using string `yaml:"using"`
} `yaml:"runs"`
}

func Load(yamlData []byte) (*Config, error) {
var ret Config
err := yaml.Unmarshal(yamlData, &ret)

if err != nil {
return nil, err
}

return &ret, nil
}

var md = `# {{ .Name }}
{{ .Description }}
## Inputs
| Name | Description | Default | Required |
| ---- | ----------- | ------- | -------- |
{{ range $key, $value := .Inputs -}}
| {{ $key }} | {{$value.Description}} | {{$value.Default }} | {{ $value.Required }}|
{{ end }}
## Outputs
| Name | Description |
| ---- | ----------- |
{{ range $key, $value := .Outputs -}}
| {{ $key }} | {{$value.Description}} |
{{ end }}`

func (c *Config) Markdown() string {
tmpl, err := template.New("markdown").Parse(md)
if err != nil {
panic(err)
}

var tpl bytes.Buffer
err = tmpl.Execute(&tpl, struct {
Name string
Description string
Inputs map[string]Input
Outputs map[string]Output
}{
Name: c.Name,
Description: c.Description,
Inputs: c.Inputs,
Outputs: c.Outputs,
})

return tpl.String()
}
87 changes: 87 additions & 0 deletions action/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package action

import (
"testing"

"github.com/MakeNowJust/heredoc"
"github.com/stretchr/testify/assert"
)

var githubExample = heredoc.Doc(`
# example from https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/creating-a-composite-run-steps-action#creating-an-action-metadata-file
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "::set-output name=random-id::$(echo $RANDOM)"
shell: bash
- run: ${{ github.action_path }}/goodbye.sh
shell: bash
`)

func TestLoadl(t *testing.T) {
var cases = []struct {
name string
yaml string
}{
{
name: "simple yaml config",
yaml: githubExample,
},
}

for _, test := range cases {
_, err := Load([]byte(test.yaml))
assert.Equal(t, nil, err, test.name)
}
}

func TestMarkdown(t *testing.T) {
var cases = []struct {
name string
yaml string
expected string
}{
{
name: "sample yaml config",
yaml: githubExample,
expected: heredoc.Doc(`
# Hello World
Greet someone
## Inputs
| Name | Description | Default | Required |
| ---- | ----------- | ------- | -------- |
| who-to-greet | Who to greet | World | true|
## Outputs
| Name | Description |
| ---- | ----------- |
| random-number | Random number |
`),
},
}

for _, test := range cases {
cfg, err := Load([]byte(test.yaml))
assert.Equal(t, nil, err, test.name)
assert.Equal(t, test.expected, cfg.Markdown(), test.name)
}
}
29 changes: 26 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,44 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/mhristof/githubactions-docs/action"
"github.com/mhristof/githubactions-docs/log"
"github.com/spf13/cobra"
)

var version = "devel"

var rootCmd = &cobra.Command{
Use: "githubactions-docs",
Short: "Generate documentation for Github Actions similar to terraform-docs",
Long: `TODO: changeme`,
Use: "githubactions-docs",
Short: "Generate documentation for Github Actions similar to terraform-docs",
Version: version,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Verbose(cmd)

if _, err := os.Stat(args[0]); os.IsNotExist(err) {
panic(fmt.Sprintf("Error, file %s does not exist", args[0]))
}

data, err := ioutil.ReadFile(args[0])
if err != nil {
log.WithFields(log.Fields{
"err": err,
"args[0]": args[0],
}).Error("Could not load file")
}

cfg, err := action.Load(data)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Could not decode action file")
}

fmt.Println(cfg.Markdown())
},
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.14

require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504
github.com/mhristof/go-update v0.1.0
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab
github.com/sirupsen/logrus v1.5.0
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.2.8
)
Loading

0 comments on commit fede587

Please sign in to comment.