cli

package
v0.0.0-...-41454cb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BuildCommand = &cli.Command{
	Name:                  "build",
	Aliases:               []string{"b"},
	Usage:                 "build an image with BuildKit",
	ArgsUsage:             "DIRECTORY",
	EnableShellCompletion: true,
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:  "name",
			Usage: "name of the image to build",
		},
		&cli.StringFlag{
			Name:  "output",
			Usage: "output the final filesystem to a local directory",
		},
		&cli.StringFlag{
			Name:  "platform",
			Usage: "platform to build for (e.g. linux/amd64, linux/arm64)",
		},
		&cli.StringFlag{
			Name:  "progress",
			Usage: "buildkit progress output mode. Values: auto, plain, tty",
			Value: "auto",
		},
		&cli.BoolFlag{
			Name:  "show-plan",
			Usage: "Show the build plan before building. This is useful for development and debugging.",
			Value: false,
		},
		&cli.StringFlag{
			Name:  "cache-key",
			Usage: "Unique id to prefix to cache keys",
		},
	}, commonPlanFlags()...),
	Action: func(ctx context.Context, cmd *cli.Command) error {
		buildResult, app, env, err := GenerateBuildResultForCommand(cmd)
		if err != nil {
			return cli.Exit(err, 1)
		}

		core.PrettyPrintBuildResult(buildResult, core.PrintOptions{Version: Version})

		if !buildResult.Success {
			os.Exit(1)
			return nil
		}

		serializedPlan, err := json.MarshalIndent(buildResult.Plan, "", "  ")
		if err != nil {
			return cli.Exit(err, 1)
		}

		if cmd.Bool("show-plan") {
			fmt.Println(string(serializedPlan))
		}

		err = validateSecrets(buildResult.Plan, env)
		if err != nil {
			return cli.Exit(err, 1)
		}

		secretsHash := getSecretsHash(env)

		platform, err := getPlatform(cmd.String("platform"))
		if err != nil {
			return cli.Exit(err, 1)
		}

		err = buildkit.BuildWithBuildkitClient(app.Source, buildResult.Plan, buildkit.BuildWithBuildkitClientOptions{
			ImageName:    cmd.String("name"),
			DumpLLB:      cmd.Bool("llb"),
			OutputDir:    cmd.String("output"),
			ProgressMode: cmd.String("progress"),
			CacheKey:     cmd.String("cache-key"),
			SecretsHash:  secretsHash,
			Secrets:      env.Variables,
			Platform:     platform,
		})
		if err != nil {
			return cli.Exit(err, 1)
		}

		return nil
	},
}
View Source
var FrontendCommand = &cli.Command{
	Name:  "frontend",
	Usage: "Start the BuildKit GRPC frontend server",
	Action: func(ctx context.Context, cmd *cli.Command) error {
		buildkit.StartFrontend()

		return nil
	},
}
View Source
var InfoCommand = &cli.Command{
	Name:                  "info",
	Aliases:               []string{"i"},
	Usage:                 "get as much information as possible about an app",
	ArgsUsage:             "DIRECTORY",
	EnableShellCompletion: true,
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:  "format",
			Usage: "output format. one of: pretty, json",
			Value: "pretty",
		},
		&cli.StringFlag{
			Name:  "out",
			Usage: "output file name",
		},
	}, commonPlanFlags()...),
	Action: func(ctx context.Context, cmd *cli.Command) error {
		buildResult, _, _, err := GenerateBuildResultForCommand(cmd)
		if err != nil {
			return cli.Exit(err, 1)
		}

		format := cmd.String("format")

		var buildResultString string
		if format == "pretty" {
			buildResultString = core.FormatBuildResult(buildResult, core.PrintOptions{
				Metadata: true,
				Version:  Version,
			})
		} else {
			serializedResult, err := json.MarshalIndent(buildResult, "", "  ")
			if err != nil {
				return cli.Exit(err, 1)
			}
			buildResultString = string(serializedResult)
		}

		output := cmd.String("out")
		if output == "" {

			os.Stdout.Write([]byte(buildResultString))
			os.Stdout.Write([]byte("\n"))
			return nil
		} else {
			if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {
				return cli.Exit(err, 1)
			}

			err = os.WriteFile(output, []byte(buildResultString), 0644)
			if err != nil {
				return cli.Exit(err, 1)
			}

			log.Infof("Plan written to %s", output)
		}

		if !buildResult.Success {
			os.Exit(1)
			return nil
		}

		return nil
	},
}
View Source
var PlanCommand = &cli.Command{
	Name:                  "plan",
	Aliases:               []string{"p"},
	Usage:                 "generate a build plan for a directory",
	ArgsUsage:             "DIRECTORY",
	EnableShellCompletion: true,
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:    "out",
			Aliases: []string{"o"},
			Usage:   "output file name",
		},
	}, commonPlanFlags()...),
	Action: func(ctx context.Context, cmd *cli.Command) error {
		buildResult, _, _, err := GenerateBuildResultForCommand(cmd)
		if err != nil {
			return cli.Exit(err, 1)
		}

		serializedPlan, err := json.MarshalIndent(buildResult.Plan, "", "  ")
		if err != nil {
			return cli.Exit(err, 1)
		}
		buildResultString := string(serializedPlan)

		output := cmd.String("out")
		if output == "" {

			os.Stdout.Write([]byte(buildResultString))
			os.Stdout.Write([]byte("\n"))
			return nil
		} else {
			if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {
				return cli.Exit(err, 1)
			}

			err = os.WriteFile(output, []byte(buildResultString), 0644)
			if err != nil {
				return cli.Exit(err, 1)
			}

			log.Infof("Plan written to %s", output)
		}

		return nil
	},
}
View Source
var PrepareCommand = &cli.Command{
	Name:                  "prepare",
	Aliases:               []string{"p"},
	Usage:                 "prepares all the files necessary for a platform to build an app with the BuildKit frontend",
	ArgsUsage:             "DIRECTORY",
	EnableShellCompletion: true,
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:  "plan-out",
			Usage: "output file for the JSON serialized build plan",
		},
		&cli.StringFlag{
			Name:  "info-out",
			Usage: "output file for the JSON serialized build result info",
		},
	}, commonPlanFlags()...),
	Action: func(ctx context.Context, cmd *cli.Command) error {
		buildResult, _, _, err := GenerateBuildResultForCommand(cmd)
		if err != nil {
			return cli.Exit(err, 1)
		}

		core.PrettyPrintBuildResult(buildResult, core.PrintOptions{Version: Version})

		if !buildResult.Success {
			os.Exit(1)
			return nil
		}

		if planOut := cmd.String("plan-out"); planOut != "" {
			if err := writeJSONFile(planOut, buildResult.Plan, "Build plan written to %s"); err != nil {
				return cli.Exit(err, 1)
			}
		}

		if infoOut := cmd.String("info-out"); infoOut != "" {
			buildResult.Plan = nil
			if err := writeJSONFile(infoOut, buildResult, "Build result info written to %s"); err != nil {
				return cli.Exit(err, 1)
			}
		}

		return nil
	},
}
View Source
var SchemaCommand = &cli.Command{
	Name:                  "schema",
	Usage:                 "outputs the JSON schema for the Railpack config",
	EnableShellCompletion: true,
	Flags:                 []cli.Flag{},
	Action: func(ctx context.Context, cmd *cli.Command) error {
		schema := config.GetJsonSchema()

		schemaJson, err := json.MarshalIndent(schema, "", "  ")
		if err != nil {
			return cli.Exit(err, 1)
		}

		os.Stdout.Write(schemaJson)
		os.Stdout.Write([]byte("\n"))

		return nil
	},
}
View Source
var Version string // This will be set by main

Functions

func GenerateBuildResultForCommand

func GenerateBuildResultForCommand(cmd *cli.Command) (*core.BuildResult, *a.App, *a.Environment, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL