Refactored binapi generator with message encoding
[govpp.git] / cmd / binapi-generator / util.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
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 package main
16
17 import (
18         "io/ioutil"
19         "os/exec"
20         "path"
21         "strings"
22
23         "github.com/sirupsen/logrus"
24 )
25
26 const (
27         versionScriptPath = "./src/scripts/version"
28         defaultVppApiDir  = "/usr/share/vpp/api"
29 )
30
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()
37                 if err != nil {
38                         logrus.Warnf("resolving VPP version from installed package failed: %v", err)
39                         logrus.Warnf("command output: %s", out)
40                 } else {
41                         version := strings.TrimSpace(string(out))
42                         logrus.Debugf("resolved VPP version from installed package: %v", version)
43                         return version
44                 }
45         }
46         // check if inside VPP git repo
47         if inputDir != "" {
48                 repo := findVppGitRepo(inputDir)
49                 if repo != "" {
50                         cmd := exec.Command(versionScriptPath)
51                         cmd.Dir = repo
52                         out, err := cmd.CombinedOutput()
53                         if err != nil {
54                                 logrus.Warnf("resolving VPP version from version script failed: %v", err)
55                                 logrus.Warnf("command output: %s", out)
56                         } else {
57                                 version := strings.TrimSpace(string(out))
58                                 logrus.Debugf("resolved VPP version from version script: %v", version)
59                                 return version
60                         }
61                 }
62                 file, err := ioutil.ReadFile(path.Join(inputDir, "VPP_VERSION"))
63                 if err == nil {
64                         return strings.TrimSpace(string(file))
65                 }
66         }
67         logrus.Warnf("VPP version could not be resolved, you can set it manually using VPP_API_VERSION env var")
68         return "unknown"
69 }
70
71 func findVppGitRepo(dir string) string {
72         cmd := exec.Command("git", "rev-parse", "--show-toplevel")
73         cmd.Dir = dir
74         out, err := cmd.CombinedOutput()
75         if err != nil {
76                 logrus.Warnf("checking VPP git repo failed: %v", err)
77                 logrus.Warnf("command output: %s", out)
78                 return ""
79         }
80         return strings.TrimSpace(string(out))
81 }