blob: 441389637fd009a2a49b6fb6feaa703e521d3938 [file] [log] [blame]
Robin Thellend9259f3b2014-05-21 10:07:24 -07001package impl
2
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]
29 p, err := repository.BindProfile(name)
Robin Thellend9259f3b2014-05-21 10:07:24 -070030 if err != nil {
Jiri Simsa1023a342014-08-20 18:01:22 -070031 return fmt.Errorf("BindProfile(%v) failed: %v", name, err)
Robin Thellend9259f3b2014-05-21 10:07:24 -070032 }
Matt Rosencrantz137b8d22014-08-18 09:56:15 -070033 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
34 defer cancel()
35 label, err := p.Label(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070036 if err != nil {
37 return err
38 }
39 fmt.Fprintln(cmd.Stdout(), label)
40 return nil
41}
42
43var cmdDescription = &cmdline.Command{
44 Run: runDescription,
45 Name: "description",
46 Short: "Shows a human-readable profile description for the profile.",
47 Long: "Shows a human-readable profile description for the profile.",
48 ArgsName: "<profile>",
49 ArgsLong: "<profile> is the full name of the profile.",
50}
51
52func runDescription(cmd *cmdline.Command, args []string) error {
53 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070054 return cmd.UsageErrorf("description: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -070055 }
Jiri Simsa1023a342014-08-20 18:01:22 -070056 name := args[0]
57 p, err := repository.BindProfile(name)
Robin Thellend9259f3b2014-05-21 10:07:24 -070058 if err != nil {
Jiri Simsa1023a342014-08-20 18:01:22 -070059 return fmt.Errorf("BindProfile(%v) failed: %v", name, err)
Robin Thellend9259f3b2014-05-21 10:07:24 -070060 }
Matt Rosencrantz137b8d22014-08-18 09:56:15 -070061 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
62 defer cancel()
63 desc, err := p.Description(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070064 if err != nil {
65 return err
66 }
67 fmt.Fprintln(cmd.Stdout(), desc)
68 return nil
69}
70
Jiri Simsa1023a342014-08-20 18:01:22 -070071var cmdSpecification = &cmdline.Command{
72 Run: runSpecification,
Robin Thellend9259f3b2014-05-21 10:07:24 -070073 Name: "spec",
74 Short: "Shows the specification of the profile.",
75 Long: "Shows the specification of the profile.",
76 ArgsName: "<profile>",
77 ArgsLong: "<profile> is the full name of the profile.",
78}
79
Jiri Simsa1023a342014-08-20 18:01:22 -070080func runSpecification(cmd *cmdline.Command, args []string) error {
Robin Thellend9259f3b2014-05-21 10:07:24 -070081 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -070082 return cmd.UsageErrorf("spec: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -070083 }
Jiri Simsa1023a342014-08-20 18:01:22 -070084 name := args[0]
85 p, err := repository.BindProfile(name)
Robin Thellend9259f3b2014-05-21 10:07:24 -070086 if err != nil {
Jiri Simsa1023a342014-08-20 18:01:22 -070087 return fmt.Errorf("BindProfile(%v) failed: %v", name, err)
Robin Thellend9259f3b2014-05-21 10:07:24 -070088 }
Matt Rosencrantz137b8d22014-08-18 09:56:15 -070089 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
90 defer cancel()
91 spec, err := p.Specification(ctx)
Robin Thellend9259f3b2014-05-21 10:07:24 -070092 if err != nil {
93 return err
94 }
95 fmt.Fprintf(cmd.Stdout(), "%#v\n", spec)
96 return nil
97}
98
99var cmdPut = &cmdline.Command{
100 Run: runPut,
101 Name: "put",
102 Short: "Sets a placeholder specification for the profile.",
103 Long: "Sets a placeholder specification for the profile.",
104 ArgsName: "<profile>",
105 ArgsLong: "<profile> is the full name of the profile.",
106}
107
108func runPut(cmd *cmdline.Command, args []string) error {
109 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -0700110 return cmd.UsageErrorf("put: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700111 }
Jiri Simsa1023a342014-08-20 18:01:22 -0700112 name := args[0]
113 p, err := repository.BindProfile(name)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700114 if err != nil {
Jiri Simsa1023a342014-08-20 18:01:22 -0700115 return fmt.Errorf("BindProfile(%v) failed: %v", name, err)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700116 }
117
118 // TODO(rthellend): Read an actual specification from a file.
119 spec := profile.Specification{
Jiri Simsa2f8bc272014-07-16 12:29:15 -0700120 Arch: build.AMD64,
121 Description: "Example profile to test the profile manager implementation.",
122 Format: build.ELF,
Robin Thellend9259f3b2014-05-21 10:07:24 -0700123 Libraries: map[profile.Library]struct{}{profile.Library{Name: "foo", MajorVersion: "1", MinorVersion: "0"}: struct{}{}},
124 Label: "example",
Jiri Simsa2f8bc272014-07-16 12:29:15 -0700125 OS: build.Linux,
Robin Thellend9259f3b2014-05-21 10:07:24 -0700126 }
Matt Rosencrantz137b8d22014-08-18 09:56:15 -0700127 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
128 defer cancel()
129 if err := p.Put(ctx, spec); err != nil {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700130 return err
131 }
Jiri Simsa1023a342014-08-20 18:01:22 -0700132 fmt.Fprintln(cmd.Stdout(), "Profile added successfully.")
Robin Thellend9259f3b2014-05-21 10:07:24 -0700133 return nil
134}
135
136var cmdRemove = &cmdline.Command{
137 Run: runRemove,
138 Name: "remove",
139 Short: "removes the profile specification for the profile.",
140 Long: "removes the profile specification for the profile.",
141 ArgsName: "<profile>",
142 ArgsLong: "<profile> is the full name of the profile.",
143}
144
145func runRemove(cmd *cmdline.Command, args []string) error {
146 if expected, got := 1, len(args); expected != got {
Todd Wanga615e4d2014-09-29 16:56:05 -0700147 return cmd.UsageErrorf("remove: incorrect number of arguments, expected %d, got %d", expected, got)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700148 }
Jiri Simsa1023a342014-08-20 18:01:22 -0700149 name := args[0]
150 p, err := repository.BindProfile(name)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700151 if err != nil {
Jiri Simsa1023a342014-08-20 18:01:22 -0700152 return fmt.Errorf("BindProfile(%v) failed: %v", name, err)
Robin Thellend9259f3b2014-05-21 10:07:24 -0700153 }
Matt Rosencrantz137b8d22014-08-18 09:56:15 -0700154 ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute)
155 defer cancel()
156 if err = p.Remove(ctx); err != nil {
Robin Thellend9259f3b2014-05-21 10:07:24 -0700157 return err
158 }
159 fmt.Fprintln(cmd.Stdout(), "Profile removed successfully.")
160 return nil
161}
162
163func Root() *cmdline.Command {
164 return &cmdline.Command{
165 Name: "profile",
Jiri Simsaddbfebb2014-06-20 15:56:06 -0700166 Short: "Command-line tool for interacting with the veyron profile repository",
167 Long: "Command-line tool for interacting with the veyron profile repository",
Jiri Simsa1023a342014-08-20 18:01:22 -0700168 Children: []*cmdline.Command{cmdLabel, cmdDescription, cmdSpecification, cmdPut, cmdRemove},
Robin Thellend9259f3b2014-05-21 10:07:24 -0700169 }
170}