prometheus-borg-exporter/list.go

45 lines
736 B
Go
Raw Normal View History

2024-04-01 17:02:29 +02:00
package main
import (
"encoding/json"
"os/exec"
"time"
)
type List struct {
Archives []struct {
Time string
}
}
func (list *List) LastArchiveUnix() float64 {
if len(list.Archives) == 0 {
return 0
}
last, err := time.Parse("2006-01-02T15:04:05.999999", list.Archives[len(list.Archives)-1].Time)
if err != nil {
return 0
}
return float64(last.Unix())
}
func GetList(path string) (*List, error) {
cmd := exec.Command("borg", "list", "--json", path)
cmd.Env = append(cmd.Env, "BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes")
stdout, err := cmd.Output()
if err != nil {
return nil, err
}
data := &List{}
err = json.Unmarshal([]byte(stdout), data)
if err != nil {
return nil, err
}
return data, nil
}