This commit adds a `--datadir` override that allows overriding the default search paths for e.g. the custom repositories. In practise only repository overrides are supported because that is the only data stored there.
80 lines
2 KiB
Go
80 lines
2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
osStdout io.Writer = os.Stdout
|
|
osStderr io.Writer = os.Stderr
|
|
)
|
|
|
|
type cmdlineOpts struct {
|
|
dataDir string
|
|
out io.Writer
|
|
}
|
|
|
|
func cmdListImages(cmd *cobra.Command, args []string) error {
|
|
filter, err := cmd.Flags().GetStringArray("filter")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
output, err := cmd.Flags().GetString("output")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dataDir, err := cmd.Flags().GetString("datadir")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
opts := &cmdlineOpts{
|
|
out: osStdout,
|
|
dataDir: dataDir,
|
|
}
|
|
return listImages(output, filter, opts)
|
|
}
|
|
|
|
func run() error {
|
|
// images logs a bunch of stuff to Debug/Info that is distracting
|
|
// the user (at least by default, like what repos being loaded)
|
|
logrus.SetLevel(logrus.WarnLevel)
|
|
|
|
rootCmd := &cobra.Command{
|
|
Use: "image-builder",
|
|
Short: "Build operating system images from a given distro/image-type/blueprint",
|
|
Long: `Build operating system images from a given distribution,
|
|
image-type and blueprint.
|
|
|
|
Image-builder builds operating system images for a range of predefined
|
|
operating sytsems like centos and RHEL with easy customizations support.`,
|
|
SilenceErrors: true,
|
|
}
|
|
rootCmd.PersistentFlags().String("datadir", "", `Override the default data direcotry for e.g. custom repositories/*.json data`)
|
|
rootCmd.SetOut(osStdout)
|
|
rootCmd.SetErr(osStderr)
|
|
|
|
listImagesCmd := &cobra.Command{
|
|
Use: "list-images",
|
|
Short: "List buildable images, use --filter to limit further",
|
|
RunE: cmdListImages,
|
|
SilenceUsage: true,
|
|
}
|
|
listImagesCmd.Flags().StringArray("filter", nil, `Filter distributions by a specific criteria (e.g. "type:rhel*")`)
|
|
listImagesCmd.Flags().String("output", "", "Output in a specific format (text, json)")
|
|
rootCmd.AddCommand(listImagesCmd)
|
|
|
|
return rootCmd.Execute()
|
|
}
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintf(osStderr, "error: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|