本文整理匯總了Golang中github.com/juju/juju/state/backups.Metadata.AsJSONBuffer方法的典型用法代碼示例。如果您正苦於以下問題:Golang Metadata.AsJSONBuffer方法的具體用法?Golang Metadata.AsJSONBuffer怎麽用?Golang Metadata.AsJSONBuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/juju/juju/state/backups.Metadata
的用法示例。
在下文中一共展示了Metadata.AsJSONBuffer方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewArchive
// NewArchive returns a new archive file containing the files.
func NewArchive(meta *backups.Metadata, files, dump []File) (*bytes.Buffer, error) {
dirs := set.NewStrings()
var sysFiles []File
for _, file := range files {
var parent string
for _, p := range strings.Split(path.Dir(file.Name), "/") {
if parent == "" {
parent = p
} else {
parent = path.Join(parent, p)
}
if !dirs.Contains(parent) {
sysFiles = append(sysFiles, File{
Name: parent,
IsDir: true,
})
dirs.Add(parent)
}
}
if file.IsDir {
if !dirs.Contains(file.Name) {
sysFiles = append(sysFiles, file)
dirs.Add(file.Name)
}
} else {
sysFiles = append(sysFiles, file)
}
}
var rootFile bytes.Buffer
if err := writeToTar(&rootFile, sysFiles); err != nil {
return nil, errors.Trace(err)
}
topfiles := []File{{
Name: "juju-backup",
IsDir: true,
}}
topfiles = append(topfiles, File{
Name: "juju-backup/dump",
IsDir: true,
})
for _, dumpFile := range dump {
topfiles = append(topfiles, File{
Name: "juju-backup/dump/" + dumpFile.Name,
Content: dumpFile.Content,
IsDir: dumpFile.IsDir,
})
}
topfiles = append(topfiles,
File{
Name: "juju-backup/root.tar",
Content: rootFile.String(),
},
)
if meta != nil {
metaFile, err := meta.AsJSONBuffer()
if err != nil {
return nil, errors.Trace(err)
}
topfiles = append(topfiles,
File{
Name: "juju-backup/metadata.json",
Content: metaFile.(*bytes.Buffer).String(),
},
)
}
var arFile bytes.Buffer
compressed := gzip.NewWriter(&arFile)
defer compressed.Close()
if err := writeToTar(compressed, topfiles); err != nil {
return nil, errors.Trace(err)
}
return &arFile, nil
}