cli/info.go
cli/info.goBrowse 1970 files
697 tokens
2,500 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package cli2 3import (4 "bytes"5 "context"6 "encoding/json"7 "fmt"8 "os"9 "path/filepath"10 11 "github.com/charmbracelet/log"12 "github.com/railwayapp/railpack/core"13 "github.com/urfave/cli/v3"14)15 16var InfoCommand = &cli.Command{17 Name: "info",18 Aliases: []string{"i"},19 Usage: "get as much information as possible about an app",20 ArgsUsage: "DIRECTORY",21 EnableShellCompletion: true,22 Flags: append([]cli.Flag{23 &cli.StringFlag{24 Name: "format",25 Usage: "output format. one of: pretty, json",26 Value: "pretty",27 },28 &cli.StringFlag{29 Name: "out",30 Usage: "output file name",31 },32 &cli.BoolFlag{33 Name: "show-plan",34 Usage: "show the generated build plan",35 },36 }, commonPlanFlags()...),37 Action: func(ctx context.Context, cmd *cli.Command) error {38 buildResult, _, _, err := GenerateBuildResultForCommand(cmd)39 if err != nil {40 return cli.Exit(err, exitCodeForError(err))41 }42 43 format := cmd.String("format")44 45 var rendered bytes.Buffer46 if format == "pretty" {47 rendered.WriteString(core.FormatBuildResult(buildResult, core.PrintOptions{48 Metadata: true,49 Version: Version,50 }))51 52 if cmd.Bool("show-plan") {53 planMap, err := addSchemaToPlanMap(buildResult.Plan)54 if err != nil {55 return cli.Exit(err, ExitCodeFailure)56 }57 serializedPlan, err := json.MarshalIndent(planMap, "", " ")58 if err != nil {59 return cli.Exit(err, ExitCodeFailure)60 }61 62 core.PrettyPrintSectionHeader(&rendered, "Generated railpack-plan.json")63 core.PrettyPrintJSON(&rendered, serializedPlan)64 } else {65 fmt.Fprintf(66 &rendered,67 "Use %s to view generated railpack-plan.json\n",68 core.FormatHighlight("--show-plan"),69 )70 }71 } else {72 serializedResult, err := json.MarshalIndent(buildResult, "", " ")73 if err != nil {74 return cli.Exit(err, ExitCodeFailure)75 }76 fmt.Fprintln(&rendered, string(serializedResult))77 }78 79 output := cmd.String("out")80 if output == "" {81 // Write to stdout if no output file specified82 _, _ = os.Stdout.Write(rendered.Bytes())83 return nil84 } else {85 if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {86 return cli.Exit(err, ExitCodeFailure)87 }88 89 err = os.WriteFile(output, rendered.Bytes(), 0644)90 if err != nil {91 return cli.Exit(err, ExitCodeFailure)92 }93 94 log.Infof("Plan written to %s", output)95 }96 97 if !buildResult.Success {98 os.Exit(ExitCodeFailure)99 return nil100 }101 102 return nil103 },104}105