Skip to content

Commit

Permalink
add readme generation on update
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Jul 17, 2019
1 parent 85666ed commit a91f855
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 4 deletions.
2 changes: 1 addition & 1 deletion a_main-packr.go

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

8 changes: 8 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ func updateCmd(cmd *cobra.Command, args []string) {
logrus.Info("generated k8s/deployment.yaml")
}

// README.md
readme := generate.NewReadme(TemplateFilesystem, service.Interface, tplContext)
if err := readme.Update(); err != nil {
logrus.Errorf("failed to generate README.md: %s", err.Error())
} else {
logrus.Info("generated README.md")
}

// update config metadata
config.Set("godin.version", internal.Version)
config.Set("project.updated", time.Now().Format(time.RFC1123))
Expand Down
37 changes: 37 additions & 0 deletions internal/generate/readme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package generate

import (
"github.com/gobuffalo/packr"
"github.com/lukasjarosch/godin/internal/template"
"github.com/vetcher/go-astra/types"
)

type Readme struct {
BaseGenerator
}

func NewReadme(box packr.Box, serviceInterface *types.Interface, ctx template.Context, options ...Option) *Readme {
defaults := &Options{
Context: ctx,
Overwrite: true,
IsGoSource: false,
Template: "readme",
TargetFile: "README.md",
}

for _, opt := range options {
opt(defaults)
}

return &Readme{
BaseGenerator{
box: box,
iface: serviceInterface,
opts: defaults,
},
}
}

