refactor(grafanabackuper): add restore function
This commit is contained in:
parent
83a15b5a87
commit
1f0d76ba9e
9 changed files with 214 additions and 8 deletions
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/memfs"
|
||||
|
@ -214,3 +216,36 @@ func (p *Project) Push(ctx context.Context) error {
|
|||
|
||||
return p.repository.PushContext(ctx, &pushOpts)
|
||||
}
|
||||
|
||||
func (p *Project) ListJSONFiles(directory string) ([]string, error) {
|
||||
files, err := p.fs.ReadDir(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var allFiles []string
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
var childFiles []string
|
||||
childFiles, err = p.ListJSONFiles(filepath.Join(directory, file.Name()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allFiles = append(allFiles, childFiles...)
|
||||
} else if strings.HasSuffix(file.Name(), ".json") {
|
||||
allFiles = append(allFiles, filepath.Join(directory, file.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
return allFiles, nil
|
||||
}
|
||||
|
||||
func (p *Project) ReadFile(filepath string) ([]byte, error) {
|
||||
file, err := p.fs.Open(filepath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return io.ReadAll(file)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue