1 // Copyright (c) 2020 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 "github.com/sirupsen/logrus"
27 versionScriptPath = "./src/scripts/version"
28 defaultVppApiDir = "/usr/share/vpp/api"
31 func ResolveVppVersion(inputDir string) string {
32 // assuming VPP package is installed
33 if inputDir == defaultVppApiDir {
34 // resolve VPP version using dpkg
35 cmd := exec.Command("dpkg-query", "-f", "${Version}", "-W", "vpp")
36 out, err := cmd.CombinedOutput()
38 logrus.Warnf("resolving VPP version from installed package failed: %v", err)
39 logrus.Warnf("command output: %s", out)
41 version := strings.TrimSpace(string(out))
42 logrus.Debugf("resolved VPP version from installed package: %v", version)
46 // check if inside VPP git repo
48 repo := findVppGitRepo(inputDir)
50 cmd := exec.Command(versionScriptPath)
52 out, err := cmd.CombinedOutput()
54 logrus.Warnf("resolving VPP version from version script failed: %v", err)
55 logrus.Warnf("command output: %s", out)
57 version := strings.TrimSpace(string(out))
58 logrus.Debugf("resolved VPP version from version script: %v", version)
62 file, err := ioutil.ReadFile(path.Join(inputDir, "VPP_VERSION"))
64 return strings.TrimSpace(string(file))
67 logrus.Warnf("VPP version could not be resolved, you can set it manually using VPP_API_VERSION env var")
71 func findVppGitRepo(dir string) string {
72 cmd := exec.Command("git", "rev-parse", "--show-toplevel")
74 out, err := cmd.CombinedOutput()
76 logrus.Warnf("checking VPP git repo failed: %v", err)
77 logrus.Warnf("command output: %s", out)
80 return strings.TrimSpace(string(out))