blob: b51f7fd12d349ee9006a15f7cf16bc46246d0ae8 [file] [log] [blame]
Robin Thellend18205cf2014-10-21 13:53:59 -07001package main
Jiri Simsab04fbb22014-06-27 17:38:04 -07002
3import (
4 "fmt"
Jiri Simsab04fbb22014-06-27 17:38:04 -07005
Jiri Simsa764efb72014-12-25 20:57:03 -08006 "v.io/core/veyron/services/mgmt/lib/binary"
Todd Wang478fcf92014-12-26 12:37:37 -08007 "v.io/lib/cmdline"
Jiri Simsab04fbb22014-06-27 17:38:04 -07008)
9
10var cmdDelete = &cmdline.Command{
11 Run: runDelete,
12 Name: "delete",
Jiri Simsa432cc2e2014-12-08 15:53:38 -080013 Short: "Delete a binary",
Jiri Simsab04fbb22014-06-27 17:38:04 -070014 Long: "Delete connects to the binary repository and deletes the specified binary",
Jiri Simsa51d78fc2014-07-09 18:34:08 -070015 ArgsName: "<von>",
16 ArgsLong: "<von> is the veyron object name of the binary to delete",
Jiri Simsab04fbb22014-06-27 17:38:04 -070017}
18
19func runDelete(cmd *cmdline.Command, args []string) error {
20 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070021 return cmd.UsageErrorf("delete: incorrect number of arguments, expected %d, got %d", expected, got)
Jiri Simsab04fbb22014-06-27 17:38:04 -070022 }
Jiri Simsa51d78fc2014-07-09 18:34:08 -070023 von := args[0]
Matt Rosencrantzf541b772015-01-13 07:58:59 -080024 if err := binary.Delete(gctx, von); err != nil {
Jiri Simsab04fbb22014-06-27 17:38:04 -070025 return err
26 }
27 fmt.Fprintf(cmd.Stdout(), "Binary deleted successfully\n")
28 return nil
29}
30
31var cmdDownload = &cmdline.Command{
32 Run: runDownload,
33 Name: "download",
Jiri Simsa432cc2e2014-12-08 15:53:38 -080034 Short: "Download a binary",
Jiri Simsab04fbb22014-06-27 17:38:04 -070035 Long: `
36Download connects to the binary repository, downloads the specified binary, and
37writes it to a file.
38`,
Jiri Simsa51d78fc2014-07-09 18:34:08 -070039 ArgsName: "<von> <filename>",
Jiri Simsab04fbb22014-06-27 17:38:04 -070040 ArgsLong: `
Jiri Simsa51d78fc2014-07-09 18:34:08 -070041<von> is the veyron object name of the binary to download
Jiri Simsab04fbb22014-06-27 17:38:04 -070042<filename> is the name of the file where the binary will be written
43`,
44}
45
46func runDownload(cmd *cmdline.Command, args []string) error {
47 if expected, got := 2, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070048 return cmd.UsageErrorf("download: incorrect number of arguments, expected %d, got %d", expected, got)
Jiri Simsab04fbb22014-06-27 17:38:04 -070049 }
Jiri Simsa51d78fc2014-07-09 18:34:08 -070050 von, filename := args[0], args[1]
Matt Rosencrantzf541b772015-01-13 07:58:59 -080051 if err := binary.DownloadToFile(gctx, von, filename); err != nil {
Jiri Simsab04fbb22014-06-27 17:38:04 -070052 return err
53 }
Jiri Simsab04fbb22014-06-27 17:38:04 -070054 fmt.Fprintf(cmd.Stdout(), "Binary downloaded to file %s\n", filename)
55 return nil
56}
57
58var cmdUpload = &cmdline.Command{
59 Run: runUpload,
60 Name: "upload",
Jiri Simsa432cc2e2014-12-08 15:53:38 -080061 Short: "Upload a binary",
Jiri Simsab04fbb22014-06-27 17:38:04 -070062 Long: `
63Upload connects to the binary repository and uploads the binary of the specified
64file. When successful, it writes the name of the new binary to stdout.
65`,
Jiri Simsa51d78fc2014-07-09 18:34:08 -070066 ArgsName: "<von> <filename>",
Jiri Simsab04fbb22014-06-27 17:38:04 -070067 ArgsLong: `
Jiri Simsa51d78fc2014-07-09 18:34:08 -070068<von> is the veyron object name of the binary to upload
Jiri Simsab04fbb22014-06-27 17:38:04 -070069<filename> is the name of the file to upload
70`,
71}
72
73func runUpload(cmd *cmdline.Command, args []string) error {
Robin Thellende2627892014-11-26 09:34:37 -080074 // TODO(rthellend): Add support for creating packages on the fly.
Jiri Simsab04fbb22014-06-27 17:38:04 -070075 if expected, got := 2, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070076 return cmd.UsageErrorf("upload: incorrect number of arguments, expected %d, got %d", expected, got)
Jiri Simsab04fbb22014-06-27 17:38:04 -070077 }
Jiri Simsa51d78fc2014-07-09 18:34:08 -070078 von, filename := args[0], args[1]
Matt Rosencrantzf541b772015-01-13 07:58:59 -080079 if err := binary.UploadFromFile(gctx, von, filename); err != nil {
Jiri Simsab04fbb22014-06-27 17:38:04 -070080 return err
81 }
Jiri Simsa51d78fc2014-07-09 18:34:08 -070082 fmt.Fprintf(cmd.Stdout(), "Binary uploaded from file %s\n", filename)
Jiri Simsab04fbb22014-06-27 17:38:04 -070083 return nil
84}
85
Jiri Simsa432cc2e2014-12-08 15:53:38 -080086var cmdURL = &cmdline.Command{
87 Run: runURL,
88 Name: "url",
89 Short: "Fetch a download URL",
90 Long: "Connect to the binary repository and fetch the download URL for the given veyron object name.",
91 ArgsName: "<von>",
92 ArgsLong: "<von> is the veyron object name of the binary repository",
93}
94
95func runURL(cmd *cmdline.Command, args []string) error {
96 if expected, got := 1, len(args); expected != got {
97 return cmd.UsageErrorf("rooturl: incorrect number of arguments, expected %d, got %d", expected, got)
98 }
99 von := args[0]
Matt Rosencrantzf541b772015-01-13 07:58:59 -0800100 url, _, err := binary.DownloadURL(gctx, von)
Jiri Simsa432cc2e2014-12-08 15:53:38 -0800101 if err != nil {
102 return err
103 }
104 fmt.Fprintf(cmd.Stdout(), "%v\n", url)
105 return nil
106}
107
Robin Thellend18205cf2014-10-21 13:53:59 -0700108func root() *cmdline.Command {
Jiri Simsab04fbb22014-06-27 17:38:04 -0700109 return &cmdline.Command{
Todd Wangfcb72a52014-10-01 09:53:56 -0700110 Name: "binary",
111 Short: "Tool for interacting with the veyron binary repository",
112 Long: `
113The binary tool facilitates interaction with the veyron binary repository.
114`,
Jiri Simsa432cc2e2014-12-08 15:53:38 -0800115 Children: []*cmdline.Command{cmdDelete, cmdDownload, cmdUpload, cmdURL},
Jiri Simsab04fbb22014-06-27 17:38:04 -0700116 }
117}