| skipped 24 lines |
25 | 25 | | listInfo(args[0]) |
26 | 26 | | }, |
27 | 27 | | } |
| 28 | + | |
| 29 | + | expCmd.AddCommand(&cobra.Command{ |
| 30 | + | Use: "mod /path/to/go/file", |
| 31 | + | Aliases: []string{"m"}, |
| 32 | + | Short: "Display go mod info.", |
| 33 | + | Args: cobra.ExactArgs(1), |
| 34 | + | Run: func(cmd *cobra.Command, args []string) { |
| 35 | + | listModInfo(args[0]) |
| 36 | + | }, |
| 37 | + | }) |
| 38 | + | |
28 | 39 | | rootCmd.AddCommand(infoCMD) |
29 | 40 | | } |
30 | 41 | | |
| skipped 52 lines |
83 | 94 | | t.Print() |
84 | 95 | | } |
85 | 96 | | |
| 97 | + | func listModInfo(fileStr string) { |
| 98 | + | fp, err := filepath.Abs(fileStr) |
| 99 | + | if err != nil { |
| 100 | + | fmt.Fprintf(os.Stderr, "Failed to parse the filepath: %s.\n", err) |
| 101 | + | os.Exit(1) |
| 102 | + | } |
| 103 | + | |
| 104 | + | f, err := gore.Open(fp) |
| 105 | + | if err != nil { |
| 106 | + | fmt.Fprintf(os.Stderr, "Error when opening the file: %s.\n", err) |
| 107 | + | os.Exit(1) |
| 108 | + | } |
| 109 | + | defer f.Close() |
| 110 | + | |
| 111 | + | if f.BuildInfo.ModInfo == nil { |
| 112 | + | fmt.Fprintf(os.Stderr, "No mod info found in the file.\n") |
| 113 | + | return |
| 114 | + | } |
| 115 | + | |
| 116 | + | mod := f.BuildInfo.ModInfo |
| 117 | + | |
| 118 | + | t := tabby.New() |
| 119 | + | |
| 120 | + | t.AddHeader("Type", "Name", "Version", "Replaced by", "Hash") |
| 121 | + | |
| 122 | + | t.AddLine("main", mod.Main.Path, mod.Main.Version, "", mod.Main.Sum) |
| 123 | + | |
| 124 | + | for _, m := range mod.Deps { |
| 125 | + | if m.Replace != nil { |
| 126 | + | t.AddLine("dep", m.Path, m.Version, m.Replace.Path, m.Sum) |
| 127 | + | t.AddLine("replacement", m.Replace.Path, m.Replace.Version, "", m.Sum) |
| 128 | + | } else { |
| 129 | + | t.AddLine("dep", m.Path, m.Version, "", m.Sum) |
| 130 | + | } |
| 131 | + | } |
| 132 | + | t.Print() |
| 133 | + | fmt.Printf("\n") |
| 134 | + | } |
| 135 | + | |