File: packages.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (129 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package track1

import (
	"fmt"
	"go/parser"
	"go/token"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

// Package represents a track 1 SDK package
type Package struct {
	root    string
	dir     string
	pkgName string
}

// Root ...
func (p Package) Root() string {
	return p.root
}

// Path ...
func (p Package) Path() string {
	path, _ := filepath.Rel(p.root, p.dir)
	return strings.ReplaceAll(path, "\\", "/")
}

// FullPath ...
func (p Package) FullPath() string {
	return p.dir
}

// Name ...
func (p Package) Name() string {
	return p.pkgName
}

// IsARMPackage ...
func (p Package) IsARMPackage() bool {
	return strings.Index(p.Path(), "/mgmt/") > -1
}

// List lists all the track 1 SDK packages under the root directory
func List(root string) ([]Package, error) {
	var results []Package
	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			// check if leaf dir
			fi, err := ioutil.ReadDir(path)
			if err != nil {
				return err
			}
			hasSubDirs := false
			interfacesDir := false
			for _, f := range fi {
				if f.IsDir() {
					hasSubDirs = true
					break
				}
				if f.Name() == "interfaces.go" {
					interfacesDir = true
				}
			}
			if !hasSubDirs {
				fs := token.NewFileSet()
				// with interfaces codegen the majority of leaf directories are now the
				// *api packages. when this is the case parse from the parent directory.
				if interfacesDir {
					path = filepath.Dir(path)
				}
				packages, err := parser.ParseDir(fs, path, func(fi os.FileInfo) bool {
					return fi.Name() != "interfaces.go"
				}, parser.PackageClauseOnly)
				if err != nil {
					return err
				}
				if len(packages) < 1 {
					return fmt.Errorf("did not find any packages in '%s' which is unexpected", path)
				}
				if len(packages) > 1 {
					return fmt.Errorf("found more than one package in '%s' which is unexpected", path)
				}
				pkgName := ""
				for _, pkg := range packages {
					pkgName = pkg.Name
				}
				// normalize the separator
				results = append(results, Package{
					root:    root,
					dir:     strings.ReplaceAll(path, "\\", "/"),
					pkgName: pkgName,
				})
			}
		}
		return nil
	})
	return results, err
}

// VerifyWithDefaultVerifiers verifies packages under the root directory with the given exceptions
func VerifyWithDefaultVerifiers(root string, exceptions map[string]bool) error {
	pkgs, err := List(root)
	if err != nil {
		return fmt.Errorf("failed to get packages: %+v", err)
	}
	verifier := GetDefaultVerifier()
	count := 0
	for _, pkg := range pkgs {
		for _, err := range verifier.Verify(pkg) {
			if _, ok := exceptions[err.Error()]; !ok {
				fmt.Fprintln(os.Stderr, err)
				count++
			}
		}
	}
	if count > 0 {
		return fmt.Errorf("found %d errors", count)
	}
	return nil
}