本文整理汇总了Golang中github.com/snapcore/snapd/snap.Open函数的典型用法代码示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: installLocalTestSnap
func (ms *mgrsSuite) installLocalTestSnap(c *C, snapYamlContent string) *snap.Info {
st := ms.o.State()
snapPath := makeTestSnap(c, snapYamlContent)
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, IsNil)
// store current state
snapName := info.Name()
var snapst snapstate.SnapState
snapstate.Get(st, snapName, &snapst)
ts, err := snapstate.InstallPath(st, &snap.SideInfo{RealName: snapName}, snapPath, "", snapstate.Flags{DevMode: true})
c.Assert(err, IsNil)
chg := st.NewChange("install-snap", "...")
chg.AddAll(ts)
st.Unlock()
err = ms.o.Settle()
st.Lock()
c.Assert(err, IsNil)
c.Assert(chg.Err(), IsNil)
c.Assert(chg.Status(), Equals, state.DoneStatus, Commentf("install-snap change failed with: %v", chg.Err()))
return info
}
示例2: tryDirect
func tryDirect(w io.Writer, path string, verbose bool) bool {
path = norm(path)
snapf, err := snap.Open(path)
if err != nil {
return false
}
info, err := snap.ReadInfoFromSnapFile(snapf, nil)
if err != nil {
return false
}
fmt.Fprintf(w, "path:\t%q\n", path)
fmt.Fprintf(w, "name:\t%s\n", info.Name())
fmt.Fprintf(w, "summary:\t%q\n", info.Summary())
var notes *Notes
if verbose {
fmt.Fprintln(w, "notes:\t")
fmt.Fprintf(w, " confinement:\t%s\n", info.Confinement)
if info.Broken == "" {
fmt.Fprintln(w, " broken:\tfalse")
} else {
fmt.Fprintf(w, " broken:\ttrue (%s)\n", info.Broken)
}
} else {
notes = NotesFromInfo(info)
}
fmt.Fprintf(w, "version:\t%s %s\n", info.Version, notes)
maybePrintType(w, string(info.Type))
return true
}
示例3: TestExtractKernelAssetsNoUnpacksKernelForGrub
func (s *kernelOSSuite) TestExtractKernelAssetsNoUnpacksKernelForGrub(c *C) {
// pretend to be a grub system
mockGrub := boottest.NewMockBootloader("grub", c.MkDir())
partition.ForceBootloader(mockGrub)
files := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{"meta/kernel.yaml", "version: 4.2"},
}
si := &snap.SideInfo{
RealName: "ubuntu-kernel",
Revision: snap.R(42),
}
fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
snapf, err := snap.Open(fn)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, si)
c.Assert(err, IsNil)
err = boot.ExtractKernelAssets(info, snapf)
c.Assert(err, IsNil)
// kernel is *not* here
kernimg := filepath.Join(mockGrub.Dir(), "ubuntu-kernel_42.snap", "kernel.img")
c.Assert(osutil.FileExists(kernimg), Equals, false)
}
示例4: unsafeReadSnapInfoImpl
func unsafeReadSnapInfoImpl(snapPath string) (*snap.Info, error) {
// Condider using DeriveSideInfo before falling back to this!
snapf, err := snap.Open(snapPath)
if err != nil {
return nil, err
}
return snap.ReadInfoFromSnapFile(snapf, nil)
}
示例5: TestReadInfoFromSnapFileCatchesInvalidImplicitHook
func (s *infoSuite) TestReadInfoFromSnapFileCatchesInvalidImplicitHook(c *C) {
yaml := `name: foo
version: 1.0`
snapPath := snaptest.MakeTestSnapWithFiles(c, yaml, emptyHooks("123abc"))
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
_, err = snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, ErrorMatches, ".*invalid hook name.*")
}
示例6: extractKernelAssets
func extractKernelAssets(snapPath string, info *snap.Info) error {
snapf, err := snap.Open(snapPath)
if err != nil {
return err
}
if err := boot.ExtractKernelAssets(info, snapf); err != nil {
return err
}
return nil
}
示例7: TestReadInfoFromSnapFileValidates
func (s *infoSuite) TestReadInfoFromSnapFileValidates(c *C) {
yaml := `name: foo.bar
version: 1.0
type: app`
snapPath := makeTestSnap(c, yaml)
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
_, err = snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, ErrorMatches, "invalid snap name.*")
}
示例8: TestFileOpenForSnapDir
func (s *FileSuite) TestFileOpenForSnapDir(c *C) {
sd := c.MkDir()
snapYaml := filepath.Join(sd, "meta", "snap.yaml")
err := os.MkdirAll(filepath.Dir(snapYaml), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(snapYaml, []byte(`name: foo`), 0644)
c.Assert(err, IsNil)
f, err := snap.Open(sd)
c.Assert(err, IsNil)
c.Assert(f, FitsTypeOf, &snapdir.SnapDir{})
}
示例9: TestReadInfoFromSnapFileCatchesInvalidConfinement
func (s *infoSuite) TestReadInfoFromSnapFileCatchesInvalidConfinement(c *C) {
yaml := `name: foo
version: 1.0
confinement: foo`
snapPath := makeTestSnap(c, yaml)
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
_, err = snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, ErrorMatches, ".*invalid confinement type.*")
}
示例10: OpenSnapFile
// OpenSnapFile opens a snap blob returning both a snap.Info completed
// with sideInfo (if not nil) and a corresponding snap.Container.
// Assumes the file was verified beforehand or the user asked for --dangerous.
func OpenSnapFile(snapPath string, sideInfo *snap.SideInfo) (*snap.Info, snap.Container, error) {
snapf, err := snap.Open(snapPath)
if err != nil {
return nil, nil, err
}
info, err := snap.ReadInfoFromSnapFile(snapf, sideInfo)
if err != nil {
return nil, nil, err
}
return info, snapf, nil
}
示例11: TestReadInfoFromSnapFileCatchesInvalidHook
func (s *infoSuite) TestReadInfoFromSnapFileCatchesInvalidHook(c *C) {
yaml := `name: foo
version: 1.0
hooks:
123abc:`
snapPath := makeTestSnap(c, yaml)
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
_, err = snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, ErrorMatches, ".*invalid hook name.*")
}
示例12: serveSnap
func (ms *mgrsSuite) serveSnap(snapPath, revno string) {
snapf, err := snap.Open(snapPath)
if err != nil {
panic(err)
}
info, err := snap.ReadInfoFromSnapFile(snapf, nil)
if err != nil {
panic(err)
}
name := info.Name()
ms.serveIDtoName[fakeSnapID(name)] = name
ms.serveSnapPath[name] = snapPath
ms.serveRevision[name] = revno
}
示例13: snapEssentialInfo
func snapEssentialInfo(w http.ResponseWriter, fn, snapID string, bs asserts.Backstore) (*essentialInfo, error) {
snapFile, err := snap.Open(fn)
if err != nil {
http.Error(w, fmt.Sprintf("cannot read: %v: %v", fn, err), http.StatusBadRequest)
return nil, errInfo
}
info, err := snap.ReadInfoFromSnapFile(snapFile, nil)
if err != nil {
http.Error(w, fmt.Sprintf("can get info for: %v: %v", fn, err), http.StatusBadRequest)
return nil, errInfo
}
snapDigest, size, err := asserts.SnapFileSHA3_384(fn)
if err != nil {
http.Error(w, fmt.Sprintf("can get digest for: %v: %v", fn, err), http.StatusBadRequest)
return nil, errInfo
}
snapRev, devAcct, err := findSnapRevision(snapDigest, bs)
if err != nil && err != asserts.ErrNotFound {
http.Error(w, fmt.Sprintf("can get info for: %v: %v", fn, err), http.StatusBadRequest)
return nil, errInfo
}
var devel, develID string
var revision int
if snapRev != nil {
snapID = snapRev.SnapID()
develID = snapRev.DeveloperID()
devel = devAcct.Username()
revision = snapRev.SnapRevision()
} else {
// XXX: fallback until we are always assertion based
develID = defaultDeveloperID
devel = defaultDeveloper
revision = makeRevision(info)
}
return &essentialInfo{
Name: info.Name(),
SnapID: snapID,
DeveloperID: develID,
DevelName: devel,
Revision: revision,
Version: info.Version,
Digest: snapDigest,
Size: size,
}, nil
}
示例14: TestExtractKernelAssetsAndRemove
func (s *kernelOSSuite) TestExtractKernelAssetsAndRemove(c *C) {
files := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{"dtbs/foo.dtb", "g'day, I'm foo.dtb"},
{"dtbs/bar.dtb", "hello, I'm bar.dtb"},
// must be last
{"meta/kernel.yaml", "version: 4.2"},
}
si := &snap.SideInfo{
RealName: "ubuntu-kernel",
Revision: snap.R(42),
}
fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
snapf, err := snap.Open(fn)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, si)
c.Assert(err, IsNil)
err = boot.ExtractKernelAssets(info, snapf)
c.Assert(err, IsNil)
// this is where the kernel/initrd is unpacked
bootdir := s.bootloader.Dir()
kernelAssetsDir := filepath.Join(bootdir, "ubuntu-kernel_42.snap")
for _, def := range files {
if def[0] == "meta/kernel.yaml" {
break
}
fullFn := filepath.Join(kernelAssetsDir, def[0])
content, err := ioutil.ReadFile(fullFn)
c.Assert(err, IsNil)
c.Assert(string(content), Equals, def[1])
}
// remove
err = boot.RemoveKernelAssets(info)
c.Assert(err, IsNil)
c.Check(osutil.FileExists(kernelAssetsDir), Equals, false)
}
示例15: TestReadInfoFromSnapFileMissingEpoch
func (s *infoSuite) TestReadInfoFromSnapFileMissingEpoch(c *C) {
yaml := `name: foo
version: 1.0
type: app`
snapPath := snaptest.MakeTestSnapWithFiles(c, yaml, nil)
snapf, err := snap.Open(snapPath)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, nil)
c.Assert(err, IsNil)
c.Check(info.Name(), Equals, "foo")
c.Check(info.Version, Equals, "1.0")
c.Check(info.Type, Equals, snap.TypeApp)
c.Check(info.Revision, Equals, snap.R(0))
c.Check(info.Epoch, Equals, "0") // Defaults to 0
}