diff options
author | 2020-07-22 04:40:55 +0200 | |
---|---|---|
committer | 2020-07-22 04:40:55 +0200 | |
commit | 58da9ac6e691a8c660eb8ca838a154e11da0db68 (patch) | |
tree | a1bbda04c6d0621ce0fc20779276620f1820190b /binapigen/run.go | |
parent | a155cd438c6558da266c1c5931361ea088b35653 (diff) |
Fix binapigen decoding and minor improvements
- fixed allocating byte slices before copying decoded data
- simplified encoding functions
- several minor improvements
Change-Id: I6669424b89eb86333805cb1b57e4551169980cc2
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'binapigen/run.go')
-rw-r--r-- | binapigen/run.go | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/binapigen/run.go b/binapigen/run.go index 88e32b7..d3a181a 100644 --- a/binapigen/run.go +++ b/binapigen/run.go @@ -48,8 +48,11 @@ func run(apiDir string, filesToGenerate []string, opts Options, fn func(*Generat } if opts.ImportPrefix == "" { - opts.ImportPrefix = resolveImportPath(opts.OutputDir) - logrus.Debugf("resolved import prefix: %s", opts.ImportPrefix) + opts.ImportPrefix, err = resolveImportPath(opts.OutputDir) + if err != nil { + return fmt.Errorf("cannot resolve import path for output dir %s: %w", opts.OutputDir, err) + } + logrus.Infof("resolved import path prefix: %s", opts.ImportPrefix) } gen, err := New(opts, apifiles, filesToGenerate) @@ -93,10 +96,6 @@ func init() { if debug := os.Getenv("DEBUG_GOVPP"); strings.Contains(debug, "binapigen") { Logger.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel) - } else if debug != "" { - Logger.SetLevel(logrus.InfoLevel) - } else { - Logger.SetLevel(logrus.WarnLevel) } } @@ -104,32 +103,30 @@ func logf(f string, v ...interface{}) { Logger.Debugf(f, v...) } -func resolveImportPath(dir string) string { +// resolveImportPath tries to resolve import path for a directory. +func resolveImportPath(dir string) (string, error) { absPath, err := filepath.Abs(dir) if err != nil { - panic(err) + return "", err } modRoot := findGoModuleRoot(absPath) if modRoot == "" { - logrus.Fatalf("module root not found at: %s", absPath) + return "", err } - modPath := findModulePath(path.Join(modRoot, "go.mod")) - if modPath == "" { - logrus.Fatalf("module path not found") + modPath, err := readModulePath(path.Join(modRoot, "go.mod")) + if err != nil { + return "", err } relDir, err := filepath.Rel(modRoot, absPath) if err != nil { - panic(err) + return "", err } - return filepath.Join(modPath, relDir) + return filepath.Join(modPath, relDir), nil } +// findGoModuleRoot looks for enclosing Go module. func findGoModuleRoot(dir string) (root string) { - if dir == "" { - panic("dir not set") - } dir = filepath.Clean(dir) - // Look for enclosing go.mod. for { if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() { return dir @@ -143,18 +140,17 @@ func findGoModuleRoot(dir string) (root string) { return "" } -var ( - modulePathRE = regexp.MustCompile(`module[ \t]+([^ \t\r\n]+)`) -) +var modulePathRE = regexp.MustCompile(`module[ \t]+([^ \t\r\n]+)`) -func findModulePath(file string) string { - data, err := ioutil.ReadFile(file) +// readModulePath reads module path from go.mod file. +func readModulePath(gomod string) (string, error) { + data, err := ioutil.ReadFile(gomod) if err != nil { - return "" + return "", err } m := modulePathRE.FindSubmatch(data) if m == nil { - return "" + return "", err } - return string(m[1]) + return string(m[1]), nil } |