sync: Implement writable importer
This commit is contained in:
parent
8ed9415c68
commit
aab66bf612
@ -53,6 +53,12 @@ type ForgeLinkedImporter interface {
|
||||
GetExerciceLink(e *fic.Exercice) (*url.URL, error)
|
||||
}
|
||||
|
||||
// WritableImporter abstracts importer that we can also write on
|
||||
type WritableImporter interface {
|
||||
// writeFile write the given buffer to the file at the given location.
|
||||
writeFile(filename string, reader io.Reader) error
|
||||
}
|
||||
|
||||
// GlobalImporter stores the main importer instance to use for global imports.
|
||||
var GlobalImporter Importer
|
||||
|
||||
@ -189,3 +195,12 @@ func ImportFile(i Importer, URI string, next func(string, string) (interface{},
|
||||
|
||||
return next(dest, URI)
|
||||
}
|
||||
|
||||
// WriteFileContent save the given content to the given text file.
|
||||
func WriteFileContent(i Importer, URI string, content []byte) error {
|
||||
if wi, ok := i.(WritableImporter); ok {
|
||||
return wi.writeFile(URI, bytes.NewReader(content))
|
||||
} else {
|
||||
return fmt.Errorf("%t is not capable of writing", i)
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package sync
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -103,6 +104,29 @@ func (i CloudImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i CloudImporter) writeFile(filename string, reader io.Reader) error {
|
||||
fullURL := i.baseDAV
|
||||
fullURL.Path = path.Join(fullURL.Path, filename)
|
||||
|
||||
client := http.Client{}
|
||||
if req, err := http.NewRequest("PUT", fullURL.String(), reader); err != nil {
|
||||
return err
|
||||
} else {
|
||||
req.SetBasicAuth(i.username, i.password)
|
||||
if resp, err := client.Do(req); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New(resp.Status)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (i CloudImporter) listDir(filename string) ([]string, error) {
|
||||
client := gowebdav.NewClient(i.baseDAV.String(), i.username, i.password)
|
||||
|
||||
|
@ -2,6 +2,7 @@ package sync
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
@ -38,6 +39,10 @@ func (i GitImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
return i.li.getFile(filename, writer)
|
||||
}
|
||||
|
||||
func (i GitImporter) writeFile(filename string, reader io.Reader) error {
|
||||
return i.li.writeFile(filename, reader)
|
||||
}
|
||||
|
||||
func (i GitImporter) listDir(filename string) ([]string, error) {
|
||||
return i.li.listDir(filename)
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package sync
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
@ -94,6 +95,16 @@ func (i LocalImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (i LocalImporter) writeFile(filename string, reader io.Reader) error {
|
||||
if fd, err := os.Create(path.Join(i.Base, filename)); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
io.Copy(fd, reader)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (i LocalImporter) listDir(filename string) ([]string, error) {
|
||||
if files, err := ioutil.ReadDir(path.Join(i.Base, filename)); err != nil {
|
||||
return nil, err
|
||||
|
Loading…
x
Reference in New Issue
Block a user