cmd: wire --input flag into client

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-01-14 15:19:39 -08:00
parent 9338d10a04
commit 16fd14b0c9
2 changed files with 34 additions and 21 deletions

View File

@ -27,8 +27,10 @@ var computeCmd = &cobra.Command{
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(appcontext.Context()) ctx := lg.WithContext(appcontext.Context())
// FIXME: boot and bootdir should be config fields, not args c, err := dagger.NewClient(ctx, dagger.ClientConfig{
c, err := dagger.NewClient(ctx, "", "", args[0]) Input: viper.GetString("input"),
BootDir: args[0],
})
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("unable to create client") lg.Fatal().Err(err).Msg("unable to create client")
} }
@ -43,3 +45,11 @@ var computeCmd = &cobra.Command{
fmt.Println(output.JSON()) fmt.Println(output.JSON())
}, },
} }
func init() {
computeCmd.Flags().String("input", "", "Input overlay")
if err := viper.BindPFlags(computeCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -50,34 +50,37 @@ type Client struct {
c *bk.Client c *bk.Client
localdirs map[string]string localdirs map[string]string
boot string cfg ClientConfig
bootdir string
input string
} }
func NewClient(ctx context.Context, host, boot, bootdir string) (*Client, error) { type ClientConfig struct {
Host string
Boot string
BootDir string
Input string
}
func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
// buildkit client // buildkit client
if host == "" { if cfg.Host == "" {
host = os.Getenv("BUILDKIT_HOST") cfg.Host = os.Getenv("BUILDKIT_HOST")
} }
if host == "" { if cfg.Host == "" {
host = defaultBuildkitHost cfg.Host = defaultBuildkitHost
} }
if boot == "" { if cfg.Boot == "" {
boot = defaultBootScript cfg.Boot = defaultBootScript
} }
if bootdir == "" { if cfg.BootDir == "" {
bootdir = defaultBootDir cfg.BootDir = defaultBootDir
} }
c, err := bk.New(ctx, host) c, err := bk.New(ctx, cfg.Host)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "buildkit client") return nil, errors.Wrap(err, "buildkit client")
} }
return &Client{ return &Client{
c: c, c: c,
boot: boot, cfg: cfg,
bootdir: bootdir,
input: `{}`,
localdirs: map[string]string{}, localdirs: map[string]string{},
}, nil }, nil
} }
@ -92,11 +95,11 @@ func (c *Client) LocalDirs(ctx context.Context) ([]string, error) {
func (c *Client) BootScript() (*Script, error) { func (c *Client) BootScript() (*Script, error) {
cc := &Compiler{} cc := &Compiler{}
src, err := cc.Compile("boot.cue", c.boot) src, err := cc.Compile("boot.cue", c.cfg.Boot)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "compile") return nil, errors.Wrap(err, "compile")
} }
src, err = src.MergeTarget(c.bootdir, "bootdir") src, err = src.MergeTarget(c.cfg.BootDir, "bootdir")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -175,7 +178,7 @@ func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.Writ
// Setup solve options // Setup solve options
opts := bk.SolveOpt{ opts := bk.SolveOpt{
FrontendAttrs: map[string]string{ FrontendAttrs: map[string]string{
bkInputKey: c.input, bkInputKey: c.cfg.Input,
bkBootKey: string(bootSource), bkBootKey: string(bootSource),
}, },
LocalDirs: map[string]string{}, LocalDirs: map[string]string{},