From 1589c206db261725503e60c5468cfdfa3d05cea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9lio=20Rocha?= Date: Tue, 5 Nov 2024 16:02:59 +0000 Subject: [PATCH] add registry to install command --- cmd/install.go | 16 ++++++++++------ config/eslint-utils.go | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cmd/install.go b/cmd/install.go index 407c02e..284f2c9 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -2,18 +2,23 @@ package cmd import ( cfg "codacy/cli-v2/config" - "github.com/spf13/cobra" + "fmt" "log" + + "github.com/spf13/cobra" ) +var registry string + func init() { + installCmd.Flags().StringVarP(®istry, "registry", "r", "", "Registry to use for installing tools") rootCmd.AddCommand(installCmd) } var installCmd = &cobra.Command{ - Use: "install", + Use: "install", Short: "Installs the tools specified in the project's config-file.", - Long: "Installs all runtimes and tools specified in the project's config-file file.", + Long: "Installs all runtimes and tools specified in the project's config-file file.", Run: func(cmd *cobra.Command, args []string) { // install runtimes fetchRuntimes(&cfg.Config) @@ -42,8 +47,9 @@ func fetchTools(config *cfg.ConfigType) { case "eslint": // eslint needs node runtime nodeRuntime := config.Runtimes()["node"] - err := cfg.InstallEslint(nodeRuntime, tool) + err := cfg.InstallEslint(nodeRuntime, tool, registry) if err != nil { + fmt.Println(err.Error()) log.Fatal(err) } default: @@ -51,5 +57,3 @@ func fetchTools(config *cfg.ConfigType) { } } } - - diff --git a/config/eslint-utils.go b/config/eslint-utils.go index 05ea4bf..239ed57 100644 --- a/config/eslint-utils.go +++ b/config/eslint-utils.go @@ -13,22 +13,36 @@ func genInfoEslint(r *Runtime) map[string]string { return map[string]string{ "installDir": installDir, - "eslint": path.Join(installDir, "node_modules", ".bin", "eslint"), + "eslint": path.Join(installDir, "node_modules", ".bin", "eslint"), } } /* * This installs eslint using node's npm alongside its sarif extension */ -func InstallEslint(nodeRuntime *Runtime, eslint *Runtime) error { +func InstallEslint(nodeRuntime *Runtime, eslint *Runtime, registry string) error { log.Println("Installing ESLint") eslintInstallArg := fmt.Sprintf("%s@%s", eslint.Name(), eslint.Version()) + if registry != "" { + fmt.Println("Using registry:", registry) + configCmd := exec.Command(nodeRuntime.Info()["npm"], "config", "set", "registry", registry) + if configOut, err := configCmd.Output(); err != nil { + fmt.Println("Error setting npm registry:", err) + fmt.Println(string(configOut)) + return err + } + } cmd := exec.Command(nodeRuntime.Info()["npm"], "install", "--prefix", eslint.Info()["installDir"], eslintInstallArg, "@microsoft/eslint-formatter-sarif") + fmt.Println(cmd.String()) // to use the chdir command we needed to create the folder before, we can change this after // cmd.Dir = eslintInstallationFolder stdout, err := cmd.Output() + if err != nil { + fmt.Println("Error installing ESLint:", err) + fmt.Println(string(stdout)) + } // Print the output fmt.Println(string(stdout)) return err