build/fietsje: split into monogon-specific library and cli tool

This is a first pass at sightly modularizing fietsje. This allows
fietsje-for-Monogon to be used as a Go library, and moves all the
toolbase startup logic into its own executable package. This allows us
to call fietsje from some multi-purpose CI/check tool that I'm slowly
implementing on the side.

Fietsje should still be split up further, allowing a generic fietsje
library to be used for more than just the Monogon repository - but that
will come at a later point.

Change-Id: Ic59c0bb954c5416fda95d3604d5aa94553dc1030
Reviewed-on: https://review.monogon.dev/c/monogon/+/331
Reviewed-by: Mateusz Zalega <mateusz@monogon.tech>
diff --git a/build/fietsje/cmd/main.go b/build/fietsje/cmd/main.go
new file mode 100644
index 0000000..b9e1553
--- /dev/null
+++ b/build/fietsje/cmd/main.go
@@ -0,0 +1,49 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// fietsje is the standalone command line tool which calls into the Fietsje
+// library (//build/fietsje) to perform actual work. The split between cli and
+// main library is used so that Fietsje can be called from other Go tooling
+// without having to shell out to a binary.
+package main
+
+import (
+	"log"
+	"os"
+	"path"
+
+	"source.monogon.dev/build/fietsje"
+	"source.monogon.dev/build/toolbase"
+	"source.monogon.dev/build/toolbase/gotoolchain"
+)
+
+func main() {
+	// Get absolute path of Monogon workspace directory currently operating on
+	// (either via bazel run or by running it directly in the root of a checkout),
+	// use it to build paths to shelf.pb.txt and repositories.bzl.
+	wd, err := toolbase.WorkspaceDirectory()
+	if err != nil {
+		log.Fatalf("%v", err)
+	}
+	shelfPath := path.Join(wd, "third_party/go/shelf.pb.text")
+	repositoriesBzlPath := path.Join(wd, "third_party/go/repositories.bzl")
+	// Set GOROOT as required by fietsje/go-the-tool.
+	os.Setenv("GOROOT", gotoolchain.Root)
+
+	if err := fietsje.Monogon(shelfPath, repositoriesBzlPath); err != nil {
+		log.Fatal(err)
+	}
+}