func (f FileLoggingCmdRunner) getTruncatedOutput(file boshsys.File, truncateLength int64) ([]byte, bool, error) { isTruncated := false stat, err := file.Stat() if err != nil { return nil, false, err } resultSize := truncateLength offset := stat.Size() - truncateLength if offset < 0 { resultSize = stat.Size() offset = 0 } else { isTruncated = true } data := make([]byte, resultSize) _, err = file.ReadAt(data, offset) if err != nil { return nil, false, err } // Do not truncate more than 25% of the data data = f.truncateUntilToken(data, truncateLength/int64(4)) return data, isTruncated, nil }
func (u *DnsUpdater) updateDNSServer(nameToRegister, ip, dnsServer, dnsKey string, ttl int) (err error) { var tmpFile system.File if tmpFile, err = u.fs.TempFile("dnsUpdater-"); err != nil { return } defer u.fs.RemoveAll(tmpFile.Name()) idx := strings.Index(nameToRegister, ".") zone := nameToRegister[idx+1:] configBody := fmt.Sprintf( dnsConfigTemplate, dnsServer, zone, nameToRegister, nameToRegister, ttl, ip, ) if err = u.fs.WriteFileString(tmpFile.Name(), configBody); err != nil { return } _, _, _, err = u.cmdRunner.RunCommand("nsupdate", "-t", "4", "-y", dnsKey, "-v", tmpFile.Name()) return }
. "github.com/cloudfoundry/bosh-utils/logger/file" boshlog "github.com/cloudfoundry/bosh-utils/logger" boshsys "github.com/cloudfoundry/bosh-utils/system" fakeboshsys "github.com/cloudfoundry/bosh-utils/system/fakes" ) func expectedLogFormat(tag, msg string) string { return fmt.Sprintf("\\[%s\\] [0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} %s\n", tag, msg) } var _ = Describe("NewFileLogger", func() { var ( fs *fakeboshsys.FakeFileSystem logFile boshsys.File ) BeforeEach(func() { fs = fakeboshsys.NewFakeFileSystem() var err error logFile, err = fs.TempFile("file-logger-test") Expect(err).ToNot(HaveOccurred()) err = logFile.Close() Expect(err).ToNot(HaveOccurred()) }) AfterEach(func() { logFile.Close() fs.RemoveAll(logFile.Name()) })