version: add --deps flag to show dependencies and other build info

This commit is contained in:
Nick Craig-Wood 2025-01-13 12:49:18 +00:00
parent 530658e0cc
commit 45ba81c726

View File

@ -3,10 +3,13 @@ package version
import ( import (
"context" "context"
"debug/buildinfo"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"runtime/debug"
"strings" "strings"
"time" "time"
@ -20,12 +23,14 @@ import (
var ( var (
check = false check = false
deps = false
) )
func init() { func init() {
cmd.Root.AddCommand(commandDefinition) cmd.Root.AddCommand(commandDefinition)
cmdFlags := commandDefinition.Flags() cmdFlags := commandDefinition.Flags()
flags.BoolVarP(cmdFlags, &check, "check", "", false, "Check for new version", "") flags.BoolVarP(cmdFlags, &check, "check", "", false, "Check for new version", "")
flags.BoolVarP(cmdFlags, &deps, "deps", "", false, "Show the Go dependencies", "")
} }
var commandDefinition = &cobra.Command{ var commandDefinition = &cobra.Command{
@ -67,18 +72,25 @@ Or
beta: 1.42.0.5 (released 2018-06-17) beta: 1.42.0.5 (released 2018-06-17)
upgrade: https://beta.rclone.org/v1.42-005-g56e1e820 upgrade: https://beta.rclone.org/v1.42-005-g56e1e820
If you supply the --deps flag then rclone will print a list of all the
packages it depends on and their versions along with some other
information about the build.
`, `,
Annotations: map[string]string{ Annotations: map[string]string{
"versionIntroduced": "v1.33", "versionIntroduced": "v1.33",
}, },
Run: func(command *cobra.Command, args []string) { RunE: func(command *cobra.Command, args []string) error {
ctx := context.Background() ctx := context.Background()
cmd.CheckArgs(0, 0, command, args) cmd.CheckArgs(0, 0, command, args)
if deps {
return printDependencies()
}
if check { if check {
CheckVersion(ctx) CheckVersion(ctx)
} else { } else {
cmd.ShowVersion() cmd.ShowVersion()
} }
return nil
}, },
} }
@ -151,3 +163,36 @@ func CheckVersion(ctx context.Context) {
fmt.Println("Your version is compiled from git so comparisons may be wrong.") fmt.Println("Your version is compiled from git so comparisons may be wrong.")
} }
} }
// Print info about a build module
func printModule(module *debug.Module) {
if module.Replace != nil {
fmt.Printf("- %s %s (replaced by %s %s)\n",
module.Path, module.Version, module.Replace.Path, module.Replace.Version)
} else {
fmt.Printf("- %s %s\n", module.Path, module.Version)
}
}
// printDependencies shows the packages we use in a format like go.mod
func printDependencies() error {
info, err := buildinfo.ReadFile(os.Args[0])
if err != nil {
return fmt.Errorf("error reading build info: %w", err)
}
fmt.Println("Go Version:")
fmt.Printf("- %s\n", info.GoVersion)
fmt.Println("Main package:")
printModule(&info.Main)
fmt.Println("Binary path:")
fmt.Printf("- %s\n", info.Path)
fmt.Println("Settings:")
for _, setting := range info.Settings {
fmt.Printf("- %s: %s\n", setting.Key, setting.Value)
}
fmt.Println("Dependencies:")
for _, dep := range info.Deps {
printModule(dep)
}
return nil
}