Skip to content

Commit

Permalink
Merge pull request #295 from mvo5/refactor/snap-info-fixes2
Browse files Browse the repository at this point in the history
Refactor/snap info fixes2
  • Loading branch information
niemeyer committed Jan 7, 2016
2 parents b05e56c + 57478ac commit 650834e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
6 changes: 4 additions & 2 deletions snap/clickdeb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ var (
)

func init() {
snap.RegisterBackend([]byte("!<arch>\ndebian"), func(path string) (snap.File, error) {
// we need to wrap "Open()" here because stock Open returns
// a *ClickDeb and not a snap.File
snap.RegisterFormat([]byte("!<arch>\ndebian"), func(path string) (snap.File, error) {
return Open(path)
})
}
Expand Down Expand Up @@ -541,5 +543,5 @@ func (d *ClickDeb) Info() (*snap.Info, error) {
return nil, fmt.Errorf("info failed for %s: %s", d.file.Name(), err)
}

return snap.NewFromPackageYaml(packageYaml)
return snap.InfoFromPackageYaml(packageYaml)
}
18 changes: 9 additions & 9 deletions snap/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ type File interface {
Info() (*Info, error)
}

// Backend implements a specific snap format
type Backend struct {
// backend implements a specific snap format
type snapFormat struct {
magic []byte
open func(fn string) (File, error)
}

var backends []Backend
var formatHandlers []snapFormat

// RegisterBackend registers a snap file backend to the system
func RegisterBackend(magic []byte, open func(fn string) (File, error)) {
backends = append(backends, Backend{magic, open})
// RegisterFormat registers a snap file format to the system
func RegisterFormat(magic []byte, open func(fn string) (File, error)) {
formatHandlers = append(formatHandlers, snapFormat{magic, open})
}

// Open opens a given snap file with the right backend
Expand All @@ -69,9 +69,9 @@ func Open(path string) (File, error) {
return nil, fmt.Errorf("cannot read snap: %v", err)
}

for _, be := range backends {
if bytes.HasPrefix(header, be.magic) {
return be.open(path)
for _, h := range formatHandlers {
if bytes.HasPrefix(header, h.magic) {
return h.open(path)
}
}

Expand Down
4 changes: 2 additions & 2 deletions snap/info_package_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"gopkg.in/yaml.v2"
)

// NewFromPackageYaml creates a new info based on the given packageYaml
func NewFromPackageYaml(yamlData []byte) (*Info, error) {
// InfoFromPackageYaml creates a new info based on the given packageYaml
func InfoFromPackageYaml(yamlData []byte) (*Info, error) {
var s Info
err := yaml.Unmarshal(yamlData, &s)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions snap/info_package_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ type: app
`)

func (s *InfoPackageYamlTestSuite) TestSimple(c *C) {
info, err := NewFromPackageYaml(mockYaml)
info, err := InfoFromPackageYaml(mockYaml)
c.Assert(err, IsNil)
c.Assert(info.Name, Equals, "foo")
c.Assert(info.Version, Equals, "1.0")
c.Assert(info.Type, Equals, TypeApp)
}

func (s *InfoPackageYamlTestSuite) TestFail(c *C) {
_, err := NewFromPackageYaml([]byte("random-crap"))
_, err := InfoFromPackageYaml([]byte("random-crap"))
c.Assert(err, ErrorMatches, "(?m)info failed to parse:.*")
}
4 changes: 2 additions & 2 deletions snap/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func init() {
snap.RegisterBackend([]byte{'h', 's', 'q', 's'}, func(path string) (snap.File, error) {
snap.RegisterFormat([]byte{'h', 's', 'q', 's'}, func(path string) (snap.File, error) {
return New(path), nil
})
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *Snap) Info() (*snap.Info, error) {
return nil, fmt.Errorf("info failed for %s: %s", s.path, err)
}

return snap.NewFromPackageYaml(packageYaml)
return snap.InfoFromPackageYaml(packageYaml)
}

// HashDigest computes a hash digest of the snap file using the given hash.
Expand Down
2 changes: 1 addition & 1 deletion snappy/click.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyyright (C) 2014-2015 Canonical Ltd
* Copyright (C) 2014-2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down

0 comments on commit 650834e

Please sign in to comment.