/
cli.go
88 lines (75 loc) · 2.82 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
// Package gen contains the implementation of the walkabout code generator.
package gen
import (
"fmt"
"runtime"
"github.com/spf13/cobra"
)
// buildID is set by a linker flag.
var buildID = "dev"
// Main is the entry point for the walkabout tool. It is invoked from
// a main() method in the top-level walkabout package.
func Main() error {
var config config
rootCmd := &cobra.Command{
Use: "walkabout",
Short: `walkabout is a code-generation tool to enhance struct types.
https://github.com/cockroachdb/walkabout`,
Example: `
walkabout InterfaceName
Generates support code to make all struct types that implement
the given interface walkable.
walkabout --union UnionInterface ( InterfaceName | StructName ) ...
Generates an interface called "UnionInterface" which will be
implemented by the named struct types, or those structs that implement
the named interface(s).
walkabout --union UnionInterface --reachable ( InterfaceName | StructName ) ...
As above, but also includes all types in the same package that are
transitively reachable from the named types. This is useful for
refitting an entire package where the existing types may not all
share a common interface.
`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
config.typeNames = args
g, err := newGeneration(config)
if err != nil {
return err
}
return g.Execute()
},
}
rootCmd.Flags().StringVarP(&config.dir, "dir", "d", ".",
"the directory to operate in")
rootCmd.Flags().StringVarP(&config.outFile, "out", "o", "",
"overrides the output file name")
rootCmd.Flags().BoolVarP(&config.reachable, "reachable", "r", false,
`make all transitively reachable types in the same package also
implement the --union interface. Only valid when using --union.`)
rootCmd.Flags().StringVarP(&config.union, "union", "u", "",
`generate a new interface with the given name to be used as the
visitable interface.`)
rootCmd.AddCommand(
&cobra.Command{
Use: "version",
Short: "print version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("walkabout version %s; %s", buildID, runtime.Version())
},
})
return rootCmd.Execute()
}