blob: e19876ee8fff2a4e5cdd546a76d93415a290b663 [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
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
19package main
20
21import (
22 "flag"
23 "fmt"
24 "io/ioutil"
25 "os"
26 "strings"
27
28 "github.com/joho/godotenv"
29)
30
31var (
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
39func 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 Bazanski662b5b32020-12-21 13:49:00 +010056 version, ok := statusVars[*flagStampVar]
Lorenz Brun878f5f92020-05-12 16:15:39 +020057 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 Bazanski662b5b32020-12-21 13:49:00 +010065 "VERSION": version,
66 "VERSION_ID": version,
67 "PRETTY_NAME": *flagName + " " + version,
Lorenz Brun878f5f92020-05-12 16:15:39 +020068 }
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}