diff --git a/pkg/cmd/package/nuget/create/create.go b/pkg/cmd/package/nuget/create/create.go index 599b56da..dd466cb3 100644 --- a/pkg/cmd/package/nuget/create/create.go +++ b/pkg/cmd/package/nuget/create/create.go @@ -66,7 +66,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command { NuPkgCreateFlags: createFlags, PackageCreateOptions: pack.NewPackageCreateOptions(f, packFlags, cmd), } - return createRun(opts) + return createRun(cmd, opts) }, } @@ -88,7 +88,11 @@ func NewCmdCreate(f factory.Factory) *cobra.Command { return cmd } -func createRun(opts *NuPkgCreateOptions) error { +func createRun(cmd *cobra.Command, opts *NuPkgCreateOptions) error { + outputFormat, err := cmd.Flags().GetString(constants.FlagOutputFormat) + if err != nil { // should never happen, but fallback if it does + outputFormat = constants.OutputFormatTable + } if !opts.NoPrompt { if err := pack.PackageCreatePromptMissing(opts.PackageCreateOptions); err != nil { return err @@ -102,7 +106,7 @@ func createRun(opts *NuPkgCreateOptions) error { return errors.New("must supply a package ID") } - err := applyDefaultsToUnspecifiedPackageOptions(opts) + err = applyDefaultsToUnspecifiedPackageOptions(opts) if err != nil { return err } @@ -127,11 +131,6 @@ func createRun(opts *NuPkgCreateOptions) error { pack.VerboseOut(opts.Writer, opts.Verbose.Value, "Packing \"%s\" version \"%s\"...\n", opts.Id.Value, opts.Version.Value) outFilePath := pack.BuildOutFileName("nupkg", opts.Id.Value, opts.Version.Value) - err = pack.BuildPackage(opts.PackageCreateOptions, outFilePath) - if err != nil { - return err - } - if !opts.NoPrompt { autoCmd := flag.GenerateAutomationCmd( opts.CmdPath, @@ -151,7 +150,18 @@ func createRun(opts *NuPkgCreateOptions) error { fmt.Fprintf(opts.Writer, "\nAutomation Command: %s\n", autoCmd) } - return nil + nuget, err := pack.BuildPackage(opts.PackageCreateOptions, outFilePath) + if (nuget != nil) { + switch outputFormat { + case constants.OutputFormatBasic: + cmd.Printf("%s\n", nuget.Name()) + case constants.OutputFormatJson: + cmd.Printf(`{"Path": "%s"}`, nuget.Name()) + default: // table + cmd.Printf("Successfully created package %s\n", nuget.Name()) + } + } + return err } func PromptMissing(opts *NuPkgCreateOptions) error { diff --git a/pkg/cmd/package/support/pack.go b/pkg/cmd/package/support/pack.go index cd323b38..588d2402 100644 --- a/pkg/cmd/package/support/pack.go +++ b/pkg/cmd/package/support/pack.go @@ -165,37 +165,37 @@ func BuildOutFileName(packageType, id, version string) string { return fmt.Sprintf("%s.%s.%s", id, version, packageType) } -func BuildPackage(opts *PackageCreateOptions, outFileName string) error { +func BuildPackage(opts *PackageCreateOptions, outFileName string) (*os.File, error) { outFilePath := filepath.Join(opts.OutFolder.Value, outFileName) outPath, err := filepath.Abs(opts.OutFolder.Value) if err != nil { - return err + return nil, err } _, err = os.Stat(outFilePath) if !opts.Overwrite.Value && err == nil { - return fmt.Errorf("package with name '%s' already exists ...aborting", outFileName) + return nil, fmt.Errorf("package with name '%s' already exists ...aborting", outFileName) } VerboseOut(opts.Writer, opts.Verbose.Value, "Saving \"%s\" to \"%s\"...\nAdding files from \"%s\" matching pattern/s \"%s\"\n", outPath, outFileName, outPath, strings.Join(opts.Include.Value, ", ")) filePaths, err := getDistinctPatternMatches(opts.BasePath.Value, opts.Include.Value) if err != nil { - return err + return nil, err } if len(filePaths) == 0 { - return errors.New("no files identified to package") + return nil, errors.New("no files identified to package") } return buildArchive(opts.Writer, outFilePath, opts.BasePath.Value, filePaths, opts.Verbose.Value) } -func buildArchive(out io.Writer, outFilePath string, basePath string, filesToArchive []string, isVerbose bool) error { +func buildArchive(out io.Writer, outFilePath string, basePath string, filesToArchive []string, isVerbose bool) (*os.File, error) { _, outFile := filepath.Split(outFilePath) zipFile, err := os.Create(outFilePath) if err != nil { - return err + return nil, err } defer zipFile.Close() @@ -210,12 +210,12 @@ func buildArchive(out io.Writer, outFilePath string, basePath string, filesToArc fullPath := filepath.Join(basePath, path) fileInfo, err := os.Stat(fullPath) if err != nil { - return err + return nil, err } header, err := zip.FileInfoHeader(fileInfo) if err != nil { - return err + return nil, err } header.Method = zip.Deflate @@ -226,7 +226,7 @@ func buildArchive(out io.Writer, outFilePath string, basePath string, filesToArc headerWriter, err := writer.CreateHeader(header) if err != nil { - return err + return nil, err } if fileInfo.IsDir() { @@ -235,23 +235,23 @@ func buildArchive(out io.Writer, outFilePath string, basePath string, filesToArc f, err := os.Open(fullPath) if err != nil { - return err + return nil, err } _, err = io.Copy(headerWriter, f) if err == nil { VerboseOut(out, isVerbose, "Added file: %s\n", path) } else { - return err + return nil, err } err = f.Close() if err != nil { - return err + return nil, err } } - return nil + return zipFile, nil } func getDistinctPatternMatches(basePath string, patterns []string) ([]string, error) { diff --git a/pkg/cmd/package/zip/create/create.go b/pkg/cmd/package/zip/create/create.go index afd8bf85..21c7be04 100644 --- a/pkg/cmd/package/zip/create/create.go +++ b/pkg/cmd/package/zip/create/create.go @@ -25,7 +25,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command { `, constants.ExecutableName), RunE: func(cmd *cobra.Command, args []string) error { opts := pack.NewPackageCreateOptions(f, createFlags, cmd) - return createRun(opts) + return createRun(cmd, opts) }, } @@ -42,7 +42,12 @@ func NewCmdCreate(f factory.Factory) *cobra.Command { return cmd } -func createRun(opts *pack.PackageCreateOptions) error { +func createRun(cmd *cobra.Command, opts *pack.PackageCreateOptions) error { + outputFormat, err := cmd.Flags().GetString(constants.FlagOutputFormat) + if err != nil { // should never happen, but fallback if it does + outputFormat = constants.OutputFormatTable + } + if !opts.NoPrompt { if err := pack.PackageCreatePromptMissing(opts); err != nil { return err @@ -71,7 +76,19 @@ func createRun(opts *pack.PackageCreateOptions) error { } outFilePath := pack.BuildOutFileName("zip", opts.Id.Value, opts.Version.Value) - return pack.BuildPackage(opts, outFilePath) + + zip, err := pack.BuildPackage(opts, outFilePath) + if (zip != nil) { + switch outputFormat { + case constants.OutputFormatBasic: + cmd.Printf("%s\n", zip.Name()) + case constants.OutputFormatJson: + cmd.Printf(`{"Path": "%s"}`, zip.Name()) + default: // table + cmd.Printf("Successfully created package %s\n", zip.Name()) + } + } + return err } func applyDefaultsToUnspecifiedOptions(opts *pack.PackageCreateOptions) {