节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
856.00 B
Stardust
//go:build !windows

package command

import (
	"context"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"time"

	"github.com/NewLifeX/Stardust/StarAgent.Native/go/internal/protocol"
)

func defaultSyncTime(target time.Time) error {
	if os.Geteuid() != 0 {
		return errors.New("需要 root 权限")
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var cmd *exec.Cmd
	if runtime.GOOS == "darwin" {
		// BSD date 使用 MMDDhhmm[[CC]YY] 格式。
		cmd = exec.CommandContext(ctx, "date", "-u", target.UTC().Format("010215042006"))
	} else {
		cmd = exec.CommandContext(ctx, "date", "-u", "-s", target.UTC().Format(protocol.TimeLayout))
	}

	output, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("date 执行失败: %w: %s", err, strings.TrimSpace(string(output)))
	}

	return nil
}