@@ -15,6 +15,7 @@ import (
1515 "net/url"
1616 "os"
1717 "path"
18+ "path/filepath"
1819 "strings"
1920)
2021
@@ -168,7 +169,7 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
168169}
169170
170171// Runs nuget/dotnet source add command
171- func AddSourceToNugetConfig (cmdType dotnet.ToolchainType , sourceUrl , user , password string ) error {
172+ func AddSourceToNugetConfig (cmdType dotnet.ToolchainType , sourceUrl , user , password , customConfigPath string ) error {
172173 cmd , err := dotnet .CreateDotnetAddSourceCmd (cmdType , sourceUrl )
173174 if err != nil {
174175 return err
@@ -178,15 +179,21 @@ func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, passw
178179 cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "name" , SourceName )
179180 cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "username" , user )
180181 cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "password" , password )
181- stdOut , errorOut , _ , err := frogio .RunCmdWithOutputParser (cmd , false )
182+
183+ if customConfigPath != "" {
184+ addConfigFileFlag (cmd , customConfigPath )
185+ }
186+
187+ _ , _ , _ , err = frogio .RunCmdWithOutputParser (cmd , false )
182188 if err != nil {
183- return fmt .Errorf ("failed to add source: %w\n %s " , err , strings . TrimSpace ( stdOut + errorOut ) )
189+ return fmt .Errorf ("failed to add source: %w" , err )
184190 }
185191 return nil
186192}
187193
188- // Runs nuget/dotnet source remove command
189- func RemoveSourceFromNugetConfigIfExists (cmdType dotnet.ToolchainType ) error {
194+ // RemoveSourceFromNugetConfigIfExists runs the nuget/dotnet source remove command.
195+ // Removes the source if it exists in the configuration.
196+ func RemoveSourceFromNugetConfigIfExists (cmdType dotnet.ToolchainType , customConfigPath string ) error {
190197 cmd , err := dotnet .NewToolchainCmd (cmdType )
191198 if err != nil {
192199 return err
@@ -197,16 +204,55 @@ func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType) error {
197204 cmd .Command = append (cmd .Command , "sources" , "remove" )
198205 cmd .CommandFlags = append (cmd .CommandFlags , "-name" , SourceName )
199206 }
207+
208+ if customConfigPath != "" {
209+ addConfigFileFlag (cmd , customConfigPath )
210+ }
211+
200212 stdOut , stdErr , _ , err := frogio .RunCmdWithOutputParser (cmd , false )
201213 if err != nil {
202- if strings .Contains (stdOut + stdErr , "Unable to find" ) {
214+ if strings .Contains (stdOut + stdErr , "Unable to find" ) || strings . Contains ( stdOut + stdErr , "does not exist" ) {
203215 return nil
204216 }
205- return fmt . Errorf ("failed to remove source: %w \n % s" , err , strings . TrimSpace ( stdOut + stdErr ))
217+ return errorutils . CheckErrorf ("failed to remove source: %s" , err . Error ( ))
206218 }
207219 return nil
208220}
209221
222+ // GetConfigPathFromEnvIfProvided returns the path to the custom NuGet.Config file if it was provided by the user.
223+ func GetConfigPathFromEnvIfProvided (cmdType dotnet.ToolchainType ) string {
224+ if cmdType == dotnet .DotnetCore {
225+ if customDotnetDir := os .Getenv ("DOTNET_CLI_HOME" ); customDotnetDir != "" {
226+ return filepath .Join (customDotnetDir , "NuGet.Config" )
227+ }
228+ }
229+ return os .Getenv ("NUGET_CONFIG_FILE" )
230+ }
231+
232+ // CreateConfigFileIfNeeded creates a new config file if it does not exist.
233+ func CreateConfigFileIfNeeded (customConfigPath string ) error {
234+ // Ensure the file exists
235+ exists , err := fileutils .IsFileExists (customConfigPath , false )
236+ if err != nil || exists {
237+ return err
238+ }
239+ // If the file does not exist, create it
240+ if err = os .MkdirAll (filepath .Dir (customConfigPath ), 0755 ); err != nil {
241+ return err
242+ }
243+ // Write the default config content to the file
244+ return os .WriteFile (customConfigPath , []byte ("<configuration></configuration>" ), 0644 )
245+ }
246+
247+ func addConfigFileFlag (cmd * dotnet.Cmd , configFilePath string ) {
248+ // Add the config file flag if needed.
249+ if cmd .GetToolchain () == dotnet .DotnetCore {
250+ cmd .CommandFlags = append (cmd .CommandFlags , "--configfile" , configFilePath )
251+ } else {
252+ cmd .CommandFlags = append (cmd .CommandFlags , "-ConfigFile" , configFilePath )
253+ }
254+ }
255+
210256// Checks if the user provided input such as -configfile flag or -Source flag.
211257// If those flags were provided, NuGet will use the provided configs (default config file or the one with -configfile)
212258// If neither provided, we are initializing our own config.
0 commit comments