<commit_before>package main

import (
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/user"
	"path"
	"time"
)

type Cache struct {
	ExpireMinute        int
	directory, filepath string
}

func NewCache(cachemin int) Cache {
	c := Cache{ExpireMinute: cachemin}
	c.setFilePath()
	return c
}

func (c *Cache) Delete() {
	if !c.exists() {
		return
	}

	if err := os.Remove(c.filepath); err != nil {
		log.Fatal(err)
	}
}

func (c *Cache) Use() bool {
	return c.ExpireMinute > 0
}

func (c *Cache) Available() bool {
	if !c.exists() {
		return false
	}
	if c.expired() {
		return false
	}
	return true
}

func (c *Cache) Save(list EC2List) {
	mkDir(c.directory)
	f, err := os.Create(c.filepath)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	list.Output(f)
}

func (c *Cache) Output(w io.Writer) {
	f, err := os.Open(c.filepath)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	b, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatal(err)
	}
	_, err = w.Write(b)
	if err != nil {
		log.Fatal(err)
	}
}

func (c *Cache) setFilePath() {
	home := getHomeDir()
	c.directory = path.Join(home, ".go-ec2list")
	c.filepath = path.Join(c.directory, "cache")
}

func (c *Cache) exists() bool {
	_, err := os.Stat(c.filepath)
	return err == nil
}

func (c *Cache) expired() bool {
	s, err := os.Stat(c.filepath)
	if err != nil {
		log.Fatal(err)
	}
	duration := time.Now().Sub(s.ModTime())
	return float64(c.ExpireMinute) < duration.Minutes()
}

func mkDir(dirPath string) {
	if err := os.MkdirAll(dirPath, 0777); err != nil {
		if !os.IsExist(err) {
			log.Fatal(err)
		}
	}
}

func getHomeDir() string {
	var err error
	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	return usr.HomeDir
}<commit_msg>Use go-homedir insted of os/user.<commit_after>package main

import (
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"time"

	"github.com/mitchellh/go-homedir"
)

type Cache struct {
	ExpireMinute        int
	directory, filepath string
}

func NewCache(cachemin int) Cache {
	c := Cache{ExpireMinute: cachemin}
	c.setFilePath()
	return c
}

func (c *Cache) Delete() {
	if !c.exists() {
		return
	}

	if err := os.Remove(c.filepath); err != nil {
		log.Fatal(err)
	}
}

func (c *Cache) Use() bool {
	return c.ExpireMinute > 0
}

func (c *Cache) Available() bool {
	if !c.exists() {
		return false
	}
	if c.expired() {
		return false
	}
	return true
}

func (c *Cache) Save(list EC2List) {
	mkDir(c.directory)
	f, err := os.Create(c.filepath)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	list.Output(f)
}

func (c *Cache) Output(w io.Writer) {
	f, err := os.Open(c.filepath)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	b, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatal(err)
	}
	_, err = w.Write(b)
	if err != nil {
		log.Fatal(err)
	}
}

func (c *Cache) setFilePath() {
	home := getHomeDir()
	c.directory = path.Join(home, ".go-ec2list")
	c.filepath = path.Join(c.directory, "cache")
}

func (c *Cache) exists() bool {
	_, err := os.Stat(c.filepath)
	return err == nil
}

func (c *Cache) expired() bool {
	s, err := os.Stat(c.filepath)
	if err != nil {
		log.Fatal(err)
	}
	duration := time.Now().Sub(s.ModTime())
	return float64(c.ExpireMinute) < duration.Minutes()
}

func mkDir(dirPath string) {
	if err := os.MkdirAll(dirPath, 0777); err != nil {
		if !os.IsExist(err) {
			log.Fatal(err)
		}
	}
}

func getHomeDir() string {
	dir, err := homedir.Dir()
	if err != nil {
		log.Fatal(err)
	}
	return dir
}
{'fraction_non_alphanumeric': 0.17438136826783115, 'fraction_numerical': 0.005240174672489083, 'mean_word_length': 3.2472187886279356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5635825', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1481, 'n_words': 347}