blob: ad6e3e24371167ee0508f00e9335a3b071d20d6d [file] [log] [blame]
Lorenz Brun878f5f92020-05-12 16:15:39 +02001// 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
Serge Bazanski216fe7b2021-05-21 18:36:16 +020017// genosrelease provides rudimentary support to generate os-release files
18// following the freedesktop spec from arguments and stamping
19//
20// https://www.freedesktop.org/software/systemd/man/os-release.html
Lorenz Brun878f5f92020-05-12 16:15:39 +020021package main
22
23import (
24 "flag"
25 "fmt"
26 "io/ioutil"
27 "os"
28 "strings"
29
30 "github.com/joho/godotenv"
31)
32
33var (
34 flagStatusFile = flag.String("status_file", "", "path to bazel workspace status file")
35 flagOutFile = flag.String("out_file", "os-release", "path to os-release output file")
36 flagStampVar = flag.String("stamp_var", "", "variable to use as version from the workspace status file")
37 flagName = flag.String("name", "", "name parameter (see freedesktop spec)")
38 flagID = flag.String("id", "", "id parameter (see freedesktop spec)")
39)
40
41func main() {
42 flag.Parse()
43 statusFileContent, err := ioutil.ReadFile(*flagStatusFile)
44 if err != nil {
45 fmt.Printf("Failed to open bazel workspace status file: %v\n", err)
46 os.Exit(1)
47 }
48 statusVars := make(map[string]string)
49 for _, line := range strings.Split(string(statusFileContent), "\n") {
50 line = strings.TrimSpace(line)
51 parts := strings.Fields(line)
52 if len(parts) != 2 {
53 continue
54 }
55 statusVars[parts[0]] = parts[1]
56 }
57
Serge Bazanski662b5b32020-12-21 13:49:00 +010058 version, ok := statusVars[*flagStampVar]
Lorenz Brun878f5f92020-05-12 16:15:39 +020059 if !ok {
60 fmt.Printf("%v key not set in bazel workspace status file\n", *flagStampVar)
61 os.Exit(1)
62 }
63 // As specified by https://www.freedesktop.org/software/systemd/man/os-release.html
64 osReleaseVars := map[string]string{
65 "NAME": *flagName,
66 "ID": *flagID,
Serge Bazanski662b5b32020-12-21 13:49:00 +010067 "VERSION": version,
68 "VERSION_ID": version,
69 "PRETTY_NAME": *flagName + " " + version,
Lorenz Brun878f5f92020-05-12 16:15:39 +020070 }
71 osReleaseContent, err := godotenv.Marshal(osReleaseVars)
72 if err != nil {
73 fmt.Printf("Failed to encode os-release file: %v\n", err)
74 os.Exit(1)
75 }
76 if err := ioutil.WriteFile(*flagOutFile, []byte(osReleaseContent), 0644); err != nil {
77 fmt.Printf("Failed to write os-release file: %v\n", err)
78 os.Exit(1)
79 }
80}