Commit eb7926478032

Vincent Demeester <vincent@sbr.pm>
2020-05-25 09:43:05
tools/bus: fix nightly builds ๐ŸŒ”
Add home as source for the nightly (automatically added for a git push) Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 0364b43
tools/bus/default.nix
@@ -1,19 +1,8 @@
-{ stdenv, lib, go }:
+{ stdenv, lib, buildGoModule }:
 
-stdenv.mkDerivation rec {
+buildGoModule rec {
   name = "bus";
   src = ./.;
 
-  phases = "buildPhase installPhase";
-  buildInputs = [ go ];
-
-  buildPhase = ''
-    HOME=$(pwd)
-    cp $src/main.go .
-    go build -o bus main.go
-  '';
-  installPhase = ''
-    mkdir $out
-    install -D bus $out/bin/bus
-  '';
+  modSha256 = "1633qy8a24pacr337v20ws12p3wgr2kf7q2mymar90qrq301wfnx";
 }
tools/bus/go.mod
@@ -0,0 +1,5 @@
+module home/tools/bus
+
+go 1.14
+
+require gopkg.in/yaml.v2 v2.3.0
tools/bus/go.sum
@@ -0,0 +1,3 @@
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
+gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
tools/bus/main.go
@@ -12,6 +12,8 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+
+	yaml "gopkg.in/yaml.v2"
 )
 
 var (
@@ -28,6 +30,23 @@ type Build struct {
 	Tags     []string `json:"tags"`
 }
 
+// Represents a build trigger object as described on <the docs for
+// this are currently down>
+type Trigger struct {
+	Action    string `json:"action"`
+	Condition string `json:"condition"`
+	To        string `json:"to"`
+}
+
+// Represents a build manifest for sourcehut.
+type Manifest struct {
+	Image    string                `json:"image"`
+	Sources  []string              `json:"sources"`
+	Secrets  []string              `json:"secrets"`
+	Tasks    [](map[string]string) `json:"tasks"`
+	Triggers []Trigger             `json:"triggers"`
+}
+
 func triggerBuild(token, name, manifest, branch string) {
 	build := Build{
 		Manifest: manifest,
@@ -69,6 +88,15 @@ func triggerBuild(token, name, manifest, branch string) {
 	}
 }
 
+func prepareManifest(manifest []byte) ([]byte, error) {
+	var m Manifest
+	if err := yaml.Unmarshal(manifest, &m); err != nil {
+		return manifest, err
+	}
+	m.Sources = append(m.Sources, "https://git.sr.ht/~vdemeester/home")
+	return yaml.Marshal(m)
+}
+
 func main() {
 	flag.Parse()
 	token, err := ioutil.ReadFile(*secret)
@@ -78,7 +106,7 @@ func main() {
 	}
 	files, err := ioutil.ReadDir(*builds)
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "cannot list builds manifest from %s.", *builds)
+		fmt.Fprintf(os.Stderr, "cannot list builds manifest from %s: %v.\n", *builds, err)
 		os.Exit(1)
 	}
 	fmt.Fprintf(os.Stdout, "triggering builds for %v\n", *branch)
@@ -86,7 +114,12 @@ func main() {
 		path := filepath.Join(*builds, f.Name())
 		manifest, err := ioutil.ReadFile(path)
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "cannot read manifest %s.", path)
+			fmt.Fprintf(os.Stderr, "cannot read manifest %s: %v.\n", path, err)
+			os.Exit(1)
+		}
+		manifest, err = prepareManifest(manifest)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "failed to prepare manifest %s: %v.\n", path, err)
 			os.Exit(1)
 		}
 		triggerBuild(string(token), f.Name(), string(manifest), *branch)