Merge pull request #29 from blocklayerhq/wire-input

cmd: wire --input flag into client
This commit is contained in:
Andrea Luzzardi 2021-01-14 15:28:10 -08:00 committed by GitHub
commit 3bb3769bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 21 deletions

View File

@ -27,8 +27,10 @@ var computeCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(appcontext.Context())
// FIXME: boot and bootdir should be config fields, not args
c, err := dagger.NewClient(ctx, "", "", args[0])
c, err := dagger.NewClient(ctx, dagger.ClientConfig{
Input: viper.GetString("input"),
BootDir: args[0],
})
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
@ -43,3 +45,11 @@ var computeCmd = &cobra.Command{
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
localdirs map[string]string
boot string
bootdir string
input string
cfg ClientConfig
}
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
if host == "" {
host = os.Getenv("BUILDKIT_HOST")
if cfg.Host == "" {
cfg.Host = os.Getenv("BUILDKIT_HOST")
}
if host == "" {
host = defaultBuildkitHost
if cfg.Host == "" {
cfg.Host = defaultBuildkitHost
}
if boot == "" {
boot = defaultBootScript
if cfg.Boot == "" {
cfg.Boot = defaultBootScript
}
if bootdir == "" {
bootdir = defaultBootDir
if cfg.BootDir == "" {
cfg.BootDir = defaultBootDir
}
c, err := bk.New(ctx, host)
c, err := bk.New(ctx, cfg.Host)
if err != nil {
return nil, errors.Wrap(err, "buildkit client")
}
return &Client{
c: c,
boot: boot,
bootdir: bootdir,
input: `{}`,
cfg: cfg,
localdirs: map[string]string{},
}, nil
}
@ -92,11 +95,11 @@ func (c *Client) LocalDirs(ctx context.Context) ([]string, error) {
func (c *Client) BootScript() (*Script, error) {
cc := &Compiler{}
src, err := cc.Compile("boot.cue", c.boot)
src, err := cc.Compile("boot.cue", c.cfg.Boot)
if err != nil {
return nil, errors.Wrap(err, "compile")
}
src, err = src.MergeTarget(c.bootdir, "bootdir")
src, err = src.MergeTarget(c.cfg.BootDir, "bootdir")
if err != nil {
return nil, err
}
@ -175,7 +178,7 @@ func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.Writ
// Setup solve options
opts := bk.SolveOpt{
FrontendAttrs: map[string]string{
bkInputKey: c.input,
bkInputKey: c.cfg.Input,
bkBootKey: string(bootSource),
},
LocalDirs: map[string]string{},