From ba382852b9dce67d22c8f8085ba75f4bab2cdbf6 Mon Sep 17 00:00:00 2001 From: Mateusz Hawrus <48822818+nieomylnieja@users.noreply.github.com> Date: Mon, 17 Jun 2024 16:41:36 +0200 Subject: [PATCH] chore: Move object-impl generator to cmd (#462) ## Motivation Internal Go tools for the project should be kept under `internal/cmd` instead of `scripts`. --- cspell.yaml | 2 ++ internal/cmd/objectimpl/README.md | 16 ++++++++++++++++ .../cmd/objectimpl/main.go | 12 ++++-------- .../cmd/objectimpl/object.tmpl | 0 manifest/v1alpha/agent/agent.go | 2 +- manifest/v1alpha/agent/agent_object.go | 2 +- manifest/v1alpha/alert/alert.go | 2 +- manifest/v1alpha/alert/alert_object.go | 2 +- manifest/v1alpha/alertmethod/alert_method.go | 2 +- .../v1alpha/alertmethod/alert_method_object.go | 2 +- manifest/v1alpha/alertpolicy/alert_policy.go | 2 +- .../v1alpha/alertpolicy/alert_policy_object.go | 2 +- manifest/v1alpha/alertsilence/alert_silence.go | 2 +- .../v1alpha/alertsilence/alert_silence_object.go | 2 +- manifest/v1alpha/annotation/annotation.go | 2 +- manifest/v1alpha/annotation/annotation_object.go | 2 +- .../budgetadjustment/budget_adjustment.go | 2 +- .../budgetadjustment/budget_adjustment_object.go | 2 +- manifest/v1alpha/dataexport/data_export.go | 2 +- .../v1alpha/dataexport/data_export_object.go | 2 +- manifest/v1alpha/direct/direct.go | 2 +- manifest/v1alpha/direct/direct_object.go | 2 +- manifest/v1alpha/project/project.go | 2 +- manifest/v1alpha/project/project_object.go | 2 +- manifest/v1alpha/rolebinding/role_binding.go | 2 +- .../v1alpha/rolebinding/role_binding_object.go | 2 +- manifest/v1alpha/service/service.go | 2 +- manifest/v1alpha/service/service_object.go | 2 +- manifest/v1alpha/slo/slo.go | 2 +- manifest/v1alpha/slo/slo_object.go | 2 +- manifest/v1alpha/usergroup/user_group.go | 2 +- manifest/v1alpha/usergroup/user_group_object.go | 2 +- 32 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 internal/cmd/objectimpl/README.md rename scripts/generate-object-impl.go => internal/cmd/objectimpl/main.go (93%) rename scripts/generate-object-impl.tpl => internal/cmd/objectimpl/object.tmpl (100%) diff --git a/cspell.yaml b/cspell.yaml index 821bc5d8..19094f73 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -99,6 +99,7 @@ words: - noprefix - nriq - nrql + - objectimpl - opentsdb - opsgenie - pagerduty @@ -133,6 +134,7 @@ words: - timeseries - timeslice - timeslices + - tmpl - tpng - tsdb - twindow diff --git a/internal/cmd/objectimpl/README.md b/internal/cmd/objectimpl/README.md new file mode 100644 index 00000000..0cc521a5 --- /dev/null +++ b/internal/cmd/objectimpl/README.md @@ -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 `` with the struct name of your object, e.g. `Project`. + +```go +//go:generate go run ../../../internal/cmd/objectimpl +``` diff --git a/scripts/generate-object-impl.go b/internal/cmd/objectimpl/main.go similarity index 93% rename from scripts/generate-object-impl.go rename to internal/cmd/objectimpl/main.go index 5b9da453..cf1c43e1 100644 --- a/scripts/generate-object-impl.go +++ b/internal/cmd/objectimpl/main.go @@ -15,7 +15,7 @@ import ( "unicode" ) -//go:embed generate-object-impl.tpl +//go:embed object.tmpl var templateStr string type generator struct { @@ -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()) } } @@ -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) } diff --git a/scripts/generate-object-impl.tpl b/internal/cmd/objectimpl/object.tmpl similarity index 100% rename from scripts/generate-object-impl.tpl rename to internal/cmd/objectimpl/object.tmpl diff --git a/manifest/v1alpha/agent/agent.go b/manifest/v1alpha/agent/agent.go index dfec435d..138cca8d 100644 --- a/manifest/v1alpha/agent/agent.go +++ b/manifest/v1alpha/agent/agent.go @@ -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 { diff --git a/manifest/v1alpha/agent/agent_object.go b/manifest/v1alpha/agent/agent_object.go index ed510262..06a7375d 100644 --- a/manifest/v1alpha/agent/agent_object.go +++ b/manifest/v1alpha/agent/agent_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Agent"; DO NOT EDIT. +// Code generated by "objectimpl Agent"; DO NOT EDIT. package agent diff --git a/manifest/v1alpha/alert/alert.go b/manifest/v1alpha/alert/alert.go index db91efd8..715d66f4 100644 --- a/manifest/v1alpha/alert/alert.go +++ b/manifest/v1alpha/alert/alert.go @@ -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 { diff --git a/manifest/v1alpha/alert/alert_object.go b/manifest/v1alpha/alert/alert_object.go index 4d71f77a..18a5ff62 100644 --- a/manifest/v1alpha/alert/alert_object.go +++ b/manifest/v1alpha/alert/alert_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Alert"; DO NOT EDIT. +// Code generated by "objectimpl Alert"; DO NOT EDIT. package alert diff --git a/manifest/v1alpha/alertmethod/alert_method.go b/manifest/v1alpha/alertmethod/alert_method.go index 7f67fc31..494979ff 100644 --- a/manifest/v1alpha/alertmethod/alert_method.go +++ b/manifest/v1alpha/alertmethod/alert_method.go @@ -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 { diff --git a/manifest/v1alpha/alertmethod/alert_method_object.go b/manifest/v1alpha/alertmethod/alert_method_object.go index 3bc4bb02..a99a0ee7 100644 --- a/manifest/v1alpha/alertmethod/alert_method_object.go +++ b/manifest/v1alpha/alertmethod/alert_method_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl AlertMethod"; DO NOT EDIT. +// Code generated by "objectimpl AlertMethod"; DO NOT EDIT. package alertmethod diff --git a/manifest/v1alpha/alertpolicy/alert_policy.go b/manifest/v1alpha/alertpolicy/alert_policy.go index 40240a6c..d2da7649 100644 --- a/manifest/v1alpha/alertpolicy/alert_policy.go +++ b/manifest/v1alpha/alertpolicy/alert_policy.go @@ -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{ diff --git a/manifest/v1alpha/alertpolicy/alert_policy_object.go b/manifest/v1alpha/alertpolicy/alert_policy_object.go index 18bd826d..ac8178f7 100644 --- a/manifest/v1alpha/alertpolicy/alert_policy_object.go +++ b/manifest/v1alpha/alertpolicy/alert_policy_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl AlertPolicy"; DO NOT EDIT. +// Code generated by "objectimpl AlertPolicy"; DO NOT EDIT. package alertpolicy diff --git a/manifest/v1alpha/alertsilence/alert_silence.go b/manifest/v1alpha/alertsilence/alert_silence.go index 7e192703..d3f670ed 100644 --- a/manifest/v1alpha/alertsilence/alert_silence.go +++ b/manifest/v1alpha/alertsilence/alert_silence.go @@ -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 { diff --git a/manifest/v1alpha/alertsilence/alert_silence_object.go b/manifest/v1alpha/alertsilence/alert_silence_object.go index 6dcd36cb..f4545c89 100644 --- a/manifest/v1alpha/alertsilence/alert_silence_object.go +++ b/manifest/v1alpha/alertsilence/alert_silence_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl AlertSilence"; DO NOT EDIT. +// Code generated by "objectimpl AlertSilence"; DO NOT EDIT. package alertsilence diff --git a/manifest/v1alpha/annotation/annotation.go b/manifest/v1alpha/annotation/annotation.go index 0de5e48b..be9a745f 100644 --- a/manifest/v1alpha/annotation/annotation.go +++ b/manifest/v1alpha/annotation/annotation.go @@ -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 { diff --git a/manifest/v1alpha/annotation/annotation_object.go b/manifest/v1alpha/annotation/annotation_object.go index 19396dc1..5680fa62 100644 --- a/manifest/v1alpha/annotation/annotation_object.go +++ b/manifest/v1alpha/annotation/annotation_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Annotation"; DO NOT EDIT. +// Code generated by "objectimpl Annotation"; DO NOT EDIT. package annotation diff --git a/manifest/v1alpha/budgetadjustment/budget_adjustment.go b/manifest/v1alpha/budgetadjustment/budget_adjustment.go index 5c437153..4fc47b8a 100644 --- a/manifest/v1alpha/budgetadjustment/budget_adjustment.go +++ b/manifest/v1alpha/budgetadjustment/budget_adjustment.go @@ -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{ diff --git a/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go b/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go index b12fe18c..36cd3b5f 100644 --- a/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go +++ b/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl BudgetAdjustment"; DO NOT EDIT. +// Code generated by "objectimpl BudgetAdjustment"; DO NOT EDIT. package budgetadjustment diff --git a/manifest/v1alpha/dataexport/data_export.go b/manifest/v1alpha/dataexport/data_export.go index e1ff0bed..0a86397b 100644 --- a/manifest/v1alpha/dataexport/data_export.go +++ b/manifest/v1alpha/dataexport/data_export.go @@ -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 { diff --git a/manifest/v1alpha/dataexport/data_export_object.go b/manifest/v1alpha/dataexport/data_export_object.go index 08e06191..b436659f 100644 --- a/manifest/v1alpha/dataexport/data_export_object.go +++ b/manifest/v1alpha/dataexport/data_export_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl DataExport"; DO NOT EDIT. +// Code generated by "objectimpl DataExport"; DO NOT EDIT. package dataexport diff --git a/manifest/v1alpha/direct/direct.go b/manifest/v1alpha/direct/direct.go index 55fb9d7d..0e506838 100644 --- a/manifest/v1alpha/direct/direct.go +++ b/manifest/v1alpha/direct/direct.go @@ -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{ diff --git a/manifest/v1alpha/direct/direct_object.go b/manifest/v1alpha/direct/direct_object.go index 5a8588ee..e0934e0f 100644 --- a/manifest/v1alpha/direct/direct_object.go +++ b/manifest/v1alpha/direct/direct_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Direct"; DO NOT EDIT. +// Code generated by "objectimpl Direct"; DO NOT EDIT. package direct diff --git a/manifest/v1alpha/project/project.go b/manifest/v1alpha/project/project.go index 22144a51..acfa06af 100644 --- a/manifest/v1alpha/project/project.go +++ b/manifest/v1alpha/project/project.go @@ -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 { diff --git a/manifest/v1alpha/project/project_object.go b/manifest/v1alpha/project/project_object.go index 6916973a..cc21e2b0 100644 --- a/manifest/v1alpha/project/project_object.go +++ b/manifest/v1alpha/project/project_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Project"; DO NOT EDIT. +// Code generated by "objectimpl Project"; DO NOT EDIT. package project diff --git a/manifest/v1alpha/rolebinding/role_binding.go b/manifest/v1alpha/rolebinding/role_binding.go index aa577711..04edd002 100644 --- a/manifest/v1alpha/rolebinding/role_binding.go +++ b/manifest/v1alpha/rolebinding/role_binding.go @@ -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{ diff --git a/manifest/v1alpha/rolebinding/role_binding_object.go b/manifest/v1alpha/rolebinding/role_binding_object.go index ae808fd0..021e4602 100644 --- a/manifest/v1alpha/rolebinding/role_binding_object.go +++ b/manifest/v1alpha/rolebinding/role_binding_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl RoleBinding"; DO NOT EDIT. +// Code generated by "objectimpl RoleBinding"; DO NOT EDIT. package rolebinding diff --git a/manifest/v1alpha/service/service.go b/manifest/v1alpha/service/service.go index 52b14721..704a92e6 100644 --- a/manifest/v1alpha/service/service.go +++ b/manifest/v1alpha/service/service.go @@ -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 { diff --git a/manifest/v1alpha/service/service_object.go b/manifest/v1alpha/service/service_object.go index af23bbc1..10702f06 100644 --- a/manifest/v1alpha/service/service_object.go +++ b/manifest/v1alpha/service/service_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Service"; DO NOT EDIT. +// Code generated by "objectimpl Service"; DO NOT EDIT. package service diff --git a/manifest/v1alpha/slo/slo.go b/manifest/v1alpha/slo/slo.go index 86b1add4..0e47569d 100644 --- a/manifest/v1alpha/slo/slo.go +++ b/manifest/v1alpha/slo/slo.go @@ -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 { diff --git a/manifest/v1alpha/slo/slo_object.go b/manifest/v1alpha/slo/slo_object.go index 81891e1d..64cfbcd9 100644 --- a/manifest/v1alpha/slo/slo_object.go +++ b/manifest/v1alpha/slo/slo_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl SLO"; DO NOT EDIT. +// Code generated by "objectimpl SLO"; DO NOT EDIT. package slo diff --git a/manifest/v1alpha/usergroup/user_group.go b/manifest/v1alpha/usergroup/user_group.go index 63e2cca6..8c3b5e8b 100644 --- a/manifest/v1alpha/usergroup/user_group.go +++ b/manifest/v1alpha/usergroup/user_group.go @@ -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{ diff --git a/manifest/v1alpha/usergroup/user_group_object.go b/manifest/v1alpha/usergroup/user_group_object.go index 91d5ace6..374b7ecb 100644 --- a/manifest/v1alpha/usergroup/user_group_object.go +++ b/manifest/v1alpha/usergroup/user_group_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl UserGroup"; DO NOT EDIT. +// Code generated by "objectimpl UserGroup"; DO NOT EDIT. package usergroup