🎉 start the project
This commit is contained in:
22
internal/cmd/cmd.go
Normal file
22
internal/cmd/cmd.go
Normal file
@ -0,0 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
root *cobra.Command
|
||||
)
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
root = rootCmd()
|
||||
}
|
||||
|
||||
func Execute(ctx context.Context) {
|
||||
if err := root.ExecuteContext(ctx); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
41
internal/cmd/root.go
Normal file
41
internal/cmd/root.go
Normal file
@ -0,0 +1,41 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"loveuer/utodo/internal/opt"
|
||||
|
||||
"gitea.loveuer.com/yizhisec/packages/logger"
|
||||
"gitea.loveuer.com/yizhisec/packages/tool"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func rootCmd(subCmds ...*cobra.Command) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "utodo",
|
||||
Short: "utodo is a todo web/pwa application",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Printf("\\/ UTodo %s\n", opt.Version)
|
||||
if opt.Cfg.Debug {
|
||||
logger.SetLogLevel(logger.LogLevelDebug)
|
||||
tool.TablePrinter(opt.Cfg)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
RunE: rootRun,
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
|
||||
|
||||
cmd.AddCommand(subCmds...)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func rootRun(cmd *cobra.Command, args []string) error {
|
||||
|
||||
<-cmd.Context().Done()
|
||||
logger.WarnCtx(cmd.Context(), "received quit signal, exiting...")
|
||||
|
||||
return nil
|
||||
}
|
7
internal/opt/opt.go
Normal file
7
internal/opt/opt.go
Normal file
@ -0,0 +1,7 @@
|
||||
package opt
|
||||
|
||||
type config struct {
|
||||
Debug bool
|
||||
}
|
||||
|
||||
var Cfg = &config{}
|
5
internal/opt/var.go
Normal file
5
internal/opt/var.go
Normal file
@ -0,0 +1,5 @@
|
||||
package opt
|
||||
|
||||
var (
|
||||
Version = "<UNKNOWN>"
|
||||
)
|
Reference in New Issue
Block a user