From f80cdb9d87dacc4086a251823935d1af0f921c17 Mon Sep 17 00:00:00 2001 From: Keshav Priyadarshi Date: Thu, 13 Jun 2024 20:00:10 +0530 Subject: [PATCH 1/4] Add option to omit debug symbols from binary Signed-off-by: Keshav Priyadarshi --- Makefile | 15 +++++++++++++-- scripts/build-all.sh | 10 +++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 63d1593..f5cbd14 100644 --- a/Makefile +++ b/Makefile @@ -20,14 +20,25 @@ BUILD_DIR=./build GOOS := $(shell $(GOCMD) env GOOS) GOARCH := $(shell $(GOCMD) env GOARCH) BINARY_OUTPUT=$(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH) +BUILD_FLAGS= +STRIP_FLAG= + +strip ?= no + +# Omit symbol table, debug info and DWARF symbol table +# https://github.com/golang/go/blob/ca5ba146da7a9d4e2a8cbe1715a78be42b45a745/src/cmd/link/doc.go#L115-L123 +ifeq ($(strip), yes) + BUILD_FLAGS=-s -w + STRIP_FLAG=--strip +endif build: @echo "Building binary to $(BINARY_OUTPUT)" - $(GOCMD) build -o $(BINARY_OUTPUT) -v + $(GOCMD) build -ldflags "$(BUILD_FLAGS)" -o $(BINARY_OUTPUT) build-all: - ./scripts/build-all.sh + ./scripts/build-all.sh $(STRIP_FLAG) clean: $(GOCMD) clean diff --git a/scripts/build-all.sh b/scripts/build-all.sh index a863b88..e0851b5 100755 --- a/scripts/build-all.sh +++ b/scripts/build-all.sh @@ -17,6 +17,14 @@ platforms=( "windows/amd64" ) +build_flags= + +# Omit symbol table, debug info and DWARF symbol table +# https://github.com/golang/go/blob/ca5ba146da7a9d4e2a8cbe1715a78be42b45a745/src/cmd/link/doc.go#L115-L123 +if [ "$1" == "--strip" ]; then + build_flags="-s -w" +fi + for platform in "${platforms[@]}" do platform_split=(${platform//\// }) @@ -28,7 +36,7 @@ do output_name+='.exe' fi - env GOOS=$GOOS GOARCH=$GOARCH go build -o ./build/$output_name + env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "$build_flags" -o ./build/$output_name if [ $? -ne 0 ]; then echo "An error occurred during the build process!" From 806b91be83d3cb98695ef9b528fb9eee5691984e Mon Sep 17 00:00:00 2001 From: Keshav Priyadarshi Date: Tue, 18 Jun 2024 18:49:28 +0530 Subject: [PATCH 2/4] Support saving command output to file - Save the command output to a file when an `outputFileName` is provided. Signed-off-by: Keshav Priyadarshi --- cmd/npm.go | 1 + cmd/pnpm.go | 1 + cmd/yarn.go | 1 + internal/utils.go | 21 +++++++++++++++++---- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/npm.go b/cmd/npm.go index f91fdd4..e741b84 100644 --- a/cmd/npm.go +++ b/cmd/npm.go @@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`, lockFiles, args, lockGenCommand, + "", forced, ) }, diff --git a/cmd/pnpm.go b/cmd/pnpm.go index a9313eb..17bf031 100644 --- a/cmd/pnpm.go +++ b/cmd/pnpm.go @@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`, lockFiles, args, lockGenCommand, + "", forced, ) }, diff --git a/cmd/yarn.go b/cmd/yarn.go index fa4feaf..be831eb 100644 --- a/cmd/yarn.go +++ b/cmd/yarn.go @@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`, lockFiles, args, lockGenCommand, + "", forced, ) }, diff --git a/internal/utils.go b/internal/utils.go index ce87f5d..f8a8e11 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -18,7 +18,7 @@ import ( "path/filepath" ) -func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, forced bool) { +func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, outputFileName string, forced bool) { path := "." if len(cmdArgs) > 0 { @@ -42,7 +42,7 @@ func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, f } } - genLock(lockGenCmd, absPath) + genLock(lockGenCmd, absPath, outputFileName) } @@ -67,14 +67,27 @@ func DoesFileExists(absPath string) bool { return false } -func genLock(lockGenCmd []string, absPath string) { +func genLock(lockGenCmd []string, absPath string, outputFileName string) { fmt.Printf("Generating lockfile using '%s'\n", lockGenCmd) // #nosec G204 command := exec.Command(lockGenCmd[0], lockGenCmd[1:]...) command.Dir = absPath - command.Stdout = os.Stdout command.Stderr = os.Stderr + command.Stdout = os.Stdout + if outputFileName != "" { + + outputPath := filepath.Join(absPath, outputFileName) + + outputFile, err := os.Create(outputPath) + if err != nil { + fmt.Fprintln(os.Stderr, "Error: failed to create output file: %v", err) + os.Exit(1) + } + defer outputFile.Close() + + command.Stdout = outputFile + } if err := command.Run(); err != nil { fmt.Fprintln(os.Stderr, "Error: Failed to generate lockfile: ", err) From 75af7c63ab6ba184a0b530d3080f282dc6a1931a Mon Sep 17 00:00:00 2001 From: Keshav Priyadarshi Date: Tue, 18 Jun 2024 18:52:01 +0530 Subject: [PATCH 3/4] Support generation of lockfile and manifest JSON dump for Swift Signed-off-by: Keshav Priyadarshi --- cmd/root.go | 1 + cmd/swift.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 cmd/swift.go diff --git a/cmd/root.go b/cmd/root.go index 6bc4f6f..9eb3039 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,6 +22,7 @@ var ecosystems = []func() *cobra.Command{ pnpmCmd, npmCmd, yarnCmd, + swiftCmd, } func NewRootCmd() *cobra.Command { diff --git a/cmd/swift.go b/cmd/swift.go new file mode 100644 index 0000000..91e5af0 --- /dev/null +++ b/cmd/swift.go @@ -0,0 +1,60 @@ +/* + +Copyright (c) nexB Inc. and others. All rights reserved. +ScanCode is a trademark of nexB Inc. +SPDX-License-Identifier: Apache-2.0 +See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +See https://github.com/nexB/dependency-inspector for support or download. +See https://aboutcode.org for more information about nexB OSS projects. + +*/ + +package cmd + +import ( + "github.com/nexB/dependency-inspector/internal" + "github.com/spf13/cobra" +) + +func swiftCmd() *cobra.Command { + lockFiles := [][]string{ + {"Package.resolved", ".package.resolved"}, + {"Package.swift.json"}, + } + lockGenCommands := [][]string{ + {"swift", "package", "resolve"}, + {"swift", "package", "dump-package"}, + } + commandOutput := []string{ + "", + "Package.swift.json", + } + forced := false + + swiftCmd := &cobra.Command{ + Use: "swift [path]", + Short: "Generate lockfile for swift project", + Long: `Create lockfile and JSON dump of manifest for swift project +if they doesn't already exist in the specified [path]. +If no path is provided, the command defaults to the current directory.`, + Args: cobra.MaximumNArgs(1), + + Run: func(cmd *cobra.Command, args []string) { + + for i := range lockFiles { + internal.CreateLockFile( + lockFiles[i], + args, + lockGenCommands[i], + commandOutput[i], + forced, + ) + } + + }, + } + + swiftCmd.Flags().BoolVarP(&forced, "force", "f", false, "Generate lockfile forcibly, ignoring existing lockfiles") + + return swiftCmd +} From 2fabe3e2476d8571c0567b369d8ba2c2c25fa751 Mon Sep 17 00:00:00 2001 From: Keshav Priyadarshi Date: Wed, 19 Jun 2024 15:09:34 +0530 Subject: [PATCH 4/4] Fix linting issues Signed-off-by: Keshav Priyadarshi --- internal/utils.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/utils.go b/internal/utils.go index f8a8e11..a657236 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -79,9 +79,10 @@ func genLock(lockGenCmd []string, absPath string, outputFileName string) { outputPath := filepath.Join(absPath, outputFileName) + // #nosec G304 outputFile, err := os.Create(outputPath) if err != nil { - fmt.Fprintln(os.Stderr, "Error: failed to create output file: %v", err) + fmt.Fprintln(os.Stderr, "Error: failed to create output file: ", err) os.Exit(1) } defer outputFile.Close()