func (s *Readme) Update() error {
return s.GenerateFull()
}
3 changes: 2 additions & 1 deletion internal/godin/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func (p *Project) InitializeConfiguration() {
config.Set("project.created", time.Now().Format(time.RFC1123))

config.Set("service.middleware.logging", true)
config.Set("service.middleware.recovery", true)
config.Set("service.middleware.recovery", false)
config.Set("service.middleware.authorization", false)
config.Set("service.middleware.caching", false)
config.Set("service.middleware.monitoring", false)
config.Set("transport.grpc.enabled", true)

// prompt for required data and save it to config
Expand Down
11 changes: 11 additions & 0 deletions internal/template/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/lukasjarosch/godin/internal/bundle/transport"
"strings"
"time"

"github.com/go-godin/rabbitmq"
"github.com/lukasjarosch/godin/internal/bundle"
Expand All @@ -20,6 +21,7 @@ type Context struct {
Godin Godin
Protobuf Protobuf
Docker Docker
Project Project
}

// NewContextFromConfig will initialize the context will all the data from the configuration
Expand Down Expand Up @@ -56,6 +58,10 @@ func NewContextFromConfig() Context {
Package: config.GetString("protobuf.package"),
Service: config.GetString("protobuf.service"),
},
Project:Project{
Created: config.GetTime("project.created"),
Updated: config.GetTime("project.updated"),
},
Godin: Godin{
Version: internal.Version,
Build: internal.Build,
Expand Down Expand Up @@ -118,6 +124,11 @@ type Godin struct {
Build string
}

type Project struct {
Created time.Time
Updated time.Time
}

type Service struct {
Name string
Namespace string
Expand Down
18 changes: 17 additions & 1 deletion internal/template/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Masterminds/sprig"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
config "github.com/spf13/viper"
)

// file template rendering
Expand Down Expand Up @@ -57,7 +58,22 @@ func (f *File) Render(fs packr.Box, templateContext Context) (rendered []byte, e
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("FindString: %s", f.templates[0]))
}
f.tpl, err = template.New(path.Base(f.templates[0])).Funcs(sprig.TxtFuncMap()).Parse(tmp)
f.tpl, err = template.New(path.Base(f.templates[0])).Funcs(sprig.TxtFuncMap()).Funcs(map[string]interface{}{
"ReadmeOptionCheckbox": func(option string) string {
if !config.IsSet(option) {
img := "![disabled](https://img.icons8.com/color/24/000000/close-window.png)"
return img
}

if config.GetBool(option) {
img := "![enabled](https://img.icons8.com/color/24/000000/checked.png)"
return img
}
img := "![disabled](https://img.icons8.com/color/24/000000/close-window.png)"
return img

},
}).Parse(tmp)
if err != nil {
return nil, errors.Wrap(err, "Parse")
}
Expand Down
73 changes: 72 additions & 1 deletion templates/readme.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
# [{{ .Service.Namespace }}] {{ .Service.Name }}
> Powered by Godin v{{ .Godin.Version }} ({{ .Godin.Commit }})
> Powered by Godin {{ .Godin.Version }} ({{ .Godin.Commit }})

* **Godin init:** {{ .Project.Created }}
* **Last godin update :** {{ .Project.Updated }}

## gRPC service: {{ .Protobuf.Service }}
{{- range .Service.Methods }}
**{{ .Name }}**
{{ range .Comments }}
*{{ replace "// " "" . }}*
{{- end }}
```go
func {{ .Name }}({{ .ParamList }}) ({{ .ReturnList }})
```
{{- end }}

{{- if .Service.Transport.AMQP }}
## AMQP subscriptions

All handlers are located in: `./internal/service/subscriber/`
Each handler has it's own file, named after the subscription topic.

| **Routing key** | **Exchange** | **Queue** | **Handler** |
|-----------------|--------------|-----------|-------------|
{{- range .Service.Subscriber }}
| {{ .Subscription.Topic }} | {{ .Subscription.Exchange }} | {{ .Subscription.Queue.Name }} | {{ .Handler }} |
{{- end }}
{{- end }}

## Transport Options
| **Option** | **Enabled** |
|--------------|----------------------------------------------------------------------------------|
| gRPC Transport layer | {{ ReadmeOptionCheckbox "transport.grpc.enabled" }} |
| gRPC Server | {{ ReadmeOptionCheckbox "transport.grpc.server" }} |
| gRPC Client | {{ ReadmeOptionCheckbox "transport.grpc.client" }} |
| AMQP Transport | {{ ReadmeOptionCheckbox "transport.amqp.enabled" }} |
| AMQP Subscriber | {{ ReadmeOptionCheckbox "transport.amqp.subscriber" }} |
| AMQP Publisher | {{ ReadmeOptionCheckbox "transport.amqp.publisher" }} |

## Endpoint middleware

Subscription middleware is automatically injected by Godin. It is provided by: [go-godin/middleware](github.com/go-godin/middleware)

| **Middleware** | **Enabled** |
|--------------|----------------------------------------------------------------------------------|
| InstrumentGRPC | ![enabled](https://img.icons8.com/color/24/000000/checked.png)
| Logging | ![enabled](https://img.icons8.com/color/24/000000/checked.png)
| RequestID | ![enabled](https://img.icons8.com/color/24/000000/checked.png)

## Service middleware

Service middleware is use-case specific middleware which the developer has to take care of.
Godin only assits in creating and maintaining service middleware, but will never overwrite middleware.

| **Middleware** | **Enabled** |
|--------------|----------------------------------------------------------------------------------|
| Authorization | {{ ReadmeOptionCheckbox "service.middleware.authorization" }}
| Caching | {{ ReadmeOptionCheckbox "service.middleware.caching" }}
| Logging | {{ ReadmeOptionCheckbox "service.middleware.logging" }}
| Recovery | {{ ReadmeOptionCheckbox "service.middleware.recovery" }}
| Monitoring | {{ ReadmeOptionCheckbox "service.middleware.monitoring" }}

{{- if .Service.Transport.AMQP }}
## Subscription middleware

Subscription middleware is automatically injected by Godin. It is provided by: [go-godin/middleware/amqp](github.com/go-godin/middleware/amqp)

| **Middleware** | **Enabled** |
|--------------|----------------------------------------------------------------------------------|
| RequestID | ![enabled](https://img.icons8.com/color/24/000000/checked.png)
| Logging | ![enabled](https://img.icons8.com/color/24/000000/checked.png)
| PrometheusInstrumentation | ![enabled](https://img.icons8.com/color/24/000000/checked.png)
{{- end }}

0 comments on commit a91f855

Please sign in to comment.