From f514b2dd037effdf5946d2c9deb74c6fd8205bdf Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 16 Feb 2015 18:42:16 +0100 Subject: [PATCH] update Part.Config() to mach spec, it takes a config and returns a config --- cmd/snappy-go/cmd_config.go | 8 +++++++- snappy/config.go | 27 ++++++--------------------- snappy/config_test.go | 26 ++++++++++++++++---------- snappy/parts.go | 7 +++++-- snappy/snapp.go | 6 +++--- snappy/systemimage.go | 6 ++++-- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/cmd/snappy-go/cmd_config.go b/cmd/snappy-go/cmd_config.go index f3102c8ed0..f1bbb7b7ea 100644 --- a/cmd/snappy-go/cmd_config.go +++ b/cmd/snappy-go/cmd_config.go @@ -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 } diff --git a/snappy/config.go b/snappy/config.go index c4e0f04deb..8491448dac 100644 --- a/snappy/config.go +++ b/snappy/config.go @@ -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 } diff --git a/snappy/config_test.go b/snappy/config_test.go index 7a5be559c1..c38bce27ed 100644 --- a/snappy/config_test.go +++ b/snappy/config_test.go @@ -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) } diff --git a/snappy/parts.go b/snappy/parts.go index a5c489daf3..8833bee8b9 100644 --- a/snappy/parts.go +++ b/snappy/parts.go @@ -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 } diff --git a/snappy/snapp.go b/snappy/snapp.go index 41a0c2fcff..7a5b262e7d 100644 --- a/snappy/snapp.go +++ b/snappy/snapp.go @@ -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 diff --git a/snappy/systemimage.go b/snappy/systemimage.go index 2a13a9015f..4e026509f8 100644 --- a/snappy/systemimage.go +++ b/snappy/systemimage.go @@ -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