Skip to content

Commit

Permalink
feat: add generate command and basic writer
Browse files Browse the repository at this point in the history
  • Loading branch information
matty-rose committed Oct 18, 2021
1 parent 90ac739 commit c34ae68
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2021 Matt Rose <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/matty-rose/gha-docs/pkg/parser"
"github.com/matty-rose/gha-docs/pkg/writer"
)

// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate [PATH]",
Short: "Generate documentation for a composite GitHub action.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
action, err := parser.Parse(args[0])
if err != nil {
return errors.Wrap(err, "couldn't parse the action file")
}

doc := writer.Write(action)
fmt.Println(doc)
return nil
},
}

func init() {
rootCmd.AddCommand(generateCmd)
}
67 changes: 67 additions & 0 deletions pkg/writer/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright © 2021 Matt Rose <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package writer

import (
"strconv"

"github.com/matty-rose/gha-docs/pkg/formatter"
"github.com/matty-rose/gha-docs/pkg/types"
)

func Write(action *types.CompositeAction) string {
doc := formatter.NewMarkdownDocument()

doc.WriteHeading(action.Name, 1)
doc.WriteText(action.Description)
doc.WriteNewLine()

if len(action.Inputs) != 0 {
var inputs [][]string
for _, inp := range action.Inputs {
inputs = append(inputs, []string{inp.Name, inp.Description, strconv.FormatBool(inp.Required), inp.Description})
}

doc.WriteNewLine()
doc.WriteHeading("Inputs", 2)
_, _ = doc.WriteTable(
[]string{"Name", "Description", "Required", "Default"},
inputs,
)
}

if len(action.Outputs) != 0 {
var outputs [][]string
for _, out := range action.Outputs {
outputs = append(outputs, []string{out.Name, out.Description, out.Value})
}

doc.WriteNewLine()
doc.WriteHeading("Outputs", 2)
_, _ = doc.WriteTable(
[]string{"Name", "Description", "Value"},
outputs,
)
}

return doc.Render()
}

0 comments on commit c34ae68

Please sign in to comment.