From 92d4f00d30ff0f6437bf10556f8af97d80dc0f89 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 23 Apr 2024 15:40:26 -0400 Subject: [PATCH] Added new component make command, cleaned up cli code --- cmd/component.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/make.go | 18 ++++++++++++++ cmd/make:route.go | 39 ------------------------------ cmd/root.go | 21 ++--------------- cmd/run.go | 20 ++-------------- go.mod | 6 ++--- 6 files changed, 85 insertions(+), 79 deletions(-) create mode 100644 cmd/component.go create mode 100644 cmd/make.go delete mode 100644 cmd/make:route.go diff --git a/cmd/component.go b/cmd/component.go new file mode 100644 index 0000000..af7f1e0 --- /dev/null +++ b/cmd/component.go @@ -0,0 +1,60 @@ +/* +Copyright © 2024 Shane C. +*/ +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) +} diff --git a/cmd/make.go b/cmd/make.go new file mode 100644 index 0000000..ed14ed7 --- /dev/null +++ b/cmd/make.go @@ -0,0 +1,18 @@ +/* +Copyright © 2024 Shane C. +*/ +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) +} diff --git a/cmd/make:route.go b/cmd/make:route.go deleted file mode 100644 index caf49b1..0000000 --- a/cmd/make:route.go +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright © 2024 NAME HERE -*/ -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") -} diff --git a/cmd/root.go b/cmd/root.go index e290dec..ca1efa7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,5 +1,5 @@ /* -Copyright © 2024 NAME HERE +Copyright © 2024 Shane C. */ 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. diff --git a/cmd/run.go b/cmd/run.go index d6b35c0..578a669 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -1,5 +1,5 @@ /* -Copyright © 2024 NAME HERE +Copyright © 2024 Shane C. */ 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") } diff --git a/go.mod b/go.mod index c88e602..5ab40bc 100644 --- a/go.mod +++ b/go.mod @@ -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