Home

ugit @main - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / cmd / ugit-uci / main.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/go-git/go-git/v5"
)

func maine() error {
	repoDir, ok := os.LookupEnv("UGIT_REPODIR")
	if !ok {
		panic("UGIT_REPODIR not set")
	}

	urlFlag := flag.String("url", "http://localhost:3448", "URL for UCI")
	flag.StringVar(urlFlag, "u", *urlFlag, "--url")
	repoDirFlag := flag.String("repo-dir", "", "Repo dir (including .git)")
	flag.StringVar(repoDirFlag, "rd", *repoDirFlag, "--repo-dir")
	manifestFlag := flag.String("manifest", ".uci.jsonnet", "Path to manifest in repo")
	flag.StringVar(manifestFlag, "m", *manifestFlag, "--manifest")
	flag.Parse()

	if *repoDirFlag != "" {
		repoDir = *repoDirFlag
	}

	repo, err := git.PlainOpen(repoDir)
	if err != nil {
		return fmt.Errorf("could not open git dir %q: %w", repoDir, err)
	}

	tree, err := repo.Worktree()
	if err != nil {
		return fmt.Errorf("could not get worktree: %w", err)
	}

	fi, err := tree.Filesystem.Open(*manifestFlag)
	if err != nil {
		return fmt.Errorf("could not open manifest %q: %w", *manifestFlag, err)
	}
	defer fi.Close()

	data, err := io.ReadAll(fi)
	if err != nil {
		return fmt.Errorf("could not read manifest: %w", err)
	}

	resp, err := http.Post(*urlFlag, "application/jsonnet", bytes.NewReader(data))
	if err != nil {
		return fmt.Errorf("could not post manifest: %w", err)
	}
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("non-ok response: %s", resp.Status)
	}

	return nil
}

func main() {
	if err := maine(); err != nil {
		fmt.Println(err)
	}
}