Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // genosrelease provides rudimentary support to generate os-release files following the freedesktop spec |
| 18 | // (https://www.freedesktop.org/software/systemd/man/os-release.html) from arguments and stamping |
| 19 | package main |
| 20 | |
| 21 | import ( |
| 22 | "flag" |
| 23 | "fmt" |
| 24 | "io/ioutil" |
| 25 | "os" |
| 26 | "strings" |
| 27 | |
| 28 | "github.com/joho/godotenv" |
| 29 | ) |
| 30 | |
| 31 | var ( |
| 32 | flagStatusFile = flag.String("status_file", "", "path to bazel workspace status file") |
| 33 | flagOutFile = flag.String("out_file", "os-release", "path to os-release output file") |
| 34 | flagStampVar = flag.String("stamp_var", "", "variable to use as version from the workspace status file") |
| 35 | flagName = flag.String("name", "", "name parameter (see freedesktop spec)") |
| 36 | flagID = flag.String("id", "", "id parameter (see freedesktop spec)") |
| 37 | ) |
| 38 | |
| 39 | func main() { |
| 40 | flag.Parse() |
| 41 | statusFileContent, err := ioutil.ReadFile(*flagStatusFile) |
| 42 | if err != nil { |
| 43 | fmt.Printf("Failed to open bazel workspace status file: %v\n", err) |
| 44 | os.Exit(1) |
| 45 | } |
| 46 | statusVars := make(map[string]string) |
| 47 | for _, line := range strings.Split(string(statusFileContent), "\n") { |
| 48 | line = strings.TrimSpace(line) |
| 49 | parts := strings.Fields(line) |
| 50 | if len(parts) != 2 { |
| 51 | continue |
| 52 | } |
| 53 | statusVars[parts[0]] = parts[1] |
| 54 | } |
| 55 | |
Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 56 | version, ok := statusVars[*flagStampVar] |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 57 | if !ok { |
| 58 | fmt.Printf("%v key not set in bazel workspace status file\n", *flagStampVar) |
| 59 | os.Exit(1) |
| 60 | } |
| 61 | // As specified by https://www.freedesktop.org/software/systemd/man/os-release.html |
| 62 | osReleaseVars := map[string]string{ |
| 63 | "NAME": *flagName, |
| 64 | "ID": *flagID, |
Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 65 | "VERSION": version, |
| 66 | "VERSION_ID": version, |
| 67 | "PRETTY_NAME": *flagName + " " + version, |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 68 | } |
| 69 | osReleaseContent, err := godotenv.Marshal(osReleaseVars) |
| 70 | if err != nil { |
| 71 | fmt.Printf("Failed to encode os-release file: %v\n", err) |
| 72 | os.Exit(1) |
| 73 | } |
| 74 | if err := ioutil.WriteFile(*flagOutFile, []byte(osReleaseContent), 0644); err != nil { |
| 75 | fmt.Printf("Failed to write os-release file: %v\n", err) |
| 76 | os.Exit(1) |
| 77 | } |
| 78 | } |