diff --git a/cmd/generate.go b/cmd/generate.go new file mode 100644 index 0000000..9b9f1e5 --- /dev/null +++ b/cmd/generate.go @@ -0,0 +1,53 @@ +/* +Copyright © 2021 Matt Rose + +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) +} diff --git a/pkg/writer/writer.go b/pkg/writer/writer.go new file mode 100644 index 0000000..63e8f20 --- /dev/null +++ b/pkg/writer/writer.go @@ -0,0 +1,67 @@ +/* +Copyright © 2021 Matt Rose + +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() +}