1// Copyright (C) 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Minimum support of KMI version in Go. Keep in sync with libkver.
16
17package gki
18
19import (
20	"fmt"
21	"regexp"
22)
23
24var digits = "([0-9]+)"
25var reKmi = regexp.MustCompile("^([0-9]+)[.]([0-9]+)-(android[0-9]+)-([0-9]+|unstable)$")
26
27// Input is a valid KMI version, e.g. 5.4-android12-0.
28// Return a sanitized string to be used as a suffix of APEX package name
29// com.android.gki.kmi_5_4_android12_0
30// Keep in sync with libkver.
31func kmiVersionToApexName(s string) (string, error) {
32	matches := reKmi.FindAllStringSubmatch(s, 4)
33
34	if matches == nil {
35		return "", fmt.Errorf("Poorly formed KMI version: %q must match regex %q", s, reKmi)
36	}
37
38	version := matches[0][1]
39	patchLevel := matches[0][2]
40	androidRelease := matches[0][3]
41	kmiGeneration := matches[0][4]
42
43	return fmt.Sprintf("com.android.gki.kmi_%s_%s_%s_%s",
44		version, patchLevel, androidRelease, kmiGeneration), nil
45}
46