Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 5 | // The following enables go generate to generate the doc.go file. |
Jiri Simsa | 32f76fb | 2015-04-07 15:39:23 -0700 | [diff] [blame] | 6 | //go:generate go run $V23_ROOT/release/go/src/v.io/x/lib/cmdline/testdata/gendoc.go . |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 7 | |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "bytes" |
| 12 | "fmt" |
| 13 | "io/ioutil" |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 14 | "regexp" |
| 15 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 16 | "v.io/v23/uniqueid" |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 17 | "v.io/x/lib/cmdline" |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | func main() { |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 21 | cmdline.Main(cmdUniqueId) |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 24 | var cmdUniqueId = &cmdline.Command{ |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 25 | Name: "uniqueid", |
Todd Wang | 6ed3b6c | 2015-04-08 14:37:04 -0700 | [diff] [blame] | 26 | Short: "generates unique identifiers", |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 27 | Long: ` |
Todd Wang | 6ed3b6c | 2015-04-08 14:37:04 -0700 | [diff] [blame] | 28 | Command uniqueid generates unique identifiers. |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 29 | It also has an option of automatically substituting unique ids with placeholders in files. |
| 30 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 31 | Children: []*cmdline.Command{cmdGenerate, cmdInject}, |
| 32 | Topics: []cmdline.Topic{}, |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 35 | var cmdGenerate = &cmdline.Command{ |
| 36 | Runner: cmdline.RunnerFunc(runGenerate), |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 37 | Name: "generate", |
| 38 | Short: "Generates UniqueIds", |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 39 | Long: ` |
| 40 | Generates unique ids and outputs them to standard out. |
| 41 | `, |
| 42 | ArgsName: "", |
| 43 | ArgsLong: "", |
| 44 | } |
| 45 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 46 | var cmdInject = &cmdline.Command{ |
| 47 | Runner: cmdline.RunnerFunc(runInject), |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 48 | Name: "inject", |
| 49 | Short: "Injects UniqueIds into existing files", |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 50 | Long: ` |
| 51 | Injects UniqueIds into existing files. |
| 52 | Strings of the form "$UNIQUEID$" will be replaced with generated ids. |
| 53 | `, |
| 54 | ArgsName: "<filenames>", |
| 55 | ArgsLong: "<filenames> List of files to inject unique ids into", |
| 56 | } |
| 57 | |
| 58 | // runGenerate implements the generate command which outputs generated ids to stdout. |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 59 | func runGenerate(env *cmdline.Env, args []string) error { |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 60 | if len(args) > 0 { |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 61 | return env.UsageErrorf("expected 0 args, got %d", len(args)) |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 62 | } |
| 63 | id, err := uniqueid.Random() |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
Suharsh Sivakumar | 13f8827 | 2015-02-20 17:41:26 -0800 | [diff] [blame] | 67 | fmt.Printf("%#v\n", id) |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // runInject implements the inject command which replaces $UNIQUEID$ strings with generated ids. |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 72 | func runInject(env *cmdline.Env, args []string) error { |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 73 | if len(args) == 0 { |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 74 | return env.UsageErrorf("expected at least one file arg, got 0") |
Benjamin Prosnitz | 7cf836d | 2015-01-30 09:19:19 -0800 | [diff] [blame] | 75 | } |
| 76 | for _, arg := range args { |
| 77 | if err := injectIntoFile(arg); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | } |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | // injectIntoFile replaces $UNIQUEID$ strings when they exist in the specified file. |
| 85 | func injectIntoFile(filename string) error { |
| 86 | inbytes, err := ioutil.ReadFile(filename) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | // Replace $UNIQUEID$ with generated ids. |
| 92 | re, err := regexp.Compile("[$]UNIQUEID") |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | replaced := re.ReplaceAllFunc(inbytes, func(match []byte) []byte { |
| 97 | id, randErr := uniqueid.Random() |
| 98 | if randErr != nil { |
| 99 | err = randErr |
| 100 | } |
| 101 | return []byte(fmt.Sprintf("%#v", id)) |
| 102 | }) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | // If the file with injections is different, write it to disk. |
| 108 | if !bytes.Equal(inbytes, replaced) { |
| 109 | fmt.Printf("Updated: %s\n", filename) |
| 110 | return ioutil.WriteFile(filename, replaced, 0) |
| 111 | } |
| 112 | return nil |
| 113 | } |