blob: 2216a60c38b17e26c3f3d43202e5679b9312ab98 [file] [log] [blame]
Robin Thellend18205cf2014-10-21 13:53:59 -07001package main
Robin Thellend9259f3b2014-05-21 10:07:24 -07002
3import (
4 "fmt"
Matt Rosencrantz137b8d22014-08-18 09:56:15 -07005 "time"
Robin Thellend9259f3b2014-05-21 10:07:24 -07006
Jiri Simsa519c5072014-09-17 21:37:57 -07007 "veyron.io/veyron/veyron/lib/cmdline"
8 "veyron.io/veyron/veyron/services/mgmt/profile"
9 "veyron.io/veyron/veyron/services/mgmt/repository"
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -070010
Jiri Simsa519c5072014-09-17 21:37:57 -070011 "veyron.io/veyron/veyron2/rt"
12 "veyron.io/veyron/veyron2/services/mgmt/build"
Robin Thellend9259f3b2014-05-21 10:07:24 -070013)
14
15var cmdLabel = &cmdline.Command{
16 Run: runLabel,
17 Name: "label",
18 Short: "Shows a human-readable profile key for the profile.",
19 Long: "Shows a human-readable profile key for the profile.",
20 ArgsName: "<profile>",
21 ArgsLong: "<profile> is the full name of the profile.",
22}
23
24func runLabel(cmd *cmdline.Command, args []string) error {
25 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070026 return cmd.UsageErrorf("label: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -070027 }
Jiri Simsa1023a342014-08-20 18:01:22 -070028 name := args[0]
Todd Wang702385a2014-11-07 01:54:08 -080029 p := repository.ProfileClient(name)
Matt Rosencrantz137b8d22014-08-18 09:56:15 -070030 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
31 defer cancel()
32 label, err := p.Label(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070033 if err != nil {
34 return err
35 }
36 fmt.Fprintln(cmd.Stdout(), label)
37 return nil
38}
39
40var cmdDescription = &cmdline.Command{
41 Run: runDescription,
42 Name: "description",
43 Short: "Shows a human-readable profile description for the profile.",
44 Long: "Shows a human-readable profile description for the profile.",
45 ArgsName: "<profile>",
46 ArgsLong: "<profile> is the full name of the profile.",
47}
48
49func runDescription(cmd *cmdline.Command, args []string) error {
50 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070051 return cmd.UsageErrorf("description: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -070052 }
Jiri Simsa1023a342014-08-20 18:01:22 -070053 name := args[0]
Todd Wang702385a2014-11-07 01:54:08 -080054 p := repository.ProfileClient(name)
Matt Rosencrantz137b8d22014-08-18 09:56:15 -070055 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
56 defer cancel()
57 desc, err := p.Description(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070058 if err != nil {
59 return err
60 }
61 fmt.Fprintln(cmd.Stdout(), desc)
62 return nil
63}
64
Jiri Simsa1023a342014-08-20 18:01:22 -070065var cmdSpecification = &cmdline.Command{
66 Run: runSpecification,
Robin Thellend9259f3b2014-05-21 10:07:24 -070067 Name: "spec",
68 Short: "Shows the specification of the profile.",
69 Long: "Shows the specification of the profile.",
70 ArgsName: "<profile>",
71 ArgsLong: "<profile> is the full name of the profile.",
72}
73
Jiri Simsa1023a342014-08-20 18:01:22 -070074func runSpecification(cmd *cmdline.Command, args []string) error {
Robin Thellend9259f3b2014-05-21 10:07:24 -070075 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070076 return cmd.UsageErrorf("spec: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -070077 }
Jiri Simsa1023a342014-08-20 18:01:22 -070078 name := args[0]
Todd Wang702385a2014-11-07 01:54:08 -080079 p := repository.ProfileClient(name)
Matt Rosencrantz137b8d22014-08-18 09:56:15 -070080 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
81 defer cancel()
82 spec, err := p.Specification(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070083 if err != nil {
84 return err
85 }
86 fmt.Fprintf(cmd.Stdout(), "%#v\n", spec)
87 return nil
88}
89
90var cmdPut = &cmdline.Command{
91 Run: runPut,
92 Name: "put",
93 Short: "Sets a placeholder specification for the profile.",
94 Long: "Sets a placeholder specification for the profile.",
95 ArgsName: "<profile>",
96 ArgsLong: "<profile> is the full name of the profile.",
97}
98
99func runPut(cmd *cmdline.Command, args []string) error {
100 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -0700101 return cmd.UsageErrorf("put: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700102 }
Jiri Simsa1023a342014-08-20 18:01:22 -0700103 name := args[0]
Todd Wang702385a2014-11-07 01:54:08 -0800104 p := repository.ProfileClient(name)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700105
106 // TODO(rthellend): Read an actual specification from a file.
107 spec := profile.Specification{
Jiri Simsa2f8bc272014-07-16 12:29:15 -0700108 Arch: build.AMD64,
109 Description: "Example profile to test the profile manager implementation.",
110 Format: build.ELF,
Robin Thellend9259f3b2014-05-21 10:07:24 -0700111 Libraries: map[profile.Library]struct{}{profile.Library{Name: "foo", MajorVersion: "1", MinorVersion: "0"}: struct{}{}},
112 Label: "example",
Jiri Simsa2f8bc272014-07-16 12:29:15 -0700113 OS: build.Linux,
Robin Thellend9259f3b2014-05-21 10:07:24 -0700114 }
Matt Rosencrantz137b8d22014-08-18 09:56:15 -0700115 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
116 defer cancel()
117 if err := p.Put(ctx, spec); err != nil {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700118 return err
119 }
Jiri Simsa1023a342014-08-20 18:01:22 -0700120 fmt.Fprintln(cmd.Stdout(), "Profile added successfully.")
Robin Thellend9259f3b2014-05-21 10:07:24 -0700121 return nil
122}
123
124var cmdRemove = &cmdline.Command{
125 Run: runRemove,
126 Name: "remove",
127 Short: "removes the profile specification for the profile.",
128 Long: "removes the profile specification for the profile.",
129 ArgsName: "<profile>",
130 ArgsLong: "<profile> is the full name of the profile.",
131}
132
133func runRemove(cmd *cmdline.Command, args []string) error {
134 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -0700135 return cmd.UsageErrorf("remove: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700136 }
Jiri Simsa1023a342014-08-20 18:01:22 -0700137 name := args[0]
Todd Wang702385a2014-11-07 01:54:08 -0800138 p := repository.ProfileClient(name)
Matt Rosencrantz137b8d22014-08-18 09:56:15 -0700139 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
140 defer cancel()
Todd Wang702385a2014-11-07 01:54:08 -0800141 if err := p.Remove(ctx); err != nil {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700142 return err
143 }
144 fmt.Fprintln(cmd.Stdout(), "Profile removed successfully.")
145 return nil
146}
147
Robin Thellend18205cf2014-10-21 13:53:59 -0700148func root() *cmdline.Command {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700149 return &cmdline.Command{
Todd Wangfcb72a52014-10-01 09:53:56 -0700150 Name: "profile",
151 Short: "Tool for interacting with the veyron profile repository",
152 Long: `
153The profile tool facilitates interaction with the veyron profile repository.
154`,
Jiri Simsa1023a342014-08-20 18:01:22 -0700155 Children: []*cmdline.Command{cmdLabel, cmdDescription, cmdSpecification, cmdPut, cmdRemove},
Robin Thellend9259f3b2014-05-21 10:07:24 -0700156 }
157}