@cryptotaxi247 / kubo / commits / 5a1a03bd8

feat: add version deps command

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Mar 21, 2019 at 13:12 UTC 5a1a03bd8f6f12fa7d23cd06dec5c37f21a828b9
2 files changed +55 -1
core/commands/commands_test.go
+2
@@ -38,6 +38,7 @@ func TestROCommands(t *testing.T) {
38 "/refs",
39 "/resolve",
40 "/version",
41 + "/version/deps",
42 }
43
44 cmdSet := make(map[string]struct{})
@@ -211,6 +212,7 @@ func TestCommands(t *testing.T) {
212 "/urlstore",
213 "/urlstore/add",
214 "/version",
215 + "/version/deps",
216 "/cid",
217 "/cid/format",
218 "/cid/base32",
core/commands/version.go
+53 -1
@@ -1,14 +1,16 @@
1 package commands
2
3 import (
4 + "errors"
5 "fmt"
6 "io"
7 "runtime"
8 + "runtime/debug"
9
10 version "github.com/ipfs/go-ipfs"
11 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
12
11 - "github.com/ipfs/go-ipfs-cmdkit"
13 + cmdkit "github.com/ipfs/go-ipfs-cmdkit"
14 cmds "github.com/ipfs/go-ipfs-cmds"
15 )
16
@@ -32,6 +34,9 @@ var VersionCmd = &cmds.Command{
34 Tagline: "Show ipfs version information.",
35 ShortDescription: "Returns the current version of ipfs and exits.",
36 },
37 + Subcommands: map[string]*cmds.Command{
38 + "deps": depsVersionCommand,
39 + },
40
41 Options: []cmdkit.Option{
42 cmdkit.BoolOption(versionNumberOptionName, "n", "Only show the version number."),
@@ -83,3 +88,50 @@ var VersionCmd = &cmds.Command{
88 },
89 Type: VersionOutput{},
90 }
91 +
92 +type Dependency struct {
93 + Path string
94 + Version string
95 + ReplacedBy string
96 + Sum string
97 +}
98 +
99 +var depsVersionCommand = &cmds.Command{
100 + Helptext: cmdkit.HelpText{
101 + Tagline: "Shows information about dependencies used for build",
102 + ShortDescription: `
103 +Print out all dependencies and their versions.`,
104 + },
105 + Type: Dependency{},
106 +
107 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
108 + info, ok := debug.ReadBuildInfo()
109 + if !ok {
110 + return errors.New("no embedded dependency information")
111 + }
112 + toDependency := func(mod *debug.Module) (dep Dependency) {
113 + dep.Path = mod.Path
114 + dep.Version = mod.Version
115 + dep.Sum = mod.Sum
116 + if repl := mod.Replace; repl != nil {
117 + dep.ReplacedBy = fmt.Sprintf("%s@%s", repl.Path, repl.Version)
118 + }
119 + return
120 + }
121 + res.Emit(toDependency(&info.Main))
122 + for _, dep := range info.Deps {
123 + res.Emit(toDependency(dep))
124 + }
125 + return nil
126 + },
127 + Encoders: cmds.EncoderMap{
128 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, dep Dependency) error {
129 + fmt.Fprintf(w, "%s@%s", dep.Path, dep.Version)
130 + if dep.ReplacedBy != "" {
131 + fmt.Fprintf(w, " => %s", dep.ReplacedBy)
132 + }
133 + fmt.Fprintf(w, "\n")
134 + return errors.New("test")
135 + }),
136 + },
137 +}