From 22854e5f7191511c750f9355e9218c9bee088e6b Mon Sep 17 00:00:00 2001 From: euforic Date: Thu, 14 Sep 2023 00:28:17 +0000 Subject: [PATCH] add basic cli interface --- cmd/templit/templit.go | 103 +++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ go.sum | 8 ++++ 3 files changed, 114 insertions(+) create mode 100644 cmd/templit/templit.go diff --git a/cmd/templit/templit.go b/cmd/templit/templit.go new file mode 100644 index 0000000..59ee1dd --- /dev/null +++ b/cmd/templit/templit.go @@ -0,0 +1,103 @@ +package main + +import ( + "encoding/json" + "fmt" + "html/template" + "maps" + "os" + + "github.com/euforic/templit" + "github.com/spf13/cobra" +) + +// flagValues stores the values of command-line flags +var flagValues = struct { + token string + branch string +}{} + +// templitCmd represents the templit command +var templitCmd = &cobra.Command{ + Use: "templit ", + Short: "A CLI tool for rendering templates from remote repositories", + Long: `templit is a CLI tool for rendering templates from remote repositories.`, + Run: func(cmd *cobra.Command, args []string) { + if err := cmd.Help(); err != nil { + fmt.Fprintln(os.Stderr, err) + } + }, +} + +// renderCmd represents the render command +var renderCmd = &cobra.Command{ + Use: "render ", + Short: "A CLI tool for rendering templates from remote repositories", + Long: `generate is for rendering templates.`, + Run: func(cmd *cobra.Command, args []string) { + // Check for the correct number of command-line arguments + if len(args) < 3 { + if err := cmd.Help(); err != nil { + fmt.Fprintln(os.Stderr, err) + } + return + } + + if flagValues.token == "" { + flagValues.token = os.Getenv("GIT_TOKEN") + } + + // Extract the command-line arguments + inputPath, outputPath, inputData := args[0], args[1], args[2] + + // values stores the JSON data + var values map[string]interface{} + + // Parse the JSON data from the command-line argument + if err := json.Unmarshal([]byte(inputData), &values); err != nil { + fmt.Fprintf(os.Stderr, "Error parsing JSON data: %s\n", err) + return + } + + // executor is the template executor + executor := templit.NewExecutor(templit.NewDefaultGitClient(flagValues.branch, flagValues.token)) + + // funcMap defines the custom functions that can be used in templates + var funcMap = template.FuncMap{ + // return an error for the embed and import functions if no GitHub token is provided + "embed": func(repoAndPath string, ctx interface{}) (string, error) { + return "", fmt.Errorf("embed function requires a GitHub token") + }, + "import": func(repoAndPath string, destPath string, ctx interface{}) (string, error) { + return "", fmt.Errorf("import function requires a GitHub token") + }, + } + + if flagValues.token != "" { + funcMap["embed"] = executor.EmbedFunc + funcMap["import"] = executor.ImportFunc(outputPath) + } + + // Copy the default function map from the templit package + maps.Copy(funcMap, templit.DefaultFuncMap) + executor.Funcs(funcMap) + + // Process the templates in the input directory and write them to the output directory + if err := executor.WalkAndProcessDir(inputPath, outputPath, values); err != nil { + fmt.Fprintf(os.Stderr, "Error processing template: %s\n", err) + } + }, +} + +func init() { + templitCmd.AddCommand(renderCmd) + renderCmd.Flags().StringVarP(&flagValues.token, "git_token", "t", "", "GitHub token") + renderCmd.Flags().StringVarP(&flagValues.branch, "branch", "b", "main", "GitHub branch") +} + +// main is the entrypoint of the application +func main() { + if err := templitCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + } +} diff --git a/go.mod b/go.mod index 59d9486..3a7b8e0 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.0 require ( github.com/go-git/go-git/v5 v5.8.1 github.com/google/go-cmp v0.5.9 + github.com/spf13/cobra v1.7.0 ) require ( @@ -17,11 +18,13 @@ require ( github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.4.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/skeema/knownhosts v1.2.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect golang.org/x/crypto v0.11.0 // indirect golang.org/x/mod v0.8.0 // indirect diff --git a/go.sum b/go.sum index 8cc10aa..3b8e467 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,7 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -36,6 +37,8 @@ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= @@ -59,11 +62,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM= github.com/skeema/knownhosts v1.2.0/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=