Skip to content

Commit 32ddfff

Browse files
committed
feat: add viper struct fields
1 parent 499862c commit 32ddfff

File tree

6 files changed

+83
-19
lines changed

6 files changed

+83
-19
lines changed

cmd/add.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ var addCmd = &cli.Command{
1111
addReactCmd,
1212
addGoAirCmd,
1313
addPriceUpdateConfigCmd,
14+
addViperTagCmd,
1415
},
1516
}

cmd/add_viper_tag.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/pot-code/web-cli/pkg/core"
7+
"github.com/pot-code/web-cli/pkg/util"
8+
"github.com/urfave/cli/v2"
9+
)
10+
11+
type addViperTagConfig struct {
12+
ConfigPath string `arg:"0" name:"CONFIG_PATH" validate:"required"`
13+
OutName string `name:"out"`
14+
}
15+
16+
var addViperTagCmd = &cli.Command{
17+
Name: "viper",
18+
Aliases: []string{"v"},
19+
Usage: "generate flags registration go file",
20+
ArgsUsage: "CONFIG_PATH",
21+
Flags: []cli.Flag{
22+
// &cli.StringFlag{
23+
// Name: "out",
24+
// Aliases: []string{"o"},
25+
// Usage: "output file name",
26+
// Value: "config_gen.go",
27+
// },
28+
},
29+
Action: func(c *cli.Context) error {
30+
config := new(addViperTagConfig)
31+
err := util.ParseConfig(c, config)
32+
if err != nil {
33+
if _, ok := err.(*util.CommandError); ok {
34+
cli.ShowCommandHelp(c, c.Command.Name)
35+
}
36+
return err
37+
}
38+
39+
cmd, err := addViperFlag(config)
40+
if err != nil {
41+
return err
42+
}
43+
return cmd.Run()
44+
},
45+
}
46+
47+
func addViperFlag(config *addViperTagConfig) (core.Runner, error) {
48+
var outData bytes.Buffer
49+
50+
return util.NewTaskComposer("", &core.FileDesc{
51+
Path: config.ConfigPath,
52+
Overwrite: true,
53+
Data: func() []byte {
54+
return outData.Bytes()
55+
},
56+
}).AddCommand(&core.Command{
57+
Bin: "gomodifytags",
58+
Before: true,
59+
Args: []string{
60+
"-file",
61+
config.ConfigPath,
62+
"-struct",
63+
"AppConfig",
64+
"-add-tags",
65+
"mapstructure,yaml",
66+
},
67+
Out: &outData,
68+
}), nil
69+
}

cmd/gen_flags_visitor.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
ErrUnhandledEt = errors.New("no handler for array element type")
1414
ErrUnhandledField = errors.New("no handler for field type")
1515
ErrUnhandledSelectorPrefix = errors.New("no handler for selector prefix")
16-
ErrUnhandledPflagMapping = errors.New("no handler for pflag mapping")
16+
ErrPflagMappingNotFound = errors.New("no pflag mapping found")
1717
)
1818

1919
type pflagType struct {
@@ -52,7 +52,7 @@ func newConfigStructVisitor(fset *token.FileSet, pkg string) *configStructVisito
5252

5353
func (cv *configStructVisitor) visitField(node ast.Node, prefix []string) {
5454
n := node.(*ast.Field)
55-
comment := n.Comment.Text()
55+
comment := strings.TrimSpace(n.Comment.Text())
5656
fieldName := n.Names[0].String()
5757

5858
tag := n.Tag.Value
@@ -65,8 +65,6 @@ func (cv *configStructVisitor) visitField(node ast.Node, prefix []string) {
6565
panic(err)
6666
}
6767

68-
comment = strings.TrimSpace(comment)
69-
7068
log.WithFields(log.Fields{
7169
"tag_raw": tag,
7270
"tag_meta.Default": ptm.Default,
@@ -115,8 +113,8 @@ func (cv *configStructVisitor) visitIdent(node ast.Node, prefix []string, ft, co
115113
log.WithFields(log.Fields{
116114
"go_type": ft,
117115
"position": cv.fset.Position(n.Pos()),
118-
}).Error(ErrUnhandledPflagMapping)
119-
panic(ErrUnhandledPflagMapping)
116+
}).Error(ErrPflagMappingNotFound)
117+
panic(ErrPflagMappingNotFound)
120118
}
121119
}
122120

pkg/core/command.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"os/exec"
78
"strings"
@@ -15,6 +16,7 @@ type Command struct {
1516
Dir string
1617
Before bool // run before file generation
1718
Args []string
19+
Out io.Writer
1820
}
1921

2022
func (c Command) String() string {
@@ -35,14 +37,18 @@ func NewCmdExecutor(cmd *Command) *CmdExecutor {
3537
}
3638

3739
func (ce CmdExecutor) Run() error {
38-
log.WithField("cmd", ce.cmd).Info("execute command")
3940
cmd := ce.cmd
41+
log.WithFields(log.Fields{"cmd": cmd, "context": "CmdExecutor.Run"}).Info("execute command")
4042
proc := exec.Command(cmd.Bin, cmd.Args...)
4143

42-
if ce.cmd.Dir != "" {
43-
proc.Dir = ce.cmd.Dir
44+
if cmd.Dir != "" {
45+
proc.Dir = cmd.Dir
4446
}
47+
4548
proc.Stdout = os.Stdout
49+
if cmd.Out != nil {
50+
proc.Stdout = cmd.Out
51+
}
4652
proc.Stderr = os.Stdout
4753
return errors.Wrapf(proc.Run(), "failed to execute command '%s'", cmd)
4854
}

pkg/util/composer.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,12 @@ func NewTaskComposer(root string, files ...*core.FileDesc) *TaskComposer {
2525

2626
// AddFile add file task
2727
func (tc *TaskComposer) AddFile(fd *core.FileDesc) *TaskComposer {
28-
log.WithFields(log.Fields{
29-
"caller": "AddFile",
30-
"file": fd.Path,
31-
"overwrite": fd.Overwrite,
32-
}).Debug("call function")
3328
tc.files = append(tc.files, fd)
3429
return tc
3530
}
3631

3732
// AddCommand add command
3833
func (tc *TaskComposer) AddCommand(cmd *core.Command) *TaskComposer {
39-
log.WithFields(log.Fields{
40-
"caller": "AddCommand",
41-
"bin": cmd.Bin,
42-
"cwd": cmd.Dir,
43-
}).Debug("call function")
4434
tc.commands = append(tc.commands, cmd)
4535
return tc
4636
}

0 commit comments

Comments
 (0)