MCPcopy
hub / github.com/hashicorp/go-getter / smbclientGetFile

Method smbclientGetFile

get_smbclient.go:159–207  ·  view source on GitHub ↗
(req *Request)

Source from the content-addressed store, hash-verified

157}
158
159func (g *SmbClientGetter) smbclientGetFile(req *Request) error {
160 hostPath, filePath, err := g.findHostAndFilePath(req.u)
161 if err != nil {
162 return err
163 }
164
165 // Get file and subdirectory name when existent
166 file := ""
167 if strings.Contains(filePath, "/") {
168 i := strings.LastIndex(filePath, "/")
169 file = filePath[i+1:]
170 filePath = filePath[:i]
171 } else {
172 file = filePath
173 filePath = "."
174 }
175
176 cmdArgs := g.smbclientCmdArgs(req.u.User, hostPath, filePath)
177 // check file exists in the smb shared folder and is not a directory
178 isDir, err := g.isDirectory(cmdArgs, file)
179 if err != nil {
180 return err
181 }
182 if isDir {
183 return fmt.Errorf("%s source path must be a file", file)
184 }
185
186 // download file
187 cmdArgs = append(cmdArgs, "-c")
188 if req.Dst != "" {
189 _, err := os.Lstat(req.Dst)
190 if err != nil {
191 if os.IsNotExist(err) {
192 // Create destination folder if it doesn't exist
193 if err := os.MkdirAll(filepath.Dir(req.Dst), 0755); err != nil {
194 return fmt.Errorf("failed to creat destination path: %s", err.Error())
195 }
196 } else {
197 return err
198 }
199 }
200 cmdArgs = append(cmdArgs, fmt.Sprintf("get %s %s", file, req.Dst))
201 } else {
202 cmdArgs = append(cmdArgs, fmt.Sprintf("get %s", file))
203 }
204
205 _, err = g.runSmbClientCommand("", cmdArgs)
206 return err
207}
208
209func (g *SmbClientGetter) smbclientCmdArgs(used *url.Userinfo, hostPath string, fileDir string) (baseCmd []string) {
210 baseCmd = append(baseCmd, "-N")

Callers 1

GetFileMethod · 0.95

Calls 6

findHostAndFilePathMethod · 0.95
smbclientCmdArgsMethod · 0.95
isDirectoryMethod · 0.95
runSmbClientCommandMethod · 0.95
DirMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected