-
Notifications
You must be signed in to change notification settings - Fork 10
/
report_repos.go
96 lines (79 loc) · 2.47 KB
/
report_repos.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cmd
import (
"fmt"
"os"
"github.com/hashicorp/copywrite/repodata"
"github.com/spf13/cobra"
)
// Flag variables
var (
fields string
fieldsArr []string
githubOrgToAudit string
)
// reportReposCmd represents the report command
var reportReposCmd = &cobra.Command{
Use: "repos",
Short: "Reports on GitHub repos matching specific criteria",
Long: `Reports on GitHub repos matching specific criteria
Outputs the fields you specify in a repodata.csv file in the working directory.`,
PreRun: func(cmd *cobra.Command, args []string) {
// validate flag input
cmd.Println("Getting data... this might take a minute")
var err error
fieldsArr, err = repodata.ValidateInputFields(fields)
if err != nil {
cliLogger.Error("Error validating inputs", err)
}
cobra.CheckErr(err)
},
Run: func(cmd *cobra.Command, args []string) {
// get all public repos under org
unfilteredRepos, err := repodata.GetRepos(githubOrgToAudit)
if err != nil {
cliLogger.Error(fmt.Sprintf("Error retrieving public repos for the \"%v\" org", githubOrgToAudit), err)
}
cobra.CheckErr(err)
// remove archived repos
filteredRepos := repodata.FilterRepos(unfilteredRepos)
// transform repos into a string map
outputData, err := repodata.Transform(filteredRepos)
if err != nil {
cliLogger.Error("Error transforming repo data", err)
}
cobra.CheckErr(err)
t := newTableWriter(cmd.OutOrStdout())
t.AppendHeader(stringArrayToRow(fieldsArr))
// Populate rows
for _, r := range outputData {
// Filter the row to just contain the fields we care about
row := make([]interface{}, 0)
for _, k := range fieldsArr {
row = append(row, r[k])
}
t.AppendRow(row)
}
// Pretty-print the table
t.Render()
// Now let's render the CSV for backwards compatibility
csvFile, err := os.Create("repodata.csv")
if err != nil {
cliLogger.Error("Error creating CSV of repo data", err)
}
cobra.CheckErr(err)
t.SetOutputMirror(csvFile)
t.RenderCSV()
err = csvFile.Close()
if err != nil {
cliLogger.Error("Error closing file", err)
}
cobra.CheckErr(err)
},
}
func init() {
reportCmd.AddCommand(reportReposCmd)
reportReposCmd.Flags().StringVarP(&fields, "fields", "f", "Name,License,HTMLURL", "Repo attributes you wish to report on")
reportReposCmd.Flags().StringVar(&githubOrgToAudit, "github-org", "hashicorp", "Sets the target GitHub org who's repos you wish to audit")
}