|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/GitHubSecurityLab/gh-qldb/utils" |
5 |
| - "github.com/spf13/cobra" |
6 |
| - "path/filepath" |
7 |
| - "log" |
8 |
| - "fmt" |
9 |
| - "io/ioutil" |
10 |
| - "encoding/json" |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/GitHubSecurityLab/gh-qldb/utils" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "path/filepath" |
11 | 11 | )
|
| 12 | + |
12 | 13 | var infoCmd = &cobra.Command{
|
13 |
| - Use: "info", |
14 |
| - Short: "Returns information for a database stored in the QLDB structure", |
15 |
| - Long: `Returns information for a database stored in the QLDB structure`, |
16 |
| - Run: func(cmd *cobra.Command, args []string) { |
17 |
| - info(nwoFlag, languageFlag) |
18 |
| - }, |
19 |
| - } |
| 14 | + Use: "info", |
| 15 | + Short: "Returns information about a database stored in the QLDB structure", |
| 16 | + Long: `Returns information about a database stored in the QLDB structure`, |
| 17 | + Run: func(cmd *cobra.Command, args []string) { |
| 18 | + info(nwoFlag, languageFlag) |
| 19 | + }, |
| 20 | +} |
20 | 21 |
|
21 | 22 | func init() {
|
22 |
| - rootCmd.AddCommand(infoCmd) |
23 |
| - infoCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to get the database for.") |
24 |
| - infoCmd.Flags().StringVarP(&languageFlag, "language", "l", "", "The primary language you want the database for.") |
25 |
| - infoCmd.Flags().BoolVarP(&jsonFlag, "json", "j", false, "Use json as the output format.") |
26 |
| - infoCmd.MarkFlagRequired("nwo") |
27 |
| - infoCmd.MarkFlagRequired("language") |
| 23 | + rootCmd.AddCommand(infoCmd) |
| 24 | + infoCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to get the database for.") |
| 25 | + infoCmd.Flags().StringVarP(&languageFlag, "language", "l", "", "The primary language you want the database for.") |
| 26 | + infoCmd.Flags().BoolVarP(&jsonFlag, "json", "j", false, "Use json as the output format.") |
| 27 | + infoCmd.MarkFlagRequired("nwo") |
| 28 | + infoCmd.MarkFlagRequired("language") |
28 | 29 | }
|
29 | 30 |
|
30 | 31 | func info(nwo string, language string) {
|
31 |
| - dir := filepath.Join(utils.GetPath(nwo), language) |
32 |
| - files, err := ioutil.ReadDir(dir) |
33 |
| - if err != nil { |
34 |
| - log.Fatal(err) |
35 |
| - } |
36 |
| - var pathList []map[string]string |
37 |
| - for _, f := range files { |
38 |
| - filename := f.Name() |
39 |
| - sha := filename[:len(filename)-len(filepath.Ext(filename))] |
40 |
| - commitSha, committedDate, err := utils.GetCommitInfo(nwo, sha) |
41 |
| - if err != nil { |
42 |
| - log.Fatal(err) |
43 |
| - } |
44 |
| - pathList = append(pathList, map[string]string{ |
45 |
| - "commitSha": commitSha, |
46 |
| - "committedDate": committedDate, |
47 |
| - "path": filepath.Join(dir, filename), |
48 |
| - }) |
49 |
| - } |
50 |
| - if jsonFlag { |
51 |
| - jsonStr, err := json.MarshalIndent(pathList, "", " ") |
52 |
| - if err != nil { |
53 |
| - log.Fatal(err) |
54 |
| - } |
55 |
| - fmt.Printf("%s", jsonStr) |
56 |
| - } else { |
57 |
| - for _, path := range pathList { |
58 |
| - fmt.Printf("%s (%s)", path["path"], path["committedDate"]) |
59 |
| - } |
60 |
| - } |
| 32 | + dir := filepath.Join(utils.GetPath(nwo), language) |
| 33 | + files, err := ioutil.ReadDir(dir) |
| 34 | + if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | + } |
| 37 | + var pathList []map[string]string |
| 38 | + for _, f := range files { |
| 39 | + filename := f.Name() |
| 40 | + sha := filename[:len(filename)-len(filepath.Ext(filename))] |
| 41 | + commitSha, committedDate, err := utils.GetCommitInfo(nwo, sha) |
| 42 | + if err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | + pathList = append(pathList, map[string]string{ |
| 46 | + "commitSha": commitSha, |
| 47 | + "committedDate": committedDate, |
| 48 | + "path": filepath.Join(dir, filename), |
| 49 | + }) |
| 50 | + } |
| 51 | + if jsonFlag { |
| 52 | + jsonStr, err := json.MarshalIndent(pathList, "", " ") |
| 53 | + if err != nil { |
| 54 | + log.Fatal(err) |
| 55 | + } |
| 56 | + fmt.Printf("%s", jsonStr) |
| 57 | + } else { |
| 58 | + for _, path := range pathList { |
| 59 | + fmt.Printf("%s (%s)", path["path"], path["committedDate"]) |
| 60 | + } |
| 61 | + } |
61 | 62 | }
|
62 |
| - |
0 commit comments