Skip to content

Commit

Permalink
[v0.4.0] update CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
evald24 committed Apr 1, 2022
1 parent f2a9b44 commit 4877a2b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
58 changes: 40 additions & 18 deletions cmd/go-gen-config/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/evald24/go-gen-config/internal/generator"
)

Expand All @@ -15,30 +16,51 @@ var (
configPath string
)

func main() {
flag.StringVar(&templatePath, "t", "", "Path to the configuration template file (yaml)")
flag.StringVar(&outputPath, "o", "", "Path to the generated output file (go)")
flag.StringVar(&configPath, "c", "", "Path to the generate config file (yaml)")
flag.Parse()
var RootCmd = &cobra.Command{
Use: "go-gen-config",
Short: "Template-based configuration generator",
Version: "v0.4.0",
Run: run,
}

checkNoEmpty(map[string]string{
"template": templatePath,
"ouput": outputPath,
})
func init() {
RootCmd.PersistentFlags().StringVarP(&templatePath, "template", "t", "", "path to the configuration template file (yaml)")
RootCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "", "path to the generated output file (go)")
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "path to the generate config file (yaml)")

if err := RootCmd.MarkPersistentFlagRequired("template"); err != nil {
fatal(err)
}

if err := RootCmd.MarkPersistentFlagRequired("output"); err != nil {
fatal(err)
}
}

func run(cmd *cobra.Command, args []string) {
if strings.TrimSpace(templatePath) == "" {
fatal("template path is empty")
}

if strings.TrimSpace(outputPath) == "" {
fatal("ouput path is empty")
}

gen := generator.New(templatePath, outputPath, configPath)
if err := gen.Generate(); err != nil {
fmt.Println(err)
return
fatal(err)
}

fmt.Println("Template generation is complete")
}

func checkNoEmpty(paths map[string]string) {
for k, v := range paths {
if strings.TrimSpace(v) == "" {
log.Fatalf("%v path is empty", k)
}
func main() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
}
}

func fatal(v ...interface{}) {
fmt.Println(v...)
os.Exit(1)
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ go 1.17

require (
github.com/iancoleman/strcase v0.2.0
github.com/spf13/cobra v1.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4877a2b

Please sign in to comment.