master chld/dgr / pkg / manifest.go
 1package dgrb
 2
 3import (
 4	"fmt"
 5	"git.sr.ht/~chld/dgr/cmd"
 6	"os"
 7	"path/filepath"
 8	"strings"
 9)
10
11func GenM(pkg string, pkginst string) {
12	f, err := os.OpenFile(
13		filepath.Join(os.Getenv("DTR_DB"), "installed"),
14		os.O_WRONLY|os.O_APPEND, 0644) // make sure to append, r/w access
15	if err != nil {
16		dgr.Die("could not open file", filepath.Join(os.Getenv("DTR_DB"), "installed"), 1)
17	}
18	defer f.Close()
19	f.Write([]byte(pkg))
20	f.Write([]byte{10}) // write a EOL too!
21	f.Sync()
22
23	// create a manifest file
24	dgr.Msg("creating manifest file for \033[33m", pkg, "\033[m")
25
26	os.MkdirAll(filepath.Join(os.Getenv("DTR_DB"), pkg), 0755)
27	pkgm := filepath.Join(os.Getenv("DTR_DB"), pkg, "manifest")
28
29	m, err := os.Create(pkgm)
30	if err != nil {
31		dgr.Die("could not create manifest file", pkgm, 1)
32	}
33	defer m.Close()
34
35	pkr, _ := filepath.Glob(pkginst)
36	if err := os.Chdir(strings.Join(pkr, "")); err != nil {
37		dgr.Die("could not chdir to path", strings.Join(pkr, ""), 1)
38	}
39
40	err = filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
41		if err != nil {
42			return err
43		}
44
45		// skip the root "."
46		if path == "." {
47			return nil
48		}
49
50		fmt.Fprintln(m, "/"+path) // write paths to m
51		return nil
52	})
53
54	if err != nil {
55		panic(err)
56	}
57
58}