Beispiel #1
0
// removeOldKeys removes keys older than specified age
func removeOldKeys() error {
	datas, err := backends.List()
	if err != nil {
		return err
	}

	now := time.Now()

	lumber.Debug("Garbage Collector - Finding files...")
	for _, data := range datas {

		// clean-after defaults to Now() to ensure no files are deleted in case
		// cobra decides to change how 'Command.Flag().Changed' works. It does this
		// because no files, written by hoarder, will have a modified time before the
		// Unix epoch began
		if data.ModTime.Unix() < (now.Unix() - int64(viper.GetInt("clean-after"))) {
			lumber.Debug("Cleaning key: ", data.Name)
			if err := backends.Remove(data.Name); err != nil {
				return fmt.Errorf("Cleaning of '%s' failed - %v", data.Name, err.Error())
			}
		}
	}

	return nil
}
Beispiel #2
0
// test list
func TestList(t *testing.T) {
	DataInfo, err := backends.List()
	if err != nil {
		t.Error("Failed to LIST file - ", err)
	}
	if DataInfo[0].Name != "testfile" {
		t.Error("Failed to LIST file - incorrect file found: %s", DataInfo[0].Name)
	}
}
Beispiel #3
0
// list returns a list of all keys with relevant information
func list(rw http.ResponseWriter, req *http.Request) {

	//
	fis, err := backends.List()
	if err != nil {
		rw.WriteHeader(500)
		rw.Write([]byte(fmt.Sprintf("%s\n", err.Error())))
		return
	}

	rw.Header().Set("Content-Type", "application/json")

	//
	jfis, err := json.Marshal(fis)
	if err != nil {
		rw.WriteHeader(500)
		rw.Write([]byte(fmt.Sprintf("%s\n", err.Error())))
		return
	}

	//
	rw.Write(append(jfis, byte('\n')))
}