-
Notifications
You must be signed in to change notification settings - Fork 839
/
package.go
135 lines (120 loc) · 4.16 KB
/
package.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
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
130
131
132
133
134
135
// Copyright 2018 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"errors"
"fmt"
"github.com/Azure/azure-sdk-for-go/tools/apidiff/exports"
"github.com/Azure/azure-sdk-for-go/tools/apidiff/repo"
"github.com/Azure/azure-sdk-for-go/tools/apidiff/report"
"github.com/spf13/cobra"
)
var (
dirMode bool
asMarkdown bool
)
var packageCmd = &cobra.Command{
Use: "package <package dir> (<base commit> <target commit(s)>) | (<commit sequence>)",
Short: "Generates a report for the package in the specified directory containing the delta between commits.",
Long: `The package command generates a report for the package in the directory specified in <package dir>.
Commits can be specified as either a base and one or more target commits or a sequence of commits.
For a base/target pair each target commit is compared against the base commit.
For a commit sequence each commit N in the sequence is compared against commit N+1.
Commit sequences must be comma-delimited.`,
RunE: func(cmd *cobra.Command, args []string) error {
rpt, err := thePackageCmd(args)
if err != nil {
return err
}
evalReportStatus(rpt)
return nil
},
}
// split into its own func as we can't call os.Exit from it (the defer won't get executed)
func thePackageCmd(args []string) (rs reportStatus, err error) {
if dirMode {
return packageCmdDirMode(args)
}
cloneRepo, err := processArgsAndClone(args)
if err != nil {
return
}
var rpt commitPkgReport
rpt.CommitsReports = map[string]report.Package{}
worker := func(pkgDir string, cloneRepo repo.WorkingTree, baseCommit, targetCommit string) error {
// lhs
vprintf("checking out base commit %s and gathering exports\n", baseCommit)
var lhs exports.Content
lhs, err = getContentForCommit(cloneRepo, pkgDir, baseCommit)
if err != nil {
return err
}
// rhs
vprintf("checking out target commit %s and gathering exports\n", targetCommit)
var rhs exports.Content
rhs, err = getContentForCommit(cloneRepo, pkgDir, targetCommit)
if err != nil {
return err
}
r := report.Generate(lhs, rhs, onlyBreakingChangesFlag, onlyAdditionsFlag)
if r.HasBreakingChanges() {
rpt.BreakingChanges = append(rpt.BreakingChanges, targetCommit)
}
rpt.CommitsReports[fmt.Sprintf("%s:%s", baseCommit, targetCommit)] = r
return nil
}
err = generateReports(args, cloneRepo, worker)
if err != nil {
return
}
err = printReport(rpt)
return
}
func init() {
packageCmd.PersistentFlags().BoolVarP(&dirMode, "directories", "i", false, "compares packages in two different directories")
packageCmd.PersistentFlags().BoolVarP(&asMarkdown, "markdown", "m", false, "emits the report in markdown format")
rootCmd.AddCommand(packageCmd)
}
func getContentForCommit(wt repo.WorkingTree, dir, commit string) (cnt exports.Content, err error) {
err = wt.Checkout(commit)
if err != nil {
err = fmt.Errorf("failed to check out commit '%s': %s", commit, err)
return
}
cnt, err = exports.Get(dir)
if err != nil {
err = fmt.Errorf("failed to get exports for commit '%s': %s", commit, err)
}
return
}
func packageCmdDirMode(args []string) (rs reportStatus, err error) {
if len(args) != 2 {
return nil, errors.New("directories mode requires two arguments")
}
lhs, err := exports.Get(args[0])
if err != nil {
return nil, fmt.Errorf("failed to get exports for package '%s': %s", args[0], err)
}
rhs, err := exports.Get(args[1])
if err != nil {
return nil, fmt.Errorf("failed to get exports for package '%s': %s", args[1], err)
}
r := report.Generate(lhs, rhs, onlyBreakingChangesFlag, onlyAdditionsFlag)
if asMarkdown && !suppressReport {
println(r.ToMarkdown())
} else {
err = printReport(r)
}
return r, err
}