update Part.Config() to mach spec, it takes a config and returns a config

This commit is contained in:
Michael Vogt
2015-02-16 18:42:16 +01:00
parent d41d0605a7
commit f514b2dd03
6 changed files with 41 additions and 39 deletions

View File

@@ -43,5 +43,11 @@ func (x *cmdConfig) Execute(args []string) (err error) {
if snap == nil {
return fmt.Errorf("No snap: '%s' found", pkgname)
}
return snap.Config(input)
newConfig, err := snap.Config(input)
if err != nil {
return err
}
fmt.Println(newConfig)
return nil
}

View File

@@ -6,41 +6,26 @@ import (
"os/exec"
"path/filepath"
"strings"
"github.com/mvo5/goconfigparser"
)
func snapConfig(snapDir, rawConfig string) error {
func snapConfig(snapDir, rawConfig string) (newConfig string, err error) {
configScript := filepath.Join(snapDir, "hooks", "config")
cmd := exec.Command(configScript)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
return "", err
}
// meh, really golang?
go func() {
defer stdin.Close()
io.Copy(stdin, strings.NewReader(rawConfig))
}()
output, err := cmd.Output()
output, err := cmd.CombinedOutput()
if err != nil {
return err
return "", fmt.Errorf("config failed with: '%s'", output)
}
// check he output,
// FIXME: goyaml does fail for me for the output "ok: true" :(
cfg := goconfigparser.New()
cfg.AllowNoSectionHeader = true
if err := cfg.ReadString(string(output)); err != nil {
return err
}
v, err := cfg.Get("", "ok")
// FIXME: more clever bool conversion
if err != nil || v != "true" {
errorStr, _ := cfg.Get("", "error")
return fmt.Errorf("config: '%s' failed with: '%s'", configScript, errorStr)
}
return nil
return string(output), nil
}

View File

@@ -12,17 +12,20 @@ import (
const configPassthroughScript = `#!/bin/sh
# just dump out for the tes
cat - > %s/config.out
# temp location to store cfg
CFG="%s/config.out"
# return resul
printf "ok: true\n"
# just dump out for the tes
cat - > $CFG
# and config
cat $CFG
`
const configErrorScript = `#!/bin/sh -ex
const configErrorScript = `#!/bin/sh
printf "ok: false\n"
printf "error: some error\n"
printf "error: some error"
exit 1
`
const configYaml = `
@@ -48,18 +51,21 @@ func (s *SnapTestSuite) TestConfigSimple(c *C) {
snapDir, err := s.makeMockSnapWithConfig(c, mockConfig)
c.Assert(err, IsNil)
err = snapConfig(snapDir, configYaml)
newConfig, err := snapConfig(snapDir, configYaml)
c.Assert(err, IsNil)
content, err := ioutil.ReadFile(filepath.Join(s.tempdir, "config.out"))
c.Assert(err, IsNil)
c.Assert(content, DeepEquals, []byte(configYaml))
c.Assert(newConfig, Equals, configYaml)
}
func (s *SnapTestSuite) TestConfigError(c *C) {
snapDir, err := s.makeMockSnapWithConfig(c, configErrorScript)
c.Assert(err, IsNil)
err = snapConfig(snapDir, configYaml)
newConfig, err := snapConfig(snapDir, configYaml)
c.Assert(err, NotNil)
c.Assert(strings.HasSuffix(err.Error(), "failed with: 'some error'"), Equals, true)
c.Assert(newConfig, Equals, "")
fmt.Println(err)
c.Assert(strings.HasSuffix(err.Error(), "failed with: 'error: some error'"), Equals, true)
}

View File

@@ -39,10 +39,13 @@ type Part interface {
InstalledSize() int
DownloadSize() int
// Action
// Install the snap
Install(pb ProgressMeter) error
// Uninstall the snap
Uninstall() error
Config(configuration []byte) error
// Config takes a yaml configuration and returns the full snap
// config with the changes. Note that "configuration" may be empty.
Config(configuration []byte) (newConfig string, err error)
// make a inactive part active
SetActive() error
}

View File

@@ -165,7 +165,7 @@ func (s *SnapPart) Uninstall() (err error) {
}
// Config is used to to configure the snap
func (s *SnapPart) Config(configuration []byte) (err error) {
func (s *SnapPart) Config(configuration []byte) (new string, err error) {
return snapConfig(s.basedir, string(configuration))
}
@@ -335,8 +335,8 @@ func (s *RemoteSnapPart) Uninstall() (err error) {
}
// Config is used to to configure the snap
func (s *RemoteSnapPart) Config(configuration []byte) (err error) {
return err
func (s *RemoteSnapPart) Config(configuration []byte) (new string, err error) {
return "", err
}
// NeedsReboot returns true if the snap becomes active on the next reboot

View File

@@ -176,8 +176,10 @@ func (s *SystemImagePart) Uninstall() (err error) {
}
// Config is used to to configure the snap
func (s *SystemImagePart) Config(configuration []byte) (err error) {
return err
func (s *SystemImagePart) Config(configuration []byte) (new string, err error) {
// FIXME: do something special here and call
// ubuntu-core-config
return "", err
}
// NeedsReboot returns true if the snap becomes active on the next reboot