/
structI.go
49 lines (41 loc) · 1.19 KB
/
structI.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
package main
import (
"fmt"
"path"
"strconv"
"github.com/rigelrozanski/common/parse"
"github.com/spf13/cobra"
)
var createInterfaceMirroringStruct = &cobra.Command{
Use: "create-interface-mirroring-struct [source-file] [lineno] <interface-name>",
Short: "create a struct which fulfilles an interface",
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
srcFile := args[0]
lineNo, err := strconv.Atoi(args[1])
if err != nil {
fmt.Printf("obad line number: %v", err)
return nil
}
interName := ""
if len(args) == 3 {
interName = args[2]
}
// get the function name
strct, _, found := parse.GetCurrentParsedStruct(srcFile, lineNo)
if !found {
fmt.Printf("o%v", "cursor not within a struct")
return nil
}
if interName == "" {
interName = capitalizedWord(strct.Name) + "I"
}
// get all the possible functions
pfns := parse.GetAllFuncsOfStruct(path.Dir(srcFile), strct.Name)
interCode := parse.InterfaceCodeFromFuncs(interName, pfns, "\r")
// write the interface code
fmt.Printf("%vggO", strct.CommentStartLine)
fmt.Printf("// %v mirrors the struct %v (DNETL)\r%v", interName, strct.Name, interCode)
return nil
},
}