From b00bcb818e9f7fe3e91b0a5d5a45c1a8129d5af9 Mon Sep 17 00:00:00 2001 From: Riddhimaan-Senapati <114703025+Riddhimaan-Senapati@users.noreply.github.com> Date: Fri, 31 Jan 2025 11:32:42 -0500 Subject: [PATCH] fixed issue #1555 1.Added a global variable servePort with a default value of "8000" 2.Added a new CLI flag --port that allows users to customize the port number used when serving the HTML report 3. Removed the hardcoded port number from the ServeHTML function, using the global servePort variable instead 4. The flag's Action function updates the global servePort variable when a custom port is specified These changes allow users to specify a custom port using the --port flag when using the --serve option, while maintaining the default port 8000 if no custom port is specified. --- cmd/osv-scanner/internal/helper/helper.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/osv-scanner/internal/helper/helper.go b/cmd/osv-scanner/internal/helper/helper.go index 5e7d0edac6..2bf9cb88ae 100644 --- a/cmd/osv-scanner/internal/helper/helper.go +++ b/cmd/osv-scanner/internal/helper/helper.go @@ -22,6 +22,11 @@ var OfflineFlags = map[string]string{ // "experimental-licenses": "", // StringSliceFlag has to be manually cleared. } +// sets default port(8000) as a global variable +var ( + servePort = "8000" // default port +) + var GlobalScanFlags = []cli.Flag{ &cli.StringFlag{ Name: "config", @@ -45,6 +50,14 @@ var GlobalScanFlags = []cli.Flag{ Name: "serve", Usage: "output as HTML result and serve it locally", }, + &cli.StringFlag{ + Name: "port", + Usage: "port number to use when serving HTML report (default: 8000)", + Action: func(_ *cli.Context, p string) error { + servePort = p + return nil + }, + }, &cli.StringFlag{ Name: "output", Usage: "saves the result to the given file path", @@ -129,7 +142,6 @@ func OpenHTML(r reporter.Reporter, outputPath string) { // The program will keep running to serve the HTML report on localhost // until the user manually terminates it (e.g. using Ctrl+C). func ServeHTML(r reporter.Reporter, outputPath string) { - servePort := "8000" localhostURL := fmt.Sprintf("http://localhost:%s/", servePort) r.Infof("Serving HTML report at %s.\nIf you are accessing remotely, use the following SSH command:\n`ssh -L local_port:destination_server_ip:%s ssh_server_hostname`\n", localhostURL, servePort) server := &http.Server{