Skip to content

Commit

Permalink
chore: Move object-impl generator to cmd (#462)
Browse files Browse the repository at this point in the history
## Motivation

Internal Go tools for the project should be kept under `internal/cmd`
instead of `scripts`.
  • Loading branch information
nieomylnieja committed Jun 17, 2024
1 parent d621694 commit ba38285
Show file tree
Hide file tree
Showing 32 changed files with 50 additions and 36 deletions.
2 changes: 2 additions & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ words:
- noprefix
- nriq
- nrql
- objectimpl
- opentsdb
- opsgenie
- pagerduty
Expand Down Expand Up @@ -133,6 +134,7 @@ words:
- timeseries
- timeslice
- timeslices
- tmpl
- tpng
- tsdb
- twindow
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/objectimpl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# docgen

`objectimpl` is a tool that generates boilerplate functions for manifest objects
in order to implement `manifest.Object` interface.

It utilizes `text/template` to generate the code.

## Usage

Add the following `generate` directive to the file that contains
the `manifest.Object` object definition.
Replace the `<OBJECT_KIND>` with the struct name of your object, e.g. `Project`.

```go
//go:generate go run ../../../internal/cmd/objectimpl <OBJECT_KIND>
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"unicode"
)

//go:embed generate-object-impl.tpl
//go:embed object.tmpl
var templateStr string

type generator struct {
Expand Down Expand Up @@ -80,7 +80,7 @@ func main() {
errFatal(err.Error())
}
outputName := filepath.Join(cwd, fmt.Sprintf("%s_object.go", strings.TrimSuffix(filename, ".go")))
if err = os.WriteFile(outputName, formatted, 0600); err != nil {
if err = os.WriteFile(outputName, formatted, 0o600); err != nil {
errFatal(err.Error())
}
}
Expand Down Expand Up @@ -168,11 +168,7 @@ func (g *generator) hasOrganizationAndManifestSource(fields *ast.FieldList) bool
return hasOrganization && hasManifestSource
}

func errFatal(f string, a ...interface{}) {
if len(a) == 0 {
fmt.Fprintln(os.Stderr, f)
} else {
fmt.Fprintf(os.Stderr, f+"\n", a...)
}
func errFatal(f string) {
fmt.Fprintln(os.Stderr, f)
os.Exit(1)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion manifest/v1alpha/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go Agent
//go:generate go run ../../../internal/cmd/objectimpl Agent

// New creates new Agent instance.
func New(metadata Metadata, spec Spec) Agent {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/agent/agent_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go Alert
//go:generate go run ../../../internal/cmd/objectimpl Alert

// New creates a new Alert based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) Alert {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/alert/alert_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/alertmethod/alert_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go AlertMethod
//go:generate go run ../../../internal/cmd/objectimpl AlertMethod

// New creates a new AlertMethod based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) AlertMethod {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/alertmethod/alert_method_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/alertpolicy/alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go AlertPolicy
//go:generate go run ../../../internal/cmd/objectimpl AlertPolicy

func New(metadata Metadata, spec Spec) AlertPolicy {
return AlertPolicy{
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/alertpolicy/alert_policy_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/alertsilence/alert_silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go AlertSilence
//go:generate go run ../../../internal/cmd/objectimpl AlertSilence

// New creates a new AlertSilence based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) AlertSilence {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/alertsilence/alert_silence_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/annotation/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go Annotation
//go:generate go run ../../../internal/cmd/objectimpl Annotation

// New creates a new Annotation based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) Annotation {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/annotation/annotation_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/budgetadjustment/budget_adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go BudgetAdjustment
//go:generate go run ../../../internal/cmd/objectimpl BudgetAdjustment

func New(metadata Metadata, spec Spec) BudgetAdjustment {
return BudgetAdjustment{
Expand Down

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/dataexport/data_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go DataExport
//go:generate go run ../../../internal/cmd/objectimpl DataExport

// New creates a new DataExport based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) DataExport {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/dataexport/data_export_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/direct/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go Direct
//go:generate go run ../../../internal/cmd/objectimpl Direct

func New(metadata Metadata, spec Spec) Direct {
return Direct{
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/direct/direct_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go Project
//go:generate go run ../../../internal/cmd/objectimpl Project

// New creates a new Project based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) Project {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/project/project_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/rolebinding/role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go RoleBinding
//go:generate go run ../../../internal/cmd/objectimpl RoleBinding

func New(metadata Metadata, spec Spec) RoleBinding {
return RoleBinding{
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/rolebinding/role_binding_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go Service
//go:generate go run ../../../internal/cmd/objectimpl Service

// New creates a new Service based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) Service {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/service/service_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/slo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../scripts/generate-object-impl.go SLO
//go:generate go run ../../../internal/cmd/objectimpl SLO

// New creates a new SLO based on provided Metadata nad Spec.
func New(metadata Metadata, spec Spec) SLO {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/slo/slo_object.go

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

2 changes: 1 addition & 1 deletion manifest/v1alpha/usergroup/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/nobl9/nobl9-go/manifest"
)

//go:generate go run ../../../scripts/generate-object-impl.go UserGroup
//go:generate go run ../../../internal/cmd/objectimpl UserGroup

func New(metadata Metadata, spec Spec) UserGroup {
return UserGroup{
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/usergroup/user_group_object.go

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

0 comments on commit ba38285

Please sign in to comment.