cli/plan.go
cli/plan.goBrowse 1970 files
445 tokens
1,602 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package cli2 3import (4 "context"5 "encoding/json"6 "os"7 "path/filepath"8 9 "github.com/charmbracelet/log"10 "github.com/urfave/cli/v3"11)12 13var PlanCommand = &cli.Command{14 Name: "plan",15 Aliases: []string{"p"},16 Usage: "generate a build plan for a directory",17 ArgsUsage: "DIRECTORY",18 EnableShellCompletion: true,19 Flags: append([]cli.Flag{20 &cli.StringFlag{21 Name: "out",22 Aliases: []string{"o"},23 Usage: "output file name",24 },25 }, commonPlanFlags()...),26 Action: func(ctx context.Context, cmd *cli.Command) error {27 buildResult, _, _, err := GenerateBuildResultForCommand(cmd)28 if err != nil {29 return cli.Exit(err, exitCodeForError(err))30 }31 32 // Include $schema in the generated plan JSON for editor support33 planMap, err := addSchemaToPlanMap(buildResult.Plan)34 if err != nil {35 return cli.Exit(err, ExitCodeFailure)36 }37 serializedPlan, err := json.MarshalIndent(planMap, "", " ")38 if err != nil {39 return cli.Exit(err, ExitCodeFailure)40 }41 buildResultString := serializedPlan42 43 output := cmd.String("out")44 if output == "" {45 // Write to stdout if no output file specified46 _, _ = os.Stdout.Write([]byte(buildResultString))47 _, _ = os.Stdout.Write([]byte("\n"))48 return nil49 } else {50 if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {51 return cli.Exit(err, ExitCodeFailure)52 }53 54 err = os.WriteFile(output, []byte(buildResultString), 0644)55 if err != nil {56 return cli.Exit(err, ExitCodeFailure)57 }58 59 log.Infof("Plan written to %s", output)60 }61 62 return nil63 },64}65