main.go
package main
import (
"log"
"gcobra/cmd"
)
func main() {
err := cmd.Execute()
if err != nil {
log.Fatalf("cmd.Execute err: %v", err)
}
}
root.go
package cmd
import (
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "",
Short: "",
Long: "",
}
func Execute() error {
return rootCmd.Execute()
}
func init() {
rootCmd.AddCommand(timeCmd)
}
time.go
package cmd
import (
"log"
"time"
"github.com/spf13/cobra"
)
var timeCmd = &cobra.Command{
Use: "time",
Short: "时间格式处理",
Long: "时间格式处理",
Run: func(cmd *cobra.Command, args []string) {},
}
var nowTimeCmd = &cobra.Command{
Use: "now",
Short: "获取当前时间",
Long: "获取当前时间",
Run: func(cmd *cobra.Command, args []string) {
nowTime := time.Now()
log.Printf("输出结果: %s, %d", nowTime.Format("2006-01-02 15:04:05"), nowTime.Unix())
},
}
var duration int64
var calculateTimeCmd = &cobra.Command{
Use: "calc",
Short: "计算所需时间",
Long: "计算所需时间",
Run: func(cmd *cobra.Command, args []string) {
nowTime := time.Now()
log.Printf("输出结果: %s, %d", nowTime.Format("2006-01-02 15:04:05"), nowTime.Local().UnixMilli()-1000*duration)
},
}
func init() {
timeCmd.AddCommand(nowTimeCmd)
timeCmd.AddCommand(calculateTimeCmd)
calculateTimeCmd.Flags().Int64VarP(&duration, "duration", "d", 0, `时间间隔`)
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。