func TestFileChangeCacheOneDir(t *testing.T) { callLog := make([]string, 32) //递归可用 kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache")) kmgFile.MustMkdirAll("testFile/d1/d2") kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("1")) MustFileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[3] = "f3" }) kmgTest.Equal(callLog[3], "f3") MustFileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[4] = "f3" }) kmgTest.Equal(callLog[4], "") time.Sleep(time.Second * 1) kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("2")) MustFileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[5] = "f3" }) kmgTest.Equal(callLog[5], "f3") }
func main() { os.Stderr.WriteString("Stderr: start\n") kmgFile.MustMkdirAll("/var/ServiceTester") pid := os.Getpid() logFilePath = filepath.Join("/var/ServiceTester", time.Now().Format(kmgTime.FormatFileName)+"_"+strconv.Itoa(pid)+".log") log("start") go func() { for { time.Sleep(time.Second) log("running") } }() kmgConsole.WaitForExit() log("stop") }
//new file log, will mkdir if dir not exist. // usage: // kmgLog.DefaultLogger = kmgLog.NewFileLogger("log") func NewFileLogWriter(logDir string) LogWriter { kmgFile.MustMkdirAll(logDir) return func(r LogRow) { b, err := r.Marshal() if err != nil { fmt.Println("[fileLoger] logToJson fail", err) return } toWrite := append(b, byte('\n')) err = kmgFile.AppendFile(filepath.Join(logDir, r.Cat+".log"), toWrite) if err != nil { fmt.Println("[fileLoger] logToJson fail", err) return } } }
func (command *GenerateHttpsCert) Execute(context *console.Context) (err error) { wd, err := os.Getwd() if err != nil { return } workDir := filepath.Join(wd, "certs") kmgFile.MustMkdirAll(workDir) os.Chdir(workDir) kmgFile.MustWriteFile("index.txt", []byte("")) kmgFile.MustWriteFile("serial", []byte("01")) kmgFile.MustWriteFile("config.conf", []byte(`[ ca ] default_ca = ca_default [ ca_default ] dir = . certs = . new_certs_dir = . database = ./index.txt serial = ./serial RANDFILE = .rand certificate = ca.crt private_key = ca.key default_days = 36500 default_crl_days = 30 default_md = md5 preserve = no policy = generic_policy [ policy_anything ] countryName = optional stateOrProvinceName = optional localityName = optional organizationName = optional organizationalUnitName = optional commonName = supplied emailAddress = optional`)) mustRunCmd("openssl req -passout pass:1234 -batch -new -x509 -newkey rsa:2048 -extensions v3_ca -keyout ca.key -out ca.crt -days 18250", "-subj", command.subject+" ca") mustRunCmd("openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out csr.csr -days 18250", "-subj", command.subject) mustRunCmdString("openssl ca -config config.conf -batch -cert ca.crt -passin pass:1234 -keyfile ca.key -policy policy_anything -out server.crt -infiles csr.csr") mustRunCmd("openssl req -new -newkey rsa:2048 -nodes -keyout client.key -out csr.csr -days 18250", "-subj", command.subject+" client") mustRunCmdString("openssl ca -config config.conf -batch -cert ca.crt -passin pass:1234 -keyfile ca.key -policy policy_anything -out client.crt -infiles csr.csr") mustRunCmdString("openssl pkcs12 -export -passout pass:1234 -inkey client.key -in client.crt -out client.pfx") return nil }
func runCommand(kmgc *kmgConfig.Env, args []string) { os.Chdir(kmgc.ProjectPath) logDir := filepath.Join(kmgc.LogPath, "run") kmgFile.MustMkdirAll(logDir) thisLogFilePath := filepath.Join(logDir, time.Now().Format(kmgTime.FormatFileName)+".log") kmgFile.MustWriteFile(thisLogFilePath, []byte{}) if !kmgPlatform.GetCompiledPlatform().Compatible(kmgPlatform.WindowsAmd64) { lastLogPath := filepath.Join(logDir, "last.log") if kmgFile.MustFileExist(lastLogPath) { kmgFile.MustSymlink(kmgFile.MustReadSymbolLink(lastLogPath), filepath.Join(logDir, "last2.log")) } kmgFile.MustSymlink(filepath.Base(thisLogFilePath), lastLogPath) } //TODO 大部分命令是 kmg gorun xxx 在这个地方可以直接调用gorun解决问题,这样可以少开一个进程加快了一些速度 // 问题: 上述做法不靠谱,会导致last.log没有用处. //if len(args) >= 2 && args[0] == "kmg" && strings.EqualFold(args[1], "gorun") { // os.Args = append(args[1:], os.Args[1:]...) // goCmd.GoRunCmd() // return //} // 下面的做法不靠谱,会让signle无法传递 //err := kmgCmd.CmdSlice(append(args, os.Args[1:]...)). // SetDir(kmgc.ProjectPath). // RunAndTeeOutputToFile(thisLogFilePath) // TODO bash转义 bashCmd := strings.Join(append(args, os.Args[1:]...), " ") bashCmdStr := bashCmd + " 2>&1 | tee -i " + thisLogFilePath + " ; test ${PIPESTATUS[0]} -eq 0" if kmgPlatform.IsWindows() { err := kmgCmd.CmdString(bashCmdStr).SetDir(kmgc.ProjectPath).StdioRun() if err != nil { err = fmt.Errorf("kmg make: %s", err) kmgConsole.ExitOnErr(err) } return } else { err := kmgCmd.CmdBash(bashCmdStr).SetDir(kmgc.ProjectPath).StdioRun() if err != nil { err = fmt.Errorf("kmg make: %s", err) kmgConsole.ExitOnErr(err) } return } }
func TestFileMd5ChangeCacheOneDir(t *testing.T) { callLog := make([]string, 32) //递归可用 kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache")) kmgFile.MustDelete("testFile/d1") kmgFile.MustMkdirAll("testFile/d1/d2") kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("1")) MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[3] = "f3" }) kmgTest.Equal(callLog[3], "f3") //没有碰过任何东西,缓存有效 MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[4] = "f3" }) kmgTest.Equal(callLog[4], "") //修改文件内容,缓存应该无效 kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("2")) MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[5] = "f3" }) kmgTest.Equal(callLog[5], "f3") //删除文件,缓存应该无效 kmgFile.MustDelete("testFile/d1/d2/f3") MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[6] = "f4" }) kmgTest.Equal(callLog[6], "f4") //添加文件,缓存应该无效 kmgFile.MustWriteFile("testFile/d1/d2/f4", []byte("3")) MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[7] = "f4" }) kmgTest.Equal(callLog[7], "f4") //读取文件,缓存有效 kmgFile.MustReadFile("testFile/d1/d2/f4") MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[8] = "f4" }) kmgTest.Equal(callLog[8], "") //创建目录,缓存有效 kmgFile.MustMkdir("testFile/d1/d2/f5") MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[9] = "f4" }) kmgTest.Equal(callLog[9], "") }