generators: Extend generators by Run() command

Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
This commit is contained in:
Thomas Hipp 2018-03-08 17:02:25 +01:00
parent b1b53d058f
commit d4c62c74ac
No known key found for this signature in database
GPG Key ID: 993408D1137B7D51
6 changed files with 53 additions and 1 deletions

View File

@ -1,6 +1,13 @@
package main
import "github.com/spf13/cobra"
import (
"fmt"
lxd "github.com/lxc/lxd/shared"
"github.com/spf13/cobra"
"github.com/lxc/distrobuilder/generators"
)
type cmdBuildDir struct {
cmd *cobra.Command
@ -13,6 +20,27 @@ func (c *cmdBuildDir) command() *cobra.Command {
Short: "Build plain rootfs",
Args: cobra.ExactArgs(2),
RunE: c.global.preRunBuild,
PostRunE: func(cmd *cobra.Command, args []string) error {
// Run global generators
for _, file := range c.global.definition.Files {
generator := generators.Get(file.Generator)
if generator == nil {
return fmt.Errorf("Unknown generator '%s'", file.Generator)
}
if len(file.Releases) > 0 && !lxd.StringInSlice(
c.global.definition.Image.Release, file.Releases) {
continue
}
err := generator.Run(c.global.flagCacheDir, c.global.sourceDir, file)
if err != nil {
continue
}
}
return nil
},
}
c.cmd = cmd

View File

@ -23,6 +23,11 @@ func (g DumpGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
return g.dumpFile(filepath.Join(sourceDir, defFile.Path), defFile.Content)
}
// Run dumps content to a file.
func (g DumpGenerator) Run(cacheDir, sourceDir string, defFile shared.DefinitionFile) error {
return g.dumpFile(filepath.Join(sourceDir, defFile.Path), defFile.Content)
}
func (g DumpGenerator) dumpFile(path, content string) error {
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {

View File

@ -16,6 +16,7 @@ import (
type Generator interface {
RunLXC(string, string, *image.LXCImage, shared.DefinitionFile) error
RunLXD(string, string, *image.LXDImage, shared.DefinitionFile) error
Run(string, string, shared.DefinitionFile) error
}
// Get returns a Generator.

View File

@ -72,3 +72,9 @@ func (g HostnameGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImag
return err
}
// Run does nothing.
func (g HostnameGenerator) Run(cacheDir, sourceDir string,
defFile shared.DefinitionFile) error {
return nil
}

View File

@ -74,3 +74,9 @@ func (g HostsGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
return err
}
// Run does nothing.
func (g HostsGenerator) Run(cacheDir, sourceDir string,
defFile shared.DefinitionFile) error {
return nil
}

View File

@ -22,3 +22,9 @@ func (g RemoveGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
defFile shared.DefinitionFile) error {
return os.RemoveAll(filepath.Join(sourceDir, defFile.Path))
}
// Run removes a path.
func (g RemoveGenerator) Run(cacheDir, sourceDir string,
defFile shared.DefinitionFile) error {
return os.RemoveAll(filepath.Join(sourceDir, defFile.Path))
}