Skip to content

Commit

Permalink
Merge pull request #267 from mesg-foundation/258-generate-service-doc…
Browse files Browse the repository at this point in the history
…umentation

Task to generate a default documentation for service
  • Loading branch information
antho1404 authored Jul 4, 2018
2 parents 1c6aef3 + 6fe4c9c commit 3ba8c7c
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ exclude_patterns:
- "**/*_test.go"
- "**/*.d.ts"
- "**/*.pb.go"
- "**/service/schema.go"
- "**/service/assets/*.go"
- "**/cmd/service/assets/*.go"
- "**/Dockerfile"

3 changes: 2 additions & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ exclude_patterns:
- "**/*_test.go"
- "**/*.d.ts"
- "**/*.pb.go"
- "**/service/schema.go"
- "**/service/assets/*.go"
- "**/cmd/service/assets/*.go"
plugins:
gofmt:
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#### Changed
#### Added

- (#267) Add Command `service gen-doc` that generate a `README.md` in the service based on the informations of the `mesg.yml`

#### Removed
#### Fixed

Expand Down
1 change: 1 addition & 0 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
Service.AddCommand(service.Init)
Service.AddCommand(service.Delete)
Service.AddCommand(service.Logs)
Service.AddCommand(service.Docs)

RootCmd.AddCommand(Service)
}
239 changes: 239 additions & 0 deletions cmd/service/assets/readmeTemplate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions cmd/service/assets/readmeTemplate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# {{.Name}}

{{.Description}}
{{if .Repository}}
```bash
mesg-core service deploy {{.Repository}}
```
{{end}}
{{if .Events}}# Events
{{range $key, $event := .Events}}
## {{$key}}

Event key: `{{$key}}`

{{$event.Description}}

{{if $event.Data}}| **Key** | **Type** | **Description** |
| --- | --- | --- |
{{range $dataKey, $data := $event.Data}}| **{{$dataKey}}** | `{{$data.Type}}` | {{$data.Description}} |
{{end}}{{end}}{{end}}{{end}}
{{if .Tasks}}
# Tasks
{{range $key, $task := .Tasks}}
## {{$key}}

Task key: `{{$key}}`

{{$task.Description}}

{{if $task.Inputs}}### Inputs

| **Key** | **Type** | **Description** |
| --- | --- | --- |
{{range $inputKey, $input := $task.Inputs}}| **{{$inputKey}}** | `{{$input.Type}}` | {{$input.Description}} |
{{end}}{{end}}

{{if $task.Outputs}}### Outputs

{{range $outputKey, $output := $task.Outputs}}##### {{$outputKey}}

Output key: `{{$outputKey}}`

{{$output.Description}}

| **Key** | **Type** | **Description** |
| --- | --- | --- |
{{range $outputKey, $output := $output.Data}}| **{{$outputKey}}** | `{{$output.Type}}` | {{$output.Description}} |
{{end}}
{{end}}{{end}}

{{end}}{{end}}
55 changes: 55 additions & 0 deletions cmd/service/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package service

import (
"fmt"
"html/template"
"os"
"path/filepath"

"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/cmd/service/assets"
"github.com/mesg-foundation/core/cmd/utils"
s "github.com/mesg-foundation/core/service"
"github.com/spf13/cobra"
survey "gopkg.in/AlecAivazis/survey.v1"
)

// Docs a service to the marketplace
var Docs = &cobra.Command{
Use: "gen-doc",
Short: "Generate the documentation for the service in a README.md file",
Example: `mesg-core service gen-doc
mesg-core service gen-doc ./PATH_TO_SERVICE`,
Run: genDocHandler,
DisableAutoGenTag: true,
}

func genDocHandler(cmd *cobra.Command, args []string) {
path := defaultPath(args)
readmePath := filepath.Join(path, "README.md")
if _, err := os.Stat(readmePath); err == nil {
var value bool
if survey.AskOne(&survey.Confirm{Message: "The file README.md already exists. Do you want to overwrite it?"}, &value, nil) != nil {
return
}
if !value {
return
}
}

service, err := s.ImportFromPath(path)
utils.HandleError(err)

f, err := os.OpenFile(readmePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer f.Close()

readmeTemplate, err := assets.Asset("cmd/service/assets/readmeTemplate.md")
utils.HandleError(err)

tmpl, err := template.New("doc").Parse(string(readmeTemplate))
utils.HandleError(err)
err = tmpl.Execute(f, service)
utils.HandleError(err)

fmt.Println(aurora.Green("File README.md generated with success"))
}
Loading

0 comments on commit 3ba8c7c

Please sign in to comment.