Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 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] |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 29 | p := repository.ProfileClient(name) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 30 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 31 | defer cancel() |
| 32 | label, err := p.Label(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | fmt.Fprintln(cmd.Stdout(), label) |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | var 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 | |
| 49 | func runDescription(cmd *cmdline.Command, args []string) error { |
| 50 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 51 | 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] | 52 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 53 | name := args[0] |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 54 | p := repository.ProfileClient(name) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 55 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 56 | defer cancel() |
| 57 | desc, err := p.Description(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | fmt.Fprintln(cmd.Stdout(), desc) |
| 62 | return nil |
| 63 | } |
| 64 | |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 65 | var cmdSpecification = &cmdline.Command{ |
| 66 | Run: runSpecification, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 67 | 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 Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 74 | func runSpecification(cmd *cmdline.Command, args []string) error { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 75 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 76 | 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] | 77 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 78 | name := args[0] |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 79 | p := repository.ProfileClient(name) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 80 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 81 | defer cancel() |
| 82 | spec, err := p.Specification(ctx) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | fmt.Fprintf(cmd.Stdout(), "%#v\n", spec) |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | var 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 | |
| 99 | func runPut(cmd *cmdline.Command, args []string) error { |
| 100 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 101 | 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] | 102 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 103 | name := args[0] |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 104 | p := repository.ProfileClient(name) |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 105 | |
| 106 | // TODO(rthellend): Read an actual specification from a file. |
| 107 | spec := profile.Specification{ |
Jiri Simsa | 2f8bc27 | 2014-07-16 12:29:15 -0700 | [diff] [blame] | 108 | Arch: build.AMD64, |
| 109 | Description: "Example profile to test the profile manager implementation.", |
| 110 | Format: build.ELF, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 111 | Libraries: map[profile.Library]struct{}{profile.Library{Name: "foo", MajorVersion: "1", MinorVersion: "0"}: struct{}{}}, |
| 112 | Label: "example", |
Jiri Simsa | 2f8bc27 | 2014-07-16 12:29:15 -0700 | [diff] [blame] | 113 | OS: build.Linux, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 114 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 115 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 116 | defer cancel() |
| 117 | if err := p.Put(ctx, spec); err != nil { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 118 | return err |
| 119 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 120 | fmt.Fprintln(cmd.Stdout(), "Profile added successfully.") |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 121 | return nil |
| 122 | } |
| 123 | |
| 124 | var 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 | |
| 133 | func runRemove(cmd *cmdline.Command, args []string) error { |
| 134 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 135 | 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] | 136 | } |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 137 | name := args[0] |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 138 | p := repository.ProfileClient(name) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 139 | ctx, cancel := rt.R().NewContext().WithTimeout(time.Minute) |
| 140 | defer cancel() |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 141 | if err := p.Remove(ctx); err != nil { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 142 | return err |
| 143 | } |
| 144 | fmt.Fprintln(cmd.Stdout(), "Profile removed successfully.") |
| 145 | return nil |
| 146 | } |
| 147 | |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 148 | func root() *cmdline.Command { |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 149 | return &cmdline.Command{ |
Todd Wang | fcb72a5 | 2014-10-01 09:53:56 -0700 | [diff] [blame] | 150 | Name: "profile", |
| 151 | Short: "Tool for interacting with the veyron profile repository", |
| 152 | Long: ` |
| 153 | The profile tool facilitates interaction with the veyron profile repository. |
| 154 | `, |
Jiri Simsa | 1023a34 | 2014-08-20 18:01:22 -0700 | [diff] [blame] | 155 | Children: []*cmdline.Command{cmdLabel, cmdDescription, cmdSpecification, cmdPut, cmdRemove}, |
Robin Thellend | 9259f3b | 2014-05-21 10:07:24 -0700 | [diff] [blame] | 156 | } |
| 157 | } |