github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/internal/http/generate.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"compress/gzip"
     9  	"os"
    10  	"path"
    11  
    12  	"github.com/Asutorufa/yuhaiin/pkg/log"
    13  )
    14  
    15  //go:generate go run generate.go
    16  
    17  func main() {
    18  	if err := compress("out"); err != nil {
    19  		panic(err)
    20  	}
    21  }
    22  
    23  func compress(f string) error {
    24  	dir, err := os.ReadDir(f)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	for _, v := range dir {
    30  		fs := path.Join(f, v.Name())
    31  
    32  		if v.IsDir() {
    33  			if err := compress(fs); err != nil {
    34  				log.Error("open dir failed", "err", err)
    35  			}
    36  			continue
    37  		}
    38  
    39  		fi, err := v.Info()
    40  		if err != nil {
    41  			return err
    42  		}
    43  		data, err := os.ReadFile(fs)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		var buf bytes.Buffer
    49  
    50  		gz := gzip.NewWriter(&buf)
    51  
    52  		if _, err := gz.Write(data); err != nil {
    53  			return err
    54  		}
    55  		gz.Close()
    56  
    57  		os.Remove(fs)
    58  
    59  		if err := os.WriteFile(fs, buf.Bytes(), fi.Mode()); err != nil {
    60  			return err
    61  		}
    62  	}
    63  
    64  	return nil
    65  }