Projects STRLCPY redress Commits 1c1851c4
🤬
  • Display go.mod info if it's available in the file.

  • Loading...
  • TcM1911 committed 3 years ago
    1c1851c4
    1 parent 34c0386f
  • ■ ■ ■ ■ ■ ■
    exp.go
     1 +// Copyright 2021 The GoRE Authors. All rights reserved.
     2 +// Use of this source code is governed by the license that
     3 +// can be found in the LICENSE file.
     4 + 
     5 +package main
     6 + 
     7 +import (
     8 + "github.com/spf13/cobra"
     9 +)
     10 + 
     11 +var expCmd = &cobra.Command{
     12 + Use: "experiment",
     13 + Aliases: []string{"exp", "x"},
     14 + Short: "Experimental functionality",
     15 + Long: expHelp,
     16 + Hidden: true,
     17 +}
     18 + 
     19 +func init() {
     20 + // Register the command.
     21 + rootCmd.AddCommand(expCmd)
     22 +}
     23 + 
     24 +const expHelp = `Experimental functionality
     25 + 
     26 +The following commands are experimental and may change or removed
     27 +in the future.
     28 +`
     29 + 
  • ■ ■ ■ ■ ■ ■
    info.go
    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 + 
Please wait...
Page is in error, reload to recover