2023-05-23 22:26:14 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-22 06:57:34 -04:00
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
"github.com/InazumaV/V2bX/common/systime"
|
2023-05-23 22:26:14 -04:00
|
|
|
"github.com/beevik/ntp"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ntpServer string
|
|
|
|
|
|
|
|
var commandSyncTime = &cobra.Command{
|
|
|
|
Use: "synctime",
|
|
|
|
Short: "Sync time from ntp server",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: synctimeHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
commandSyncTime.Flags().StringVar(&ntpServer, "server", "time.apple.com", "ntp server")
|
|
|
|
command.AddCommand(commandSyncTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func synctimeHandle(_ *cobra.Command, _ []string) {
|
|
|
|
t, err := ntp.Time(ntpServer)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(Err("get time from server error: ", err))
|
|
|
|
fmt.Println(Err("同步时间失败"))
|
|
|
|
return
|
|
|
|
}
|
2023-07-22 06:57:34 -04:00
|
|
|
err = systime.SetSystemTime(t)
|
2023-05-23 22:26:14 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(Err("set system time error: ", err))
|
|
|
|
fmt.Println(Err("同步时间失败"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(Ok("同步时间成功"))
|
|
|
|
}
|