forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
76 lines (65 loc) · 1.91 KB
/
root.go
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package cmd
import (
"bufio"
"fmt"
"os"
"path/filepath"
"github.com/Azure/azure-sdk-for-go/tools/pkgchk/track1"
"github.com/spf13/cobra"
)
var exceptFileFlag string
var rootCmd = &cobra.Command{
Use: "pkgchk <dir>",
Short: "Performs package validation tasks against all packages found under the specified directory.",
Long: `This tool will perform various package validation checks against all of the packages
found under the specified directory. Failures can be baselined and thus ignored by
copying the failure text verbatim, pasting it into a text file then specifying that
file via the optional exceptions flag.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return theCommand(args)
},
}
func init() {
rootCmd.PersistentFlags().StringVarP(&exceptFileFlag, "exceptions", "e", "", "text file containing the list of exceptions")
}
// Execute executes the specified command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func theCommand(args []string) error {
root, err := filepath.Abs(args[0])
if err != nil {
return fmt.Errorf("failed to get absolute path: %+v", err)
}
exceptions, err := loadExceptions(exceptFileFlag)
if err != nil {
return fmt.Errorf("failed to load exceptions: %+v", err)
}
return track1.VerifyWithDefaultVerifiers(root, exceptions)
}
func loadExceptions(exceptFile string) (map[string]bool, error) {
if exceptFile == "" {
return nil, nil
}
f, err := os.Open(exceptFile)
if err != nil {
return nil, err
}
defer f.Close()
exceptions := make(map[string]bool)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
exceptions[scanner.Text()] = true
}
if err = scanner.Err(); err != nil {
return nil, err
}
return exceptions, nil
}