60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
/*
|
|
Copyright © 2024 Shane C. <shane@scaffoe.com>
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"io/fs"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
const templateFile = `{{ define "components/$replace_me" }}
|
|
|
|
{{end}}`
|
|
|
|
// componentCmd represents the component command
|
|
var componentCmd = &cobra.Command{
|
|
Use: "component [name]",
|
|
Short: "Creates a new component",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if !viper.GetBool("dev") {
|
|
fmt.Println("\033[1m\033[38;5;160mError\033[0m | You must be in dev mode to use this command!")
|
|
os.Exit(1)
|
|
}
|
|
|
|
componentName := strings.ToLower(args[0])
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("src/views/components/%s.html", componentName)); err != nil {
|
|
if errors.Is(err, fs.ErrExist) {
|
|
fmt.Println("\033[1m\033[38;5;160mError\033[0m | Component with the same name already exists!")
|
|
os.Exit(1)
|
|
} else if !errors.Is(err, fs.ErrNotExist) {
|
|
fmt.Println("\033[1m\033[38;5;160mError\033[0m | Error getting file information:")
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
templateContent := strings.Replace(templateFile, "$replace_me", componentName, -1)
|
|
|
|
if err := os.WriteFile(fmt.Sprintf("src/views/components/%s.html", componentName), []byte(templateContent), 0644); err != nil {
|
|
fmt.Println("\033[1m\033[38;5;160mError\033[0m | Error writing to file:")
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("\033[1m\033[38;5;35mSuccess\033[0m | Created \033[3m\033[38;5;247mcomponents/%s\033[0m\n", componentName)
|
|
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
makeCmd.AddCommand(componentCmd)
|
|
}
|