-
Notifications
You must be signed in to change notification settings - Fork 566
/
cmd.go
93 lines (84 loc) · 2.15 KB
/
cmd.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
package cmd
import (
"fmt"
"io"
"os"
"text/tabwriter"
"time"
"google.golang.org/grpc"
"github.com/pachyderm/pachyderm/src/client"
pfscmds "github.com/pachyderm/pachyderm/src/server/pfs/cmds"
ppscmds "github.com/pachyderm/pachyderm/src/server/pps/cmds"
"github.com/spf13/cobra"
"go.pedge.io/pb/go/google/protobuf"
"go.pedge.io/pkg/cobra"
"go.pedge.io/proto/version"
"golang.org/x/net/context"
)
func PachctlCmd(address string) (*cobra.Command, error) {
rootCmd := &cobra.Command{
Use: os.Args[0],
Long: `Access the Pachyderm API.
Envronment variables:
ADDRESS=0.0.0.0:30650, the server to connect to.
`,
}
pfsCmds := pfscmds.Cmds(address)
for _, cmd := range pfsCmds {
rootCmd.AddCommand(cmd)
}
ppsCmds, err := ppscmds.Cmds(address)
if err != nil {
return nil, err
}
for _, cmd := range ppsCmds {
rootCmd.AddCommand(cmd)
}
version := &cobra.Command{
Use: "version",
Short: "Return version information.",
Long: "Return version information.",
Run: pkgcobra.RunFixedArgs(0, func(args []string) error {
versionClient, err := getVersionAPIClient(address)
if err != nil {
return err
}
ctx, _ := context.WithTimeout(context.Background(), time.Second)
version, err := versionClient.GetVersion(ctx, &google_protobuf.Empty{})
if err != nil {
return err
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
printVersionHeader(writer)
printVersion(writer, "pachctl", client.Version)
printVersion(writer, "pachd", version)
return writer.Flush()
}),
}
rootCmd.AddCommand(version)
return rootCmd, nil
}
func getVersionAPIClient(address string) (protoversion.APIClient, error) {
clientConn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return nil, err
}
return protoversion.NewAPIClient(clientConn), nil
}
func printVersionHeader(w io.Writer) {
fmt.Fprintf(w, "COMPONENT\tVERSION\t\n")
}
func printVersion(w io.Writer, component string, version *protoversion.Version) {
fmt.Fprintf(
w,
"%s\t%d.%d.%d",
component,
version.Major,
version.Minor,
version.Micro,
)
if version.Additional != "" {
fmt.Fprintf(w, "(%s)", version.Additional)
}
fmt.Fprintf(w, "\t\n")
}