From 03a19be319461e819904bc083d87468c64fed77c Mon Sep 17 00:00:00 2001 From: Costas Papastathis Date: Wed, 26 Jun 2024 16:03:58 +0300 Subject: [PATCH] adding on jam summarize for extension --- commands/summarize.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/commands/summarize.go b/commands/summarize.go index 0f978da..e688b53 100644 --- a/commands/summarize.go +++ b/commands/summarize.go @@ -10,6 +10,7 @@ import ( type summarizeFlags struct { buildpackTarballPath string + extensionTarballPath string format string } @@ -19,16 +20,21 @@ func summarize() *cobra.Command { Use: "summarize", Short: "summarize buildpackage", RunE: func(cmd *cobra.Command, args []string) error { - return summarizeRun(*flags) + isBuildpack, _ := cmd.Flags().GetString("buildpack") + if isBuildpack != "" { + return summarizeRun(*flags) + } else { + return summarizeExtensionRun(*flags) + } }, } cmd.Flags().StringVar(&flags.buildpackTarballPath, "buildpack", "", "path to a buildpackage tarball (required)") - cmd.Flags().StringVar(&flags.format, "format", "markdown", "format of output options are (markdown, json)") + cmd.Flags().StringVar(&flags.extensionTarballPath, "extension", "", "path to a buildpackage tarball (required)") + cmd.PersistentFlags().StringVar(&flags.format, "format", "markdown", "format of output options are (markdown, json)") + + cmd.MarkFlagsOneRequired("buildpack", "extension") + cmd.MarkFlagsMutuallyExclusive("buildpack", "extension") - err := cmd.MarkFlagRequired("buildpack") - if err != nil { - fmt.Fprintf(os.Stderr, "Unable to mark buildpack flag as required") - } return cmd } @@ -55,3 +61,24 @@ func summarizeRun(flags summarizeFlags) error { return nil } + +func summarizeExtensionRun(flags summarizeFlags) error { + + extensionInspector := internal.NewExtensionInspector() + formatter := internal.NewExtensionFormatter(os.Stdout) + configs, err := extensionInspector.Dependencies(flags.extensionTarballPath) + if err != nil { + return fmt.Errorf("failed to inspect extension dependencies: %w", err) + } + + switch flags.format { + case "markdown": + formatter.Markdown(configs) + case "json": + formatter.JSON(configs) + default: + return fmt.Errorf("unknown format %q, please choose from the following formats: markdown, json)", flags.format) + } + + return nil +}