Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 1 | package impl |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 5 | "time" |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 6 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 7 | "veyron.io/veyron/veyron/lib/cmdline" |
| 8 | "veyron.io/veyron/veyron/services/mgmt/profile" |
| 9 | "veyron.io/veyron/veyron/services/mgmt/repository" |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 10 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 11 | "veyron.io/veyron/veyron2/rt" |
| 12 | "veyron.io/veyron/veyron2/services/mgmt/build" |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var 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 | |
| 24 | func runLabel(cmd *cmdline.Command, args []string) error { |
| 25 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 26 | return cmd.UsageErrorf("label: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 27 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 28 | name := args[0] |
| 29 | p, err := repository.BindProfile(name) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 30 | if err != nil { |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 31 | return fmt.Errorf("BindProfile(%v) failed: %v", name, err) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 32 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 33 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 34 | defer cancel() |
| 35 | label, err := p.Label(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | fmt.Fprintln(cmd.Stdout(), label) |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | var 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 | |
| 52 | func runDescription(cmd *cmdline.Command, args []string) error { |
| 53 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 54 | return cmd.UsageErrorf("description: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 55 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 56 | name := args[0] |
| 57 | p, err := repository.BindProfile(name) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 58 | if err != nil { |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 59 | return fmt.Errorf("BindProfile(%v) failed: %v", name, err) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 60 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 61 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 62 | defer cancel() |
| 63 | desc, err := p.Description(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | fmt.Fprintln(cmd.Stdout(), desc) |
| 68 | return nil |
| 69 | } |
| 70 | |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 71 | var cmdSpecification = &cmdline.Command{ |
| 72 | Run: runSpecification, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 73 | 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 Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 80 | func runSpecification(cmd *cmdline.Command, args []string) error { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 81 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 82 | return cmd.UsageErrorf("spec: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 83 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 84 | name := args[0] |
| 85 | p, err := repository.BindProfile(name) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 86 | if err != nil { |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 87 | return fmt.Errorf("BindProfile(%v) failed: %v", name, err) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 88 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 89 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 90 | defer cancel() |
| 91 | spec, err := p.Specification(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | fmt.Fprintf(cmd.Stdout(), "%#v\n", spec) |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | var 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 | |
| 108 | func runPut(cmd *cmdline.Command, args []string) error { |
| 109 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 110 | return cmd.UsageErrorf("put: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 111 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 112 | name := args[0] |
| 113 | p, err := repository.BindProfile(name) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 114 | if err != nil { |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 115 | return fmt.Errorf("BindProfile(%v) failed: %v", name, err) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // TODO(rthellend): Read an actual specification from a file. |
| 119 | spec := profile.Specification{ |
Jiri Simsa | 2f8bc27 | 2014-07-16 12:29:15 -0700 | [diff] [blame] | 120 | Arch: build.AMD64, |
| 121 | Description: "Example profile to test the profile manager implementation.", |
| 122 | Format: build.ELF, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 123 | Libraries: map[profile.Library]struct{}{profile.Library{Name: "foo", MajorVersion: "1", MinorVersion: "0"}: struct{}{}}, |
| 124 | Label: "example", |
Jiri Simsa | 2f8bc27 | 2014-07-16 12:29:15 -0700 | [diff] [blame] | 125 | OS: build.Linux, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 126 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 127 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 128 | defer cancel() |
| 129 | if err := p.Put(ctx, spec); err != nil { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 130 | return err |
| 131 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 132 | fmt.Fprintln(cmd.Stdout(), "Profile added successfully.") |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 133 | return nil |
| 134 | } |
| 135 | |
| 136 | var 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 | |
| 145 | func runRemove(cmd *cmdline.Command, args []string) error { |
| 146 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 147 | return cmd.UsageErrorf("remove: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 148 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 149 | name := args[0] |
| 150 | p, err := repository.BindProfile(name) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 151 | if err != nil { |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 152 | return fmt.Errorf("BindProfile(%v) failed: %v", name, err) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 153 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 154 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 155 | defer cancel() |
| 156 | if err = p.Remove(ctx); err != nil { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 157 | return err |
| 158 | } |
| 159 | fmt.Fprintln(cmd.Stdout(), "Profile removed successfully.") |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | func Root() *cmdline.Command { |
| 164 | return &cmdline.Command{ |
| 165 | Name: "profile", |
Jiri Simsa | ddbfebb | 2014-06-20 15:56:06 -0700 | [diff] [blame] | 166 | Short: "Command-line tool for interacting with the veyron profile repository", |
| 167 | Long: "Command-line tool for interacting with the veyron profile repository", |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 168 | Children: []*cmdline.Command{cmdLabel, cmdDescription, cmdSpecification, cmdPut, cmdRemove}, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 169 | } |
| 170 | } |