Skip to content

Commit

Permalink
Create templates
Browse files Browse the repository at this point in the history
  • Loading branch information
frankywahl committed Mar 15, 2024
1 parent d4c2637 commit 17f9214
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
55 changes: 17 additions & 38 deletions cmd/new.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package cmd

import (
"fmt"
"io"
"os"

"github.com/frankywahl/super_hooks/internal/templates"
"github.com/spf13/cobra"
)

func printFile(dst io.Writer, file string) error {
f, err := templates.Assets.Open(file)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(os.Stdout, f); err != nil {
return err
}
return nil
}

func newCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new",
Expand All @@ -26,13 +40,7 @@ func newBashCmd() *cobra.Command {
Short: "Bash template for hooks command",
Long: "Bash template output to standard out",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf(`#!/usr/bin/env bash
if [ "${1}" == "--about" ]; then
echo "Template description of command"
exit 0
fi
`)
return nil
return printFile(os.Stdout, "bash")
},
}

Expand All @@ -45,36 +53,7 @@ func newGoCmd() *cobra.Command {
Short: "Go template for hooks command",
Long: "Go template output to standard out",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf(`package main
import (
"context"
"flag"
"fmt"
"log"
)
func main() {
var about bool
flag.BoolVar(&about, "about", false, "know about this command")
flag.Parse()
if about {
fmt.Println("Describe command here")
return
}
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
return nil
}
`)
return nil
return printFile(os.Stdout, "go")
},
}

Expand Down
6 changes: 6 additions & 0 deletions internal/templates/bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

if [ "${1}" == "--about" ]; then
echo "Template description of command"
exit 0
fi
28 changes: 28 additions & 0 deletions internal/templates/go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"flag"
"fmt"
"log"
)

func main() {
var about bool
flag.BoolVar(&about, "about", false, "know about this command")
flag.Parse()

if about {
fmt.Println("Template description of command")
return
}

ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}

func run(ctx context.Context) error {
return nil
}
8 changes: 8 additions & 0 deletions internal/templates/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package templates

import (
"embed"
)

//go:embed bash go
var Assets embed.FS

0 comments on commit 17f9214

Please sign in to comment.