Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add "dev render" command
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jun 1, 2022
1 parent b976ca0 commit 803165d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pkg/appdefinition/appdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func (a *AppDefinition) WithBuildParams(params map[string]interface{}) (*AppDefi
}, nil
}

func (a *AppDefinition) JSON() (string, error) {
app, err := a.ctx.Value()
if err != nil {
return "", err
}
data, err := json.MarshalIndent(app, "", " ")
return string(data), err
}

func (a *AppDefinition) AppSpec() (*v1.AppSpec, error) {
app, err := a.ctx.Value()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
)

func NewDev() *cobra.Command {
return cli.Command(&Dev{}, cobra.Command{
cmd := cli.Command(&Dev{}, cobra.Command{
Use: "dev [flags] DIRECTORY",
SilenceUsage: true,
Short: "Build and run an app in development mode",
Long: "Build and run an app in development mode",
Args: cobra.MaximumNArgs(1),
})
cmd.AddCommand(NewRender())
return cmd
}

type Dev struct {
Expand Down
79 changes: 79 additions & 0 deletions pkg/cli/dev_render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cli

import (
"fmt"

"github.com/acorn-io/acorn/pkg/appdefinition"
"github.com/acorn-io/acorn/pkg/build"
"github.com/acorn-io/acorn/pkg/cue"
"github.com/acorn-io/acorn/pkg/flagparams"
cli "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func NewRender() *cobra.Command {
cmd := cli.Command(&Render{}, cobra.Command{
Use: "render [flags] DIRECTORY",
SilenceUsage: true,
Short: "Evaluate and display an acorn.cue with deploy params",
Args: cobra.MinimumNArgs(1),
})
cmd.Flags().SetInterspersed(false)
return cmd
}

type Render struct {
File string `short:"f" usage:"Name of the dev file" default:"DIRECTORY/acorn.cue"`
}

func (s *Render) Run(cmd *cobra.Command, args []string) error {
cwd := "."
if len(args) > 0 {
cwd = args[0]
}

buildFile := build.ResolveFile(s.File, cwd)
data, err := cue.ReadCUE(buildFile)
if err != nil {
return err
}

appDef, err := appdefinition.NewAppDefinition(data)
if err != nil {
return err
}

appSpec, err := appDef.AppSpec()
if err != nil {
return err
}

params, err := appDef.DeployParams()
if err != nil {
return err
}

flags := flagparams.New(s.File, params)
flags.Usage = usage(appSpec)

deployParams, err := flags.Parse(args)
if pflag.ErrHelp == err {
return nil
} else if err != nil {
return err
}

appDef, err = appDef.WithDeployParams(deployParams)
if err != nil {
return err
}

v, err := appDef.JSON()
if err != nil {
return err
}
fmt.Print(v)

return nil
}

0 comments on commit 803165d

Please sign in to comment.