blob: bd98c3274517590f3fee8f07f1d736e154ceea76 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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 Prosnitz7cf836d2015-01-30 09:19:19 -08005// The following enables go generate to generate the doc.go file.
Jiri Simsa32f76fb2015-04-07 15:39:23 -07006//go:generate go run $V23_ROOT/release/go/src/v.io/x/lib/cmdline/testdata/gendoc.go .
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -08007
8package main
9
10import (
11 "bytes"
12 "fmt"
13 "io/ioutil"
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080014 "regexp"
15
Jiri Simsa6ac95222015-02-23 16:11:49 -080016 "v.io/v23/uniqueid"
Todd Wang9560b9c2015-05-11 13:27:58 -070017 "v.io/x/lib/cmdline"
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080018)
19
20func main() {
Todd Wang9560b9c2015-05-11 13:27:58 -070021 cmdline.Main(cmdUniqueId)
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080022}
23
Todd Wang9560b9c2015-05-11 13:27:58 -070024var cmdUniqueId = &cmdline.Command{
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080025 Name: "uniqueid",
Todd Wang6ed3b6c2015-04-08 14:37:04 -070026 Short: "generates unique identifiers",
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080027 Long: `
Todd Wang6ed3b6c2015-04-08 14:37:04 -070028Command uniqueid generates unique identifiers.
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080029It also has an option of automatically substituting unique ids with placeholders in files.
30`,
Todd Wang9560b9c2015-05-11 13:27:58 -070031 Children: []*cmdline.Command{cmdGenerate, cmdInject},
32 Topics: []cmdline.Topic{},
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080033}
34
Todd Wang9560b9c2015-05-11 13:27:58 -070035var cmdGenerate = &cmdline.Command{
36 Runner: cmdline.RunnerFunc(runGenerate),
Todd Wangf1550cf2015-05-11 10:58:41 -070037 Name: "generate",
38 Short: "Generates UniqueIds",
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080039 Long: `
40Generates unique ids and outputs them to standard out.
41`,
42 ArgsName: "",
43 ArgsLong: "",
44}
45
Todd Wang9560b9c2015-05-11 13:27:58 -070046var cmdInject = &cmdline.Command{
47 Runner: cmdline.RunnerFunc(runInject),
Todd Wangf1550cf2015-05-11 10:58:41 -070048 Name: "inject",
49 Short: "Injects UniqueIds into existing files",
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080050 Long: `
51Injects UniqueIds into existing files.
52Strings 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 Wang9560b9c2015-05-11 13:27:58 -070059func runGenerate(env *cmdline.Env, args []string) error {
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080060 if len(args) > 0 {
Todd Wangf1550cf2015-05-11 10:58:41 -070061 return env.UsageErrorf("expected 0 args, got %d", len(args))
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080062 }
63 id, err := uniqueid.Random()
64 if err != nil {
65 return err
66 }
Suharsh Sivakumar13f88272015-02-20 17:41:26 -080067 fmt.Printf("%#v\n", id)
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080068 return nil
69}
70
71// runInject implements the inject command which replaces $UNIQUEID$ strings with generated ids.
Todd Wang9560b9c2015-05-11 13:27:58 -070072func runInject(env *cmdline.Env, args []string) error {
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080073 if len(args) == 0 {
Todd Wangf1550cf2015-05-11 10:58:41 -070074 return env.UsageErrorf("expected at least one file arg, got 0")
Benjamin Prosnitz7cf836d2015-01-30 09:19:19 -080075 }
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.
85func 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}