xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / cmd / site / remote.go

3.7 KB
1package main
2
3import (
4	"errors"
5	"fmt"
6	"log"
7	"os"
8	"path/filepath"
9	"regexp"
10	"slices"
11	"strings"
12
13	"xonecas.io/site/internal/site"
14	"xonecas.io/site/internal/store"
15)
16
17// remote lists the remotes in the sources file, or adds, removes or creates one.
18// add mirrors a remote and refreshes the hosts' metadata, as import does. new makes a repo the site
19// hosts, to push to over SSH; each push updates the site. rm deletes a mirror, but keeps a hosted repo.
20func remote(args []string, sources, root, meta string) error {
21	urls, err := store.Sources(sources)
22	if err != nil {
23		return err
24	}
25	if len(args) == 1 && args[0] == "list" {
26		for _, url := range urls {
27			fmt.Println(url)
28		}
29		return nil
30	}
31	if len(args) != 2 || !slices.Contains([]string{"add", "rm", "new"}, args[0]) {
32		return errors.New("usage: site remote list | new <name> | add <path or url> | rm <path or url>; site help shows more")
33	}
34	if args[0] == "new" {
35		if err := newRepo(args[1], urls, sources, root, meta); err != nil {
36			return err
37		}
38		host := strings.TrimPrefix(site.BaseURL, "https://")
39		fmt.Printf("git remote add origin %s@%s:%s.git\ngit push -u origin main\n", os.Getenv("USER"), host, args[1])
40		return nil
41	}
42	url, err := store.Clean(args[1])
43	if err != nil {
44		return err
45	}
46	dir := store.Dir(root, url)
47	if args[0] == "rm" {
48		if err := store.RemoveSource(sources, url); err != nil {
49			return err
50		}
51		if store.Hosted(dir) {
52			log.Printf("removed %s; its repo stays at %s", url, dir)
53			return nil
54		}
55		log.Printf("removed %s; site index and site build drop it", url)
56		return os.RemoveAll(dir)
57	}
58	if err := store.AddSource(sources, url); err != nil {
59		return err
60	}
61	if err := importAll(root, []string{url}, append(urls, url), 1, meta); err != nil {
62		return errors.Join(err, store.RemoveSource(sources, url), os.RemoveAll(dir))
63	}
64	log.Printf("added %s; site index and site build list it", url)
65	return nil
66}
67
68// hostedURL is the sources line, and public clone URL, of the repo the site hosts as name.
69func hostedURL(name string) string { return site.BaseURL + "/" + name + ".git" }
70
71// newRepo creates the hosted repo name in the store and lists it.
72// Its hook rebuilds the site in the background, one build at a time, so a push returns at once.
73func newRepo(name string, urls []string, sources, root, meta string) error {
74	if !regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`).MatchString(name) || strings.HasSuffix(name, ".git") {
75		return fmt.Errorf("%q is not a repo name: lower case letters, digits, . _ and -", name)
76	}
77	for _, u := range urls {
78		if strings.TrimSuffix(filepath.Base(u), ".git") == name {
79			return fmt.Errorf("%s already names a project", u)
80		}
81	}
82	url := hostedURL(name)
83	dir, err := filepath.Abs(store.Dir(root, url))
84	if err != nil {
85		return err
86	}
87	exe, err := os.Executable()
88	if err != nil {
89		return err
90	}
91	wd, _ := os.Getwd()
92	lock, _ := filepath.Abs(filepath.Join(root, ".build.lock"))
93	build := fmt.Sprintf("%s index -sources %s -store %s -meta %s", exe, sources, root, meta)
94	hook := fmt.Sprintf("unset GIT_DIR\ncd '%s' && flock '%s' sh -c '%s' < /dev/null >> '%s' 2>&1 &", wd, lock, build, filepath.Join(dir, "build.log"))
95	if err := store.Create(dir, hook); err != nil {
96		return err
97	}
98	if err := store.AddSource(sources, url); err != nil {
99		return errors.Join(err, os.RemoveAll(dir))
100	}
101	return nil
102}
103
104// importAll mirrors the remotes in sync, then saves the hosts' metadata for all of them to meta.
105func importAll(root string, sync, all []string, jobs int, meta string) error {
106	if failed := mirrorAll(root, sync, jobs); failed > 0 {
107		return fmt.Errorf("%d of %d remotes failed", failed, len(sync))
108	}
109	metas, err := store.FetchMeta(all)
110	if err != nil {
111		return err
112	}
113	return writeJSON(meta, metas)
114}