From fdf2238f2a96d8497c538c262a5f226092a4e301 Mon Sep 17 00:00:00 2001 From: dnitsch Date: Mon, 5 May 2025 10:12:28 +0100 Subject: [PATCH 1/2] fix: remove init from cobra --- .../async-api-gen-doc/async_api_gen_doc.go | 122 +++++++++++++++++ ...root_test.go => async_api_gen_doc_test.go} | 9 +- .../cmd/async-api-gen-doc/global_ctx.go | 107 ++++++++------- .../cmd/async-api-gen-doc/global_ctx_test.go | 15 ++- .../cmd/async-api-gen-doc/root.go | 112 ---------------- .../cmd/async-api-gen-doc/single_ctx.go | 123 +++++++++--------- .../cmd/async-api-gen-doc/single_ctx_test.go | 32 +++-- src/go/async-api-gen-doc/cmd/main.go | 7 +- src/go/taskfile.yml | 5 +- 9 files changed, 280 insertions(+), 252 deletions(-) create mode 100755 src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc.go rename src/go/async-api-gen-doc/cmd/async-api-gen-doc/{root_test.go => async_api_gen_doc_test.go} (80%) delete mode 100755 src/go/async-api-gen-doc/cmd/async-api-gen-doc/root.go diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc.go new file mode 100755 index 0000000..197314f --- /dev/null +++ b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc.go @@ -0,0 +1,122 @@ +package asyncapigendoc + +import ( + "context" + "fmt" + "os" + "path/filepath" + + "github.com/dnitsch/async-api-generator/internal/generate" + "github.com/dnitsch/async-api-generator/internal/parser" + "github.com/dnitsch/async-api-generator/internal/storage" + log "github.com/dnitsch/simplelog" + "github.com/spf13/cobra" +) + +var ( + Version string = "0.0.1" + Revision string = "1111aaaa" +) + +type flags struct { + verbose bool + dryRun bool + outputLocation string + inputLocation string +} + +type AsyncApiGenDocCmd struct { + ctx context.Context + Cmd *cobra.Command + logger log.Loggeriface + rootFlags *flags + outputStorageConfig *storage.Conf + inputLocationStorageConfig *storage.Conf +} + +func NewCmd(ctx context.Context) *AsyncApiGenDocCmd { + f := &flags{} + aagd := &AsyncApiGenDocCmd{ + ctx: ctx, + logger: log.New(os.Stderr, log.ErrorLvl), + rootFlags: f, + } + aagd.Cmd = &cobra.Command{ + Use: "gendoc", + Aliases: []string{"aadg", "generator"}, + Short: "Generator for AsyncAPI documents", + Long: `Generator for AsyncAPI documents, functions by performing lexical analysis on source files in a given base directory. + These can then be further fed into other generator tools, e.g. client/server generators`, + Example: "", + SilenceUsage: true, + Version: fmt.Sprintf("%s-%s", Version, Revision), + PreRunE: func(cmd *cobra.Command, args []string) error { + return aagd.setStorageLocation(f.inputLocation, f.outputLocation) + }, + } + aagd.Cmd.PersistentFlags().StringVarP(&f.outputLocation, "output", "o", "local://$HOME/.gendoc", `Output type and destination, currently only supports [local://, azblob://]. if dry-run is set then this is ignored`) + aagd.Cmd.PersistentFlags().StringVarP(&f.inputLocation, "input", "i", "local://.", `Path to start the search in, Must include the protocol - see output for options`) + aagd.Cmd.PersistentFlags().BoolVarP(&f.verbose, "verbose", "v", false, "Verbose output") + aagd.Cmd.PersistentFlags().BoolVarP(&f.dryRun, "dry-run", "", false, "Dry run only runs in validate mode and does not emit anything") + return aagd +} + +func (c *AsyncApiGenDocCmd) WithCommands() { + for _, fn := range []func(*AsyncApiGenDocCmd){singleContextCmd, globalCtxCmd} { + fn(c) + } +} + +func (c *AsyncApiGenDocCmd) Execute() error { + return c.Cmd.ExecuteContext(c.ctx) +} + +// config bootstraps pflags into useable config +func (c *AsyncApiGenDocCmd) config(outConf *storage.Conf, sf *genDocContextFlags) (*generate.Config, func(), error) { + dirName := filepath.Base(outConf.Destination) + + conf := &generate.Config{ + ParserConfig: parser.Config{ServiceRepoUrl: sf.repoUrl, BusinessDomain: sf.businessDomain, BoundedDomain: sf.boundedCtxDomain, ServiceLanguage: sf.repoLang}, + SearchDirName: dirName, + Output: outConf, + } + if sf.isService { + // use the current search dir name as the serviceId + // this allows certain objects to __not__ have parentId or id specified + conf.ParserConfig.ServiceId = dirName + } + + if !c.rootFlags.dryRun { + // create interim local dirs for interim state or interim download storage + interim, err := os.MkdirTemp("", ".gendoc-interim-*") + if err != nil { + return nil, nil, err + } + download, err := os.MkdirTemp("", ".gendoc-download-*") + if err != nil { + return nil, nil, err + } + conf.InterimOutputDir = interim + conf.DownloadDir = download + } + + return conf, func() { + _ = os.RemoveAll(conf.InterimOutputDir) + _ = os.RemoveAll(conf.DownloadDir) + }, nil +} + +// setStorageLocation sets the input/output locations +func (c *AsyncApiGenDocCmd) setStorageLocation(input, output string) error { + inStoreConf, err := storage.ParseStorageOutputConfig(input) + if err != nil { + return err + } + outStoreConf, err := storage.ParseStorageOutputConfig(output) + if err != nil { + return err + } + c.inputLocationStorageConfig = inStoreConf + c.outputStorageConfig = outStoreConf + return nil +} diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/root_test.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc_test.go similarity index 80% rename from src/go/async-api-gen-doc/cmd/async-api-gen-doc/root_test.go rename to src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc_test.go index b665a22..bce18ac 100644 --- a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/root_test.go +++ b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/async_api_gen_doc_test.go @@ -2,6 +2,7 @@ package asyncapigendoc_test import ( "bytes" + "context" "io" "testing" @@ -13,12 +14,12 @@ func Test_root_ok(t *testing.T) { baseDir := "test/foo.sample" b := new(bytes.Buffer) - cmd := asyncapigendoc.AsyncAPIGenCmd - + cmd := asyncapigendoc.NewCmd(context.TODO()) + cmd.WithCommands() fshelper.DebugDirHelper(t, baseDir, "cmd/async-api-gen-doc", "../../") - cmd.SetArgs([]string{"--version"}) - cmd.SetErr(b) + cmd.Cmd.SetArgs([]string{"--version"}) + cmd.Cmd.SetErr(b) cmd.Execute() out, err := io.ReadAll(b) if err != nil { diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx.go index 84e31f6..7af9b42 100644 --- a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx.go +++ b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx.go @@ -11,66 +11,61 @@ import ( "github.com/spf13/cobra" ) -var ( - globalCtxCmd = &cobra.Command{ +func globalCtxCmd(rootCmd *AsyncApiGenDocCmd) { + + cmd := &cobra.Command{ Use: "global-context", Aliases: []string{"gc", "global"}, Short: `Runs the gendoc against a directory containing processed GenDocBlox.`, Long: `Runs the gendoc against a directory containing processed GenDocBlox. Builds a hierarchical tree with the generated interim states across multiple contexts. - Source must be specified [see output] option for examples and structure`, - RunE: globalCtxExecute, +Source must be specified [see output] option for examples and structure`, + RunE: func(cmd *cobra.Command, args []string) error { + if rootCmd.rootFlags.verbose { + rootCmd.logger = log.New(os.Stdout, log.DebugLvl) + } + + conf, cleanUp, err := rootCmd.config(rootCmd.inputLocationStorageConfig, &genDocContextFlags{}) + if err != nil { + return err + } + defer cleanUp() + rootCmd.logger.Debugf("interim output: %s", conf.InterimOutputDir) + rootCmd.logger.Debugf("download output: %s", conf.DownloadDir) + + ctx, cancel := context.WithCancel(cmd.Context()) + defer cancel() + + if err := fetchPrep(ctx, conf, rootCmd.inputLocationStorageConfig); err != nil { + return err + } + + files, err := fshelper.ListFiles(conf.DownloadDir) + if err != nil { + return err + } + + g := generate.New(conf, rootCmd.logger) + + g.LoadInputsFromFiles(files) + + if err := g.ConvertProcessed(); err != nil { + return err + } + + if err := g.BuildContextTree(); err != nil { + return err + } + + if err := g.AsyncAPIFromProcessedTree(); err != nil { + return err + } + return uploadPrep(ctx, g, rootCmd.outputStorageConfig) + }, PreRunE: func(cmd *cobra.Command, args []string) error { - return setStorageLocation(inputLocation, outputLocation) + return rootCmd.setStorageLocation(rootCmd.rootFlags.inputLocation, rootCmd.rootFlags.outputLocation) }, } -) - -func init() { - AsyncAPIGenCmd.AddCommand(globalCtxCmd) -} - -func globalCtxExecute(cmd *cobra.Command, args []string) error { - - if verbose { - logger = log.New(os.Stdout, log.DebugLvl) - } - - conf, cleanUp, err := config(inputLocationStorageConfig) - if err != nil { - return err - } - defer cleanUp() - logger.Debugf("interim output: %s", conf.InterimOutputDir) - logger.Debugf("download output: %s", conf.DownloadDir) - - ctx, cancel := context.WithCancel(cmd.Context()) - defer cancel() - - if err := fetchPrep(ctx, conf, inputLocationStorageConfig); err != nil { - return err - } - - files, err := fshelper.ListFiles(conf.DownloadDir) - if err != nil { - return err - } - - g := generate.New(conf, logger) - - g.LoadInputsFromFiles(files) - - if err := g.ConvertProcessed(); err != nil { - return err - } - - if err := g.BuildContextTree(); err != nil { - return err - } - - if err := g.AsyncAPIFromProcessedTree(); err != nil { - return err - } - return uploadPrep(ctx, g, outputStorageConfig) + rootCmd.Cmd.AddCommand(cmd) } // fetchPrep @@ -81,7 +76,11 @@ func fetchPrep(ctx context.Context, conf *generate.Config, storageConf *storage. return err } - fetchReq := &storage.StorageFetchRequest{Destination: storageConf.Destination, ContainerName: storageConf.TopLevelFolder, EmitPath: conf.DownloadDir} + fetchReq := &storage.StorageFetchRequest{ + Destination: storageConf.Destination, + ContainerName: storageConf.TopLevelFolder, + EmitPath: conf.DownloadDir, + } if err := sc.Fetch(ctx, fetchReq); err != nil { return err diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx_test.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx_test.go index 18f6b87..be36e56 100644 --- a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx_test.go +++ b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/global_ctx_test.go @@ -2,6 +2,7 @@ package asyncapigendoc_test import ( "bytes" + "context" "fmt" "io" "io/fs" @@ -18,15 +19,16 @@ func Test_global_Analyis_runs_ok(t *testing.T) { t.Run("azblob source and output", func(t *testing.T) { t.Skip() - cmd := asyncapigendoc.AsyncAPIGenCmd + cmd := asyncapigendoc.NewCmd(context.TODO()) + cmd.WithCommands() b := new(bytes.Buffer) - cmd.SetArgs([]string{"global-context", "-i", + cmd.Cmd.SetArgs([]string{"global-context", "-i", "azblob://stdevsandboxeuwdev/interim/current", "--output", "azblob://stdevsandboxeuwdev/processed"}) - cmd.SetErr(b) + cmd.Cmd.SetErr(b) cmd.Execute() out, err := io.ReadAll(b) if err != nil { @@ -46,18 +48,19 @@ func Test_global_Analyis_runs_ok(t *testing.T) { } defer os.RemoveAll(out) - cmd := asyncapigendoc.AsyncAPIGenCmd + cmd := asyncapigendoc.NewCmd(context.TODO()) + cmd.WithCommands() baseDir := "test/interim-generated" b := new(bytes.Buffer) output := fmt.Sprintf("local://%s", out) - cmd.SetArgs([]string{"global-context", "-i", + cmd.Cmd.SetArgs([]string{"global-context", "-i", fmt.Sprintf("local://%s", fshelper.DebugDirHelper(t, baseDir, "cmd/async-api-gen-doc", "../../")), "--verbose", "--output", output}) - cmd.SetErr(b) + cmd.Cmd.SetErr(b) cmd.Execute() rb, err := io.ReadAll(b) diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/root.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/root.go deleted file mode 100755 index b634e16..0000000 --- a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/root.go +++ /dev/null @@ -1,112 +0,0 @@ -package asyncapigendoc - -import ( - "context" - "fmt" - "os" - "path/filepath" - - "github.com/dnitsch/async-api-generator/internal/generate" - "github.com/dnitsch/async-api-generator/internal/parser" - "github.com/dnitsch/async-api-generator/internal/storage" - log "github.com/dnitsch/simplelog" - "github.com/spf13/cobra" -) - -var ( - Version string = "0.0.1" - Revision string = "1111aaaa" -) - -var logger = log.New(os.Stderr, log.ErrorLvl) - -var ( - outputStorageConfig *storage.Conf - inputLocationStorageConfig *storage.Conf -) - -var ( - verbose bool - dryRun bool - outputLocation string - inputLocation string -) - -var AsyncAPIGenCmd = &cobra.Command{ - Use: "gendoc", - Aliases: []string{"aadg", "generator"}, - Short: "Generator for AsyncAPI documents", - Long: `Generator for AsyncAPI documents, functions by performing lexical analysis on source files in a given base directory. -These can then be further fed into other generator tools, e.g. client/server generators`, - Example: "", - SilenceUsage: true, - Version: fmt.Sprintf("%s-%s", Version, Revision), - PreRunE: func(cmd *cobra.Command, args []string) error { - return setStorageLocation(inputLocation, outputLocation) - }, -} - -func Execute(ctx context.Context) { - if err := AsyncAPIGenCmd.ExecuteContext(ctx); err != nil { - os.Exit(1) - } - os.Exit(0) -} - -func init() { - AsyncAPIGenCmd.PersistentFlags().StringVarP(&outputLocation, "output", "o", "local://$HOME/.gendoc", `Output type and destination, currently only supports [local://, azblob://]. if dry-run is set then this is ignored`) - AsyncAPIGenCmd.PersistentFlags().StringVarP(&inputLocation, "input", "i", "local://.", `Path to start the search in, Must include the protocol - see output for options`) - AsyncAPIGenCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output") - AsyncAPIGenCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "", false, "Dry run only runs in validate mode and does not emit anything") -} - -// config bootstraps pflags into useable config -// -// TODO: use viper -func config(outConf *storage.Conf) (*generate.Config, func(), error) { - dirName := filepath.Base(outConf.Destination) - - conf := &generate.Config{ - ParserConfig: parser.Config{ServiceRepoUrl: repoUrl, BusinessDomain: businessDomain, BoundedDomain: boundedCtxDomain, ServiceLanguage: repoLang}, - SearchDirName: dirName, - Output: outConf, - } - if isService { - // use the current search dir name as the serviceId - // this allows certain objects to __not__ have parentId or id specified - conf.ParserConfig.ServiceId = dirName - } - - if !dryRun { - // create interim local dirs for interim state or interim download storage - interim, err := os.MkdirTemp("", ".gendoc-interim-*") - if err != nil { - return nil, nil, err - } - download, err := os.MkdirTemp("", ".gendoc-download-*") - if err != nil { - return nil, nil, err - } - conf.InterimOutputDir = interim - conf.DownloadDir = download - } - - return conf, func() { - _ = os.RemoveAll(conf.InterimOutputDir) - _ = os.RemoveAll(conf.DownloadDir) - }, nil -} - -func setStorageLocation(input, output string) error { - inStoreConf, err := storage.ParseStorageOutputConfig(input) - if err != nil { - return err - } - outStoreConf, err := storage.ParseStorageOutputConfig(output) - if err != nil { - return err - } - inputLocationStorageConfig = inStoreConf - outputStorageConfig = outStoreConf - return nil -} diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx.go index 9f2bc56..3258092 100644 --- a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx.go +++ b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx.go @@ -13,78 +13,81 @@ import ( "github.com/spf13/cobra" ) -var ( +type genDocContextFlags struct { businessDomain string boundedCtxDomain string repoUrl string repoLang string isService bool serviceId string - singleCtxCmd = &cobra.Command{ +} + +func singleContextCmd(rootCmd *AsyncApiGenDocCmd) { + f := &genDocContextFlags{} + singleCtxCmd := &cobra.Command{ Use: "single-context", Aliases: []string{"sc", "single"}, Short: `Runs the gendoc against a single repo source`, Long: `Runs the gendoc against a single repo source and emits the output to specified storage.`, - RunE: singleCtxExecute, + RunE: func(cmd *cobra.Command, args []string) error { + if rootCmd.rootFlags.verbose { + rootCmd.logger = log.New(os.Stdout, log.DebugLvl) + } + + conf, cleanUp, err := rootCmd.config(rootCmd.inputLocationStorageConfig, f) + if err != nil { + return err + } + + defer cleanUp() + + files, err := fshelper.ListFiles(rootCmd.inputLocationStorageConfig.Destination) + if err != nil { + return err + } + + gendoc := generate.New(conf, rootCmd.logger) + + gendoc.LoadInputsFromFiles(files) + + if err := gendoc.GenDocBlox(); err != nil { + return err + } + + if rootCmd.rootFlags.dryRun { + rootCmd.logger.Debugf("--dry-run only not storing locally or remotely") + return nil + } + + // set out name for single repo analysis + outName := fmt.Sprintf("current/%s.json", conf.SearchDirName) + // select storage adapter + sc, err := storage.ClientFactory(rootCmd.outputStorageConfig.Typ, rootCmd.outputStorageConfig.Destination) + if err != nil { + return err + } + + storageUpldReq := &storage.StorageUploadRequest{ + ContainerName: rootCmd.outputStorageConfig.TopLevelFolder, + BlobKey: outName, + Destination: filepath.Join(rootCmd.outputStorageConfig.Destination, rootCmd.outputStorageConfig.TopLevelFolder, outName), + } + + ctx, cancel := context.WithCancel(cmd.Context()) + defer cancel() + + return gendoc.CommitInterimState(ctx, sc, storageUpldReq) + }, PreRunE: func(cmd *cobra.Command, args []string) error { - return setStorageLocation(inputLocation, outputLocation) + return rootCmd.setStorageLocation(rootCmd.rootFlags.inputLocation, rootCmd.rootFlags.outputLocation) }, } -) - -func init() { - singleCtxCmd.PersistentFlags().StringVarP(&businessDomain, "business-domain", "b", "", `businessDomain e.g. Warehouse Systems`) - singleCtxCmd.PersistentFlags().StringVarP(&boundedCtxDomain, "bounded-ctx", "c", "", `boundedCtxDomain`) - singleCtxCmd.PersistentFlags().StringVarP(&repoUrl, "repo", "r", "", `repoUrl`) - singleCtxCmd.PersistentFlags().StringVarP(&repoLang, "lang", "", "C#", `Main Language used in repo`) - singleCtxCmd.PersistentFlags().StringVarP(&serviceId, "service-id", "", "", `serviceId`) - singleCtxCmd.PersistentFlags().BoolVarP(&isService, "is-service", "s", false, `whether the repo is a service repo`) - AsyncAPIGenCmd.AddCommand(singleCtxCmd) -} - -func singleCtxExecute(cmd *cobra.Command, args []string) error { - - if verbose { - logger = log.New(os.Stdout, log.DebugLvl) - } - - conf, cleanUp, err := config(inputLocationStorageConfig) - if err != nil { - return err - } - - defer cleanUp() - - files, err := fshelper.ListFiles(inputLocationStorageConfig.Destination) - if err != nil { - return err - } - - gendoc := generate.New(conf, logger) - - gendoc.LoadInputsFromFiles(files) - - if err := gendoc.GenDocBlox(); err != nil { - return err - } - - if dryRun { - logger.Debugf("--dry-run only not storing locally or remotely") - return nil - } - - // set out name for single repo analysis - outName := fmt.Sprintf("current/%s.json", conf.SearchDirName) - // select storage adapter - sc, err := storage.ClientFactory(outputStorageConfig.Typ, outputStorageConfig.Destination) - if err != nil { - return err - } - - storageUpldReq := &storage.StorageUploadRequest{ContainerName: outputStorageConfig.TopLevelFolder, BlobKey: outName, Destination: filepath.Join(outputStorageConfig.Destination, outputStorageConfig.TopLevelFolder, outName)} - - ctx, cancel := context.WithCancel(cmd.Context()) - defer cancel() - return gendoc.CommitInterimState(ctx, sc, storageUpldReq) + singleCtxCmd.PersistentFlags().StringVarP(&f.businessDomain, "business-domain", "b", "", `businessDomain e.g. Warehouse Systems`) + singleCtxCmd.PersistentFlags().StringVarP(&f.boundedCtxDomain, "bounded-ctx", "c", "", `boundedCtxDomain`) + singleCtxCmd.PersistentFlags().StringVarP(&f.repoUrl, "repo", "r", "", `repoUrl`) + singleCtxCmd.PersistentFlags().StringVarP(&f.repoLang, "lang", "", "C#", `Main Language used in repo`) + singleCtxCmd.PersistentFlags().StringVarP(&f.serviceId, "service-id", "", "", `serviceId`) + singleCtxCmd.PersistentFlags().BoolVarP(&f.isService, "is-service", "s", false, `whether the repo is a service repo`) + rootCmd.Cmd.AddCommand(singleCtxCmd) } diff --git a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx_test.go b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx_test.go index 645cdea..71cbda3 100644 --- a/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx_test.go +++ b/src/go/async-api-gen-doc/cmd/async-api-gen-doc/single_ctx_test.go @@ -2,6 +2,7 @@ package asyncapigendoc_test import ( "bytes" + "context" "fmt" "io" "strings" @@ -17,10 +18,12 @@ func Test_single_repo_Analyis_runs_ok(t *testing.T) { searchParentDir := fmt.Sprintf("local://%s", fshelper.DebugDirHelper(t, baseDir, "cmd/async-api-gen-doc", "../../")) t.Run("local output and is-service set", func(t *testing.T) { - cmd := asyncapigendoc.AsyncAPIGenCmd + cmd := asyncapigendoc.NewCmd(context.TODO()) + b := new(bytes.Buffer) - cmd.SetArgs([]string{"single-context", "--verbose", "--input", searchParentDir, "--is-service", "--bounded-ctx", "s2s", "--business-domain", "domain"}) - cmd.SetErr(b) + cmd.Cmd.SetArgs([]string{"single-context", "--verbose", "--input", searchParentDir, "--is-service", "--bounded-ctx", "s2s", "--business-domain", "domain"}) + cmd.Cmd.SetErr(b) + cmd.WithCommands() cmd.Execute() out, err := io.ReadAll(b) if err != nil { @@ -32,10 +35,12 @@ func Test_single_repo_Analyis_runs_ok(t *testing.T) { }) t.Run("dry-run service set", func(t *testing.T) { - cmd := asyncapigendoc.AsyncAPIGenCmd + cmd := asyncapigendoc.NewCmd(context.TODO()) + cmd.WithCommands() + b := new(bytes.Buffer) - cmd.SetArgs([]string{"single-context", "--verbose", "--is-service", "-i", searchParentDir, "--dry-run"}) - cmd.SetErr(b) + cmd.Cmd.SetArgs([]string{"single-context", "--verbose", "--is-service", "-i", searchParentDir, "--dry-run"}) + cmd.Cmd.SetErr(b) cmd.Execute() out, err := io.ReadAll(b) if err != nil { @@ -48,10 +53,12 @@ func Test_single_repo_Analyis_runs_ok(t *testing.T) { t.Run("local input and azblob output", func(t *testing.T) { // uncomment this for local testing only t.Skip() - cmd := asyncapigendoc.AsyncAPIGenCmd + cmd := asyncapigendoc.NewCmd(context.TODO()) + cmd.WithCommands() + b := new(bytes.Buffer) - cmd.SetArgs([]string{"single-context", "--verbose", "--is-service", "-i", searchParentDir, "--bounded-ctx", "s2s", "--business-domain", "domain", "--output", "azblob://stdevsandboxeuwdev/interim"}) - cmd.SetErr(b) + cmd.Cmd.SetArgs([]string{"single-context", "--verbose", "--is-service", "-i", searchParentDir, "--bounded-ctx", "s2s", "--business-domain", "domain", "--output", "azblob://stdevsandboxeuwdev/interim"}) + cmd.Cmd.SetErr(b) cmd.Execute() out, err := io.ReadAll(b) if err != nil { @@ -83,9 +90,10 @@ func Test_Failures_on_incorrect_input(t *testing.T) { t.Run(name, func(t *testing.T) { b := new(bytes.Buffer) - cmd := asyncapigendoc.AsyncAPIGenCmd - cmd.SetArgs(tt.flags) - cmd.SetErr(b) + cmd := asyncapigendoc.NewCmd(context.TODO()) + cmd.WithCommands() + cmd.Cmd.SetArgs(tt.flags) + cmd.Cmd.SetErr(b) err := cmd.Execute() if err == nil { t.Fatal("should have failed with error") diff --git a/src/go/async-api-gen-doc/cmd/main.go b/src/go/async-api-gen-doc/cmd/main.go index febac08..a806224 100644 --- a/src/go/async-api-gen-doc/cmd/main.go +++ b/src/go/async-api-gen-doc/cmd/main.go @@ -2,10 +2,15 @@ package main import ( "context" + "log" asyncapigendoc "github.com/dnitsch/async-api-generator/cmd/async-api-gen-doc" ) func main() { - asyncapigendoc.Execute(context.Background()) + cmd := asyncapigendoc.NewCmd(context.Background()) + cmd.WithCommands() + if err := cmd.Execute(); err != nil { + log.Fatal(err) + } } diff --git a/src/go/taskfile.yml b/src/go/taskfile.yml index 1e4faf3..6de2cf0 100644 --- a/src/go/taskfile.yml +++ b/src/go/taskfile.yml @@ -30,8 +30,7 @@ tasks: - test_prereq cmd: | set -exo pipefail - go test ./... -timeout 30s -v -mod=readonly -race -coverprofile=.coverage/out > .coverage/test.out - cat .coverage/test.out + go test ./... -timeout 30s -v -mod=readonly -race -coverprofile=.coverage/out | tee .coverage/test.out install: desc: Install dependencies @@ -79,7 +78,7 @@ tasks: Generates all the binaries for the gendoc utility. can be called from inside CI like this: - + `task async-api-generator:bin GIT_TAG=0.1.2 REVISION=$commit_sha` deps: - clean_go From 8abdcf40cd08bdeb9a1892bcaf97f41b2271bf52 Mon Sep 17 00:00:00 2001 From: dnitsch Date: Mon, 5 May 2025 10:34:02 +0100 Subject: [PATCH 2/2] fix: omitted mistake --- docs/usage.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 10bde0e..afe3673 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -106,12 +106,12 @@ gendoc global-context --input local:///path/to/src/domain.sample --output local: ### Local Example -Point it to an input directory of any repo - e.g. `domain.Packing.DirectDespatchAggregation`. +Point it to an input directory of any repo - e.g. `domain.Foo.BarActionQux`. This will generate the interim code that the ```sh -gendoc single-context --input local://$FULL_PATH_TO/domain.Packing.DirectDespatchAggregation --is-service --bounded-ctx Packing --business-domain domain \ +gendoc single-context --input local://$FULL_PATH_TO/domain.Foo.BarActionQux --is-service --bounded-ctx Foo --business-domain domain \ --repo "https://github.com/repo" \ --output local://$HOME/.gendoc/poc ``` @@ -124,4 +124,4 @@ This is then used as in input for the global-context and it will output a full A gendoc global-context --input local://$HOME/.gendoc/poc/current --output local://$HOME/.gendoc/poc/processed ``` -The files are emitted with the `AsyncAPI.ID` as the name in the `asyncapi` directory, e.g.: `asyncapi/urn:domain:Packing:domain.Packing.DirectDespatchAggregation.yml`. +The files are emitted with the `AsyncAPI.ID` as the name in the `asyncapi` directory, e.g.: `asyncapi/urn:domain:Foo:domain.Foo.BarActionQux.yml`.