itd/api/fs.go

103 lines
2.0 KiB
Go
Raw Normal View History

2021-11-23 19:12:16 +00:00
package api
import (
"github.com/mitchellh/mapstructure"
"go.arsenm.dev/itd/internal/types"
)
func (c *Client) Rename(old, new string) error {
_, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeMove,
Files: []string{old, new},
},
})
if err != nil {
return err
}
return nil
}
func (c *Client) Remove(paths ...string) error {
2021-11-23 19:12:16 +00:00
_, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeDelete,
Files: paths,
2021-11-23 19:12:16 +00:00
},
})
if err != nil {
return err
}
return nil
}
func (c *Client) Mkdir(paths ...string) error {
2021-11-23 19:12:16 +00:00
_, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeMkdir,
Files: paths,
2021-11-23 19:12:16 +00:00
},
})
if err != nil {
return err
}
return nil
}
func (c *Client) ReadDir(path string) ([]types.FileInfo, error) {
res, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeList,
Files: []string{path},
},
})
if err != nil {
return nil, err
}
var out []types.FileInfo
err = mapstructure.Decode(res.Value, &out)
if err != nil {
return nil, err
}
return out, nil
}
2021-12-13 01:08:48 +00:00
func (c *Client) ReadFile(localPath, remotePath string) (<-chan types.FSTransferProgress, error) {
c.readProgressCh = make(chan types.FSTransferProgress, 5)
_, err := c.request(types.Request{
2021-11-23 19:12:16 +00:00
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeRead,
2021-12-12 06:11:01 +00:00
Files: []string{localPath, remotePath},
2021-11-23 19:12:16 +00:00
},
})
2021-12-13 01:08:48 +00:00
2021-11-23 19:12:16 +00:00
if err != nil {
2021-12-13 01:08:48 +00:00
return nil, err
2021-11-23 19:12:16 +00:00
}
2021-12-13 01:08:48 +00:00
return c.readProgressCh, nil
2021-11-23 19:12:16 +00:00
}
2021-12-13 01:08:48 +00:00
func (c *Client) WriteFile(localPath, remotePath string) (<-chan types.FSTransferProgress, error) {
c.writeProgressCh = make(chan types.FSTransferProgress, 5)
_, err := c.request(types.Request{
2021-11-23 19:12:16 +00:00
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
2021-11-23 22:14:45 +00:00
Type: types.FSTypeWrite,
2021-12-12 06:11:01 +00:00
Files: []string{remotePath, localPath},
2021-11-23 19:12:16 +00:00
},
})
if err != nil {
2021-12-13 01:08:48 +00:00
return nil, err
2021-11-23 19:12:16 +00:00
}
2021-12-13 01:08:48 +00:00
return c.writeProgressCh, nil
2021-11-23 19:12:16 +00:00
}