Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print the package that was created #402

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions pkg/cmd/package/nuget/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions pkg/cmd/package/support/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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) {
Expand Down
23 changes: 20 additions & 3 deletions pkg/cmd/package/zip/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Loading