-
Notifications
You must be signed in to change notification settings - Fork 9
/
version.go
109 lines (90 loc) · 2.99 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package version
import (
"context"
"fmt"
"io"
"runtime"
"runtime/debug"
"github.com/kr/text"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/cli/version"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/versions"
"namespacelabs.dev/foundation/schema/storage"
)
func NewVersionCmd() *cobra.Command {
var buildInfo bool
cmd := &cobra.Command{
Use: "version",
Short: "Outputs the compiled version of Namespace.",
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
if buildInfo {
info, ok := debug.ReadBuildInfo()
if !ok {
return fnerrors.InternalError("buildinfo is missing")
}
fmt.Fprintln(console.Stdout(ctx), info.String())
return nil
}
v, err := CollectVersionInfo()
if err != nil {
return err
}
out := console.Stdout(ctx)
FormatVersionInfo(out, v)
return nil
}),
}
cmd.PersistentFlags().BoolVar(&buildInfo, "build_info", buildInfo, "Output all of build info.")
return cmd
}
type VersionInfo struct {
Binary *storage.NamespaceBinaryVersion `json:"binary"`
GOOS string `json:"GOOS"`
GOARCH string `json:"GOARCH"`
APIVersion int `json:"api_version"`
CacheVersion int `json:"cache_version"`
ToolAPIVersion int `json:"tool_api_version"`
}
func CollectVersionInfo() (*VersionInfo, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
return nil, fnerrors.InternalError("buildinfo is missing")
}
v, err := version.VersionFrom(info)
if err != nil {
return nil, err
}
if v.GitCommit == "" {
return nil, fnerrors.InternalError("binary does not include version information")
}
return &VersionInfo{
Binary: v,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
APIVersion: versions.Builtin().APIVersion,
CacheVersion: versions.Builtin().CacheVersion,
ToolAPIVersion: versions.ToolAPIVersion,
}, nil
}
func FormatVersionInfo(out io.Writer, v *VersionInfo) {
FormatBinaryVersion(out, v.Binary)
x := text.NewIndentWriter(out, []byte(" ")) // align with FormatBinaryVersion
fmt.Fprintf(x, "architecture %s/%s\n", v.GOOS, v.GOARCH)
fmt.Fprintf(x, "internal api %d (cache=%d tools=%d)\n", v.APIVersion, v.CacheVersion, v.ToolAPIVersion)
}
func FormatBinaryVersion(out io.Writer, v *storage.NamespaceBinaryVersion) {
fmt.Fprintf(out, "version %s (commit %s)\n", v.Version, v.GitCommit)
x := text.NewIndentWriter(out, []byte(" "))
if v.BuildTimeStr != "" {
fmt.Fprintf(x, "commit date %s\n", v.BuildTimeStr)
}
if v.Modified {
fmt.Fprintf(x, "built from modified repo\n")
}
}