@@ -15,6 +15,7 @@ import (
15
15
"net/url"
16
16
"os"
17
17
"path"
18
+ "path/filepath"
18
19
"strings"
19
20
)
20
21
@@ -168,7 +169,7 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
168
169
}
169
170
170
171
// 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 {
172
173
cmd , err := dotnet .CreateDotnetAddSourceCmd (cmdType , sourceUrl )
173
174
if err != nil {
174
175
return err
@@ -178,15 +179,21 @@ func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, passw
178
179
cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "name" , SourceName )
179
180
cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "username" , user )
180
181
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 )
182
188
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 )
184
190
}
185
191
return nil
186
192
}
187
193
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 {
190
197
cmd , err := dotnet .NewToolchainCmd (cmdType )
191
198
if err != nil {
192
199
return err
@@ -197,16 +204,55 @@ func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType) error {
197
204
cmd .Command = append (cmd .Command , "sources" , "remove" )
198
205
cmd .CommandFlags = append (cmd .CommandFlags , "-name" , SourceName )
199
206
}
207
+
208
+ if customConfigPath != "" {
209
+ addConfigFileFlag (cmd , customConfigPath )
210
+ }
211
+
200
212
stdOut , stdErr , _ , err := frogio .RunCmdWithOutputParser (cmd , false )
201
213
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" ) {
203
215
return nil
204
216
}
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 ( ))
206
218
}
207
219
return nil
208
220
}
209
221
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
+
210
256
// Checks if the user provided input such as -configfile flag or -Source flag.
211
257
// If those flags were provided, NuGet will use the provided configs (default config file or the one with -configfile)
212
258
// If neither provided, we are initializing our own config.
0 commit comments