Added new component make command, cleaned up cli code

This commit is contained in:
Shane C 2024-04-23 15:40:26 -04:00
parent 0645ae3cea
commit 92d4f00d30
No known key found for this signature in database
GPG key ID: 565F3A1C80F8AA9D
6 changed files with 85 additions and 79 deletions

60
cmd/component.go Normal file
View file

@ -0,0 +1,60 @@
/*
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)
}

18
cmd/make.go Normal file
View file

@ -0,0 +1,18 @@
/*
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
package cmd
import (
"github.com/spf13/cobra"
)
// makeCmd represents the make command
var makeCmd = &cobra.Command{
Use: "make",
Short: "Makes things.",
}
func init() {
rootCmd.AddCommand(makeCmd)
}

View file

@ -1,39 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// make:routeCmd represents the make:route command
var makeRouteCmd = &cobra.Command{
Use: "make:route",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("make:route called")
},
}
func init() {
rootCmd.AddCommand(makeRouteCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// make:routeCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// make:routeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -1,5 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
package cmd
@ -16,16 +16,7 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "shadow",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Short: "A blogging platform",
}
// Execute adds all child commands to the root command and sets flags appropriately.
@ -40,15 +31,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.shadow.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.

View file

@ -1,5 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
package cmd
@ -24,13 +24,7 @@ import (
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Runs the main service",
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("dev") && !fiber.IsChild() {
@ -83,14 +77,4 @@ to quickly create a Cobra application.`,
func init() {
rootCmd.AddCommand(runCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// runCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

6
go.mod
View file

@ -12,9 +12,9 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.2
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/gofiber/fiber/v2 v2.52.4 // indirect
github.com/gofiber/fiber/v2 v2.52.4
github.com/gofiber/template v1.8.3 // indirect
github.com/gofiber/template/html/v2 v2.1.1 // indirect
github.com/gofiber/utils v1.1.0 // indirect
@ -43,7 +43,7 @@ require (
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect