55 "fmt"
66 "github.com/jfrog/build-info-go/build"
77 "github.com/jfrog/build-info-go/build/utils/dotnet"
8- "github.com/jfrog/gofrog/io"
8+ frogio "github.com/jfrog/gofrog/io"
99 commonBuild "github.com/jfrog/jfrog-cli-core/v2/common/build"
1010 "github.com/jfrog/jfrog-cli-core/v2/utils/config"
1111 "github.com/jfrog/jfrog-client-go/auth"
@@ -19,6 +19,7 @@ import (
1919)
2020
2121const (
22+ // SourceName should match the one in the config file template.
2223 SourceName = "JFrogCli"
2324 configFilePattern = "jfrog.cli.nuget."
2425
@@ -30,14 +31,16 @@ The initial error is:
3031)
3132
3233type DotnetCommand struct {
33- toolchainType dotnet.ToolchainType
34- subCommand string
35- argAndFlags []string
36- repoName string
37- solutionPath string
38- useNugetV2 bool
39- buildConfiguration * commonBuild.BuildConfiguration
40- serverDetails * config.ServerDetails
34+ toolchainType dotnet.ToolchainType
35+ subCommand string
36+ argAndFlags []string
37+ repoName string
38+ solutionPath string
39+ useNugetV2 bool
40+ // By default, package sources are required to use HTTPS. This option allows sources to use HTTP.
41+ allowInsecureConnections bool
42+ buildConfiguration * commonBuild.BuildConfiguration
43+ serverDetails * config.ServerDetails
4144}
4245
4346func (dc * DotnetCommand ) SetServerDetails (serverDetails * config.ServerDetails ) * DotnetCommand {
@@ -70,6 +73,11 @@ func (dc *DotnetCommand) SetUseNugetV2(useNugetV2 bool) *DotnetCommand {
7073 return dc
7174}
7275
76+ func (dc * DotnetCommand ) SetAllowInsecureConnections (allowInsecureConnections bool ) * DotnetCommand {
77+ dc .allowInsecureConnections = allowInsecureConnections
78+ return dc
79+ }
80+
7381func (dc * DotnetCommand ) SetArgAndFlags (argAndFlags []string ) * DotnetCommand {
7482 dc .argAndFlags = argAndFlags
7583 return dc
@@ -159,21 +167,44 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
159167 return newWorkingDir , errorutils .CheckError (err )
160168}
161169
162- // Runs nuget sources add command
163- func AddSourceToNugetConfig (cmdType dotnet.ToolchainType , configFileName , sourceUrl , user , password string ) error {
170+ // Runs nuget/dotnet source add command
171+ func AddSourceToNugetConfig (cmdType dotnet.ToolchainType , sourceUrl , user , password string ) error {
164172 cmd , err := dotnet .CreateDotnetAddSourceCmd (cmdType , sourceUrl )
165173 if err != nil {
166174 return err
167175 }
168176
169177 flagPrefix := cmdType .GetTypeFlagPrefix ()
170- cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "configfile" , configFileName )
171178 cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "name" , SourceName )
172179 cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "username" , user )
173180 cmd .CommandFlags = append (cmd .CommandFlags , flagPrefix + "password" , password )
174- output , err := io .RunCmdOutput (cmd )
175- log .Debug ("'Add sources' command executed. Output:" , output )
176- return err
181+ stdOut , errorOut , _ , err := frogio .RunCmdWithOutputParser (cmd , false )
182+ if err != nil {
183+ return fmt .Errorf ("failed to add source: %w\n %s" , err , strings .TrimSpace (stdOut + errorOut ))
184+ }
185+ return nil
186+ }
187+
188+ // Runs nuget/dotnet source remove command
189+ func RemoveSourceFromNugetConfigIfExists (cmdType dotnet.ToolchainType ) error {
190+ cmd , err := dotnet .NewToolchainCmd (cmdType )
191+ if err != nil {
192+ return err
193+ }
194+ if cmdType == dotnet .DotnetCore {
195+ cmd .Command = append (cmd .Command , "nuget" , "remove" , "source" , SourceName )
196+ } else {
197+ cmd .Command = append (cmd .Command , "sources" , "remove" )
198+ cmd .CommandFlags = append (cmd .CommandFlags , "-name" , SourceName )
199+ }
200+ stdOut , stdErr , _ , err := frogio .RunCmdWithOutputParser (cmd , false )
201+ if err != nil {
202+ if strings .Contains (stdOut + stdErr , "Unable to find" ) {
203+ return nil
204+ }
205+ return fmt .Errorf ("failed to remove source: %w\n %s" , err , strings .TrimSpace (stdOut + stdErr ))
206+ }
207+ return nil
177208}
178209
179210// Checks if the user provided input such as -configfile flag or -Source flag.
@@ -219,7 +250,7 @@ func (dc *DotnetCommand) prepareConfigFileIfNeeded() (cleanup func() error, err
219250 return fileutils .RemoveTempDir (tempDirPath )
220251 }
221252
222- configFile , err := InitNewConfig (tempDirPath , dc .repoName , dc .serverDetails , dc .useNugetV2 )
253+ configFile , err := InitNewConfig (tempDirPath , dc .repoName , dc .serverDetails , dc .useNugetV2 , dc . allowInsecureConnections )
223254 if err == nil {
224255 dc .argAndFlags = append (dc .argAndFlags , dc .GetToolchain ().GetTypeFlagPrefix ()+ "configfile" , configFile .Name ())
225256 }
@@ -246,7 +277,7 @@ func getFlagValueIfExists(cmdFlag string, argAndFlags []string) (string, error)
246277}
247278
248279// InitNewConfig is used when neither of the flags were provided, and we need to init our own config.
249- func InitNewConfig (configDirPath , repoName string , server * config.ServerDetails , useNugetV2 bool ) (configFile * os.File , err error ) {
280+ func InitNewConfig (configDirPath , repoName string , server * config.ServerDetails , useNugetV2 , allowInsecureConnections bool ) (configFile * os.File , err error ) {
250281 // Initializing a new NuGet config file that NuGet will use into a temp file
251282 configFile , err = os .CreateTemp (configDirPath , configFilePattern )
252283 if errorutils .CheckError (err ) != nil {
@@ -260,13 +291,13 @@ func InitNewConfig(configDirPath, repoName string, server *config.ServerDetails,
260291 // We would prefer to write the NuGet configuration using the `nuget add source` command,
261292 // but the NuGet configuration utility doesn't currently allow setting protocolVersion.
262293 // Until that is supported, the templated method must be used.
263- err = addSourceToNugetTemplate (configFile , server , useNugetV2 , repoName )
294+ err = addSourceToNugetTemplate (configFile , server , useNugetV2 , repoName , allowInsecureConnections )
264295 return
265296}
266297
267298// Adds a source to the nuget config template
268- func addSourceToNugetTemplate (configFile * os.File , server * config.ServerDetails , useNugetV2 bool , repoName string ) error {
269- sourceUrl , user , password , err := getSourceDetails (server , repoName , useNugetV2 )
299+ func addSourceToNugetTemplate (configFile * os.File , server * config.ServerDetails , useNugetV2 bool , repoName string , allowInsecureConnections bool ) error {
300+ sourceUrl , user , password , err := GetSourceDetails (server , repoName , useNugetV2 )
270301 if err != nil {
271302 return err
272303 }
@@ -278,11 +309,11 @@ func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails,
278309 }
279310
280311 // Format the templates
281- _ , err = fmt .Fprintf (configFile , dotnet .ConfigFileFormat , sourceUrl , protoVer , user , password )
312+ _ , err = fmt .Fprintf (configFile , dotnet .ConfigFileFormat , sourceUrl , protoVer , allowInsecureConnections , user , password )
282313 return err
283314}
284315
285- func getSourceDetails (details * config.ServerDetails , repoName string , useNugetV2 bool ) (sourceURL , user , password string , err error ) {
316+ func GetSourceDetails (details * config.ServerDetails , repoName string , useNugetV2 bool ) (sourceURL , user , password string , err error ) {
286317 var u * url.URL
287318 u , err = url .Parse (details .ArtifactoryUrl )
288319 if errorutils .CheckError (err ) != nil {
0 commit comments