github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/utils/dotnet/toolchaincmd.go (about) 1 package dotnet 2 3 import ( 4 "github.com/jfrog/jfrog-cli-core/utils/coreutils" 5 "github.com/jfrog/jfrog-client-go/utils/errorutils" 6 "io" 7 "os/exec" 8 ) 9 10 type ToolchainType int 11 12 const ( 13 Nuget ToolchainType = iota 14 DotnetCore 15 ) 16 17 type toolchainInfo struct { 18 name string 19 flagPrefix string 20 addSourceArgs []string 21 } 22 23 var toolchainsMap = map[ToolchainType]toolchainInfo{ 24 Nuget: { 25 name: "nuget", 26 flagPrefix: "-", 27 addSourceArgs: []string{"sources", "add"}, 28 }, 29 DotnetCore: { 30 name: "dotnet", 31 flagPrefix: "--", 32 addSourceArgs: []string{"nuget", "add", "source"}, 33 }, 34 } 35 36 func (toolchainType ToolchainType) String() string { 37 return toolchainsMap[toolchainType].name 38 } 39 40 func (toolchainType ToolchainType) GetTypeFlagPrefix() string { 41 return toolchainsMap[toolchainType].flagPrefix 42 } 43 44 func (toolchainType ToolchainType) GetAddSourceArgs() []string { 45 return toolchainsMap[toolchainType].addSourceArgs 46 } 47 48 func NewToolchainCmd(cmdType ToolchainType) (*Cmd, error) { 49 // On non Windows OS, NuGet may be run using mono. 50 if cmdType == Nuget && coreutils.IsLinux() { 51 return newNonWindowsNugetCmd() 52 } 53 execPath, err := exec.LookPath(cmdType.String()) 54 if err != nil { 55 return nil, errorutils.CheckError(err) 56 } 57 return &Cmd{toolchain: cmdType, execPath: execPath}, nil 58 } 59 60 // NuGet can be run on non Windows OS in one of the following ways: 61 // 1. using nuget client 62 // 2. using Mono 63 func newNonWindowsNugetCmd() (*Cmd, error) { 64 // First we will try lo look for 'nuget' in PATH. 65 nugetPath, err := exec.LookPath("nuget") 66 if err == nil { 67 return &Cmd{toolchain: Nuget, execPath: nugetPath}, nil 68 } 69 // If 'nuget' wasn't found, we Will try to run nuget using mono. 70 // Mono's first argument is nuget.exe's path, so we will look for both mono and nuget.exe in PATH. 71 monoPath, err := exec.LookPath("mono") 72 if err != nil { 73 return nil, errorutils.CheckError(err) 74 } 75 nugetExePath, err := exec.LookPath("nuget.exe") 76 if err != nil { 77 return nil, errorutils.CheckError(err) 78 } 79 return &Cmd{toolchain: Nuget, execPath: monoPath, Command: []string{nugetExePath}}, nil 80 } 81 82 func CreateDotnetAddSourceCmd(cmdType ToolchainType, sourceUrl string) (*Cmd, error) { 83 addSourceCmd, err := NewToolchainCmd(cmdType) 84 if err != nil { 85 return nil, errorutils.CheckError(err) 86 } 87 addSourceCmd.Command = append(addSourceCmd.Command, cmdType.GetAddSourceArgs()...) 88 switch cmdType { 89 case Nuget: 90 addSourceCmd.CommandFlags = append(addSourceCmd.CommandFlags, "-source", sourceUrl) 91 case DotnetCore: 92 addSourceCmd.Command = append(addSourceCmd.Command, sourceUrl) 93 // DotnetCore cli does not support password encryption on non-Windows OS, so we will write the raw password. 94 if !coreutils.IsWindows() { 95 addSourceCmd.CommandFlags = append(addSourceCmd.CommandFlags, "--store-password-in-clear-text") 96 } 97 } 98 return addSourceCmd, nil 99 } 100 101 func (config *Cmd) GetCmd() *exec.Cmd { 102 var cmd []string 103 cmd = append(cmd, config.execPath) 104 cmd = append(cmd, config.Command...) 105 cmd = append(cmd, config.CommandFlags...) 106 return exec.Command(cmd[0], cmd[1:]...) 107 } 108 109 func (config *Cmd) GetEnv() map[string]string { 110 return map[string]string{} 111 } 112 113 func (config *Cmd) GetStdWriter() io.WriteCloser { 114 return config.StrWriter 115 } 116 117 func (config *Cmd) GetErrWriter() io.WriteCloser { 118 return config.ErrWriter 119 } 120 121 func (config *Cmd) GetToolchain() ToolchainType { 122 return config.toolchain 123 } 124 125 type Cmd struct { 126 toolchain ToolchainType 127 execPath string 128 Command []string 129 CommandFlags []string 130 StrWriter io.WriteCloser 131 ErrWriter io.WriteCloser 132 }