72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"complie-erlang/cache"
|
|
"complie-erlang/parser/zm_lib"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type cacheSet struct {
|
|
Path string
|
|
debug bool
|
|
}
|
|
|
|
func (c *cacheSet) Run(cmd *cobra.Command, args []string) {
|
|
if len(args) == 0 {
|
|
fmt.Println("No command specified.")
|
|
return
|
|
}
|
|
|
|
if err := c.run(args); err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Done.")
|
|
|
|
}
|
|
|
|
func (c *cacheSet) run(args []string) error {
|
|
pathByExecutable, err := zm_lib.FindPathByExecutable(c.Path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newCache, err := cache.NewCache(pathByExecutable)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch args[0] {
|
|
case "set":
|
|
return newCache.Set(args[1], args[2])
|
|
case "get":
|
|
val, is := newCache.Get(args[1])
|
|
if is {
|
|
fmt.Printf("%s : %s \n", args[0], val)
|
|
} else {
|
|
fmt.Printf("%s : nil \n", args[0])
|
|
}
|
|
|
|
default:
|
|
return fmt.Errorf("unknown command: %s", args[0])
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
var singleSet = new(cacheSet)
|
|
var logsCmd = &cobra.Command{
|
|
Use: "global",
|
|
Short: "缓存数据模版",
|
|
Long: `构建功能数据`,
|
|
Run: singleSet.Run,
|
|
}
|
|
|
|
logsCmd.PersistentFlags().BoolVar(&singleSet.debug, "debug", false, "是否启动调试模式")
|
|
logsCmd.PersistentFlags().StringVar(&singleSet.Path, "path", globalCacheFileName, "缓存数据存放地址(相对于根目录)")
|
|
|
|
rootCmd.AddCommand(logsCmd)
|
|
}
|