swap 'pkg push' for 'pkg build --push', keeping 'pkg push' as deprecated but still working (#4141)

Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
Avi Deitcher 2025-07-04 18:00:28 +03:00 committed by GitHub
parent 2b4687338b
commit c0c5668116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 241 additions and 226 deletions

View File

@ -75,9 +75,11 @@ func pkgCmd() *cobra.Command {
}, },
} }
cmd.AddCommand(pkgBuildCmd()) // because there is an alias 'pkg push' for 'pkg build --push', we need to add the build command first
buildCmd := pkgBuildCmd()
cmd.AddCommand(buildCmd)
cmd.AddCommand(pkgBuilderCmd()) cmd.AddCommand(pkgBuilderCmd())
cmd.AddCommand(pkgPushCmd()) cmd.AddCommand(pkgPushCmd(buildCmd))
cmd.AddCommand(pkgShowTagCmd()) cmd.AddCommand(pkgShowTagCmd())
cmd.AddCommand(pkgManifestCmd()) cmd.AddCommand(pkgManifestCmd())
cmd.AddCommand(pkgRemoteTagCmd()) cmd.AddCommand(pkgRemoteTagCmd())

View File

@ -24,17 +24,17 @@ const (
// pkg build --pull - builds unless is in cache or published in registry; pulls from registry if not in cache // pkg build --pull - builds unless is in cache or published in registry; pulls from registry if not in cache
// pkg build --force - always builds even if is in cache or published in registry // pkg build --force - always builds even if is in cache or published in registry
// pkg build --force --pull - always builds even if is in cache or published in registry; --pull ignored // pkg build --force --pull - always builds even if is in cache or published in registry; --pull ignored
// pkg push - always builds unless is in cache // pkg build --push - always builds unless is in cache or published in registry; pushes to registry
// pkg push --force - always builds even if is in cache // pkg build --push --force - always builds even if is in cache
// pkg push --nobuild - skips build; if not in cache, fails // pkg build --push --nobuild - skips build; if not in cache, fails
// pkg push --nobuild --force - nonsensical // pkg build --push --nobuild --force - nonsensical
// pkg push - equivalent to pkg build --push
// addCmdRunPkgBuildPush adds the RunE function and flags to a cobra.Command func pkgBuildCmd() *cobra.Command {
// for "pkg build" or "pkg push".
func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
var ( var (
force bool force bool
pull bool pull bool
push bool
ignoreCache bool ignoreCache bool
docker bool docker bool
platforms string platforms string
@ -53,8 +53,15 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
progress string progress string
ssh []string ssh []string
) )
cmd := &cobra.Command{
cmd.RunE = func(cmd *cobra.Command, args []string) error { Use: "build",
Short: "build an OCI package from a directory with a yaml configuration file",
Long: `Build an OCI package from a directory with a yaml configuration file.
'path' specifies the path to the package source directory.
`,
Example: ` linuxkit pkg build [options] pkg/dir/`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pkgs, err := pkglib.NewFromConfig(pkglibConfig, args...) pkgs, err := pkglib.NewFromConfig(pkglibConfig, args...)
if err != nil { if err != nil {
return err return err
@ -80,7 +87,7 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String())) opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String()))
if withPush { if push {
opts = append(opts, pkglib.WithBuildPush()) opts = append(opts, pkglib.WithBuildPush())
if nobuild { if nobuild {
opts = append(opts, pkglib.WithBuildSkip()) opts = append(opts, pkglib.WithBuildSkip())
@ -245,7 +252,7 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
var msg, action string var msg, action string
switch { switch {
case !withPush: case !push:
msg = fmt.Sprintf("Building %q", p.Tag()) msg = fmt.Sprintf("Building %q", p.Tag())
action = "building" action = "building"
case nobuild: case nobuild:
@ -263,10 +270,11 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
} }
} }
return nil return nil
},
} }
cmd.Flags().BoolVar(&force, "force", false, "Force rebuild even if image is in local cache") cmd.Flags().BoolVar(&force, "force", false, "Force rebuild even if image is in local cache")
cmd.Flags().BoolVar(&pull, "pull", false, "Pull image if in registry but not in local cache; conflicts with --force") cmd.Flags().BoolVar(&pull, "pull", false, "Pull image if in registry but not in local cache; conflicts with --force")
cmd.Flags().BoolVar(&push, "push", false, "After building, if successful, push the image to the registry; if --nobuild is set, just push")
cmd.Flags().BoolVar(&ignoreCache, "ignore-cached", false, "Ignore cached intermediate images, always pulling from registry") cmd.Flags().BoolVar(&ignoreCache, "ignore-cached", false, "Ignore cached intermediate images, always pulling from registry")
cmd.Flags().BoolVar(&docker, "docker", false, "Store the built image in the docker image cache instead of the default linuxkit cache") cmd.Flags().BoolVar(&docker, "docker", false, "Store the built image in the docker image cache instead of the default linuxkit cache")
cmd.Flags().StringVar(&platforms, "platforms", "", "Which platforms to build for, defaults to all of those for which the package can be built") cmd.Flags().StringVar(&platforms, "platforms", "", "Which platforms to build for, defaults to all of those for which the package can be built")
@ -287,18 +295,6 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
return cmd return cmd
} }
func pkgBuildCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "build",
Short: "build an OCI package from a directory with a yaml configuration file",
Long: `Build an OCI package from a directory with a yaml configuration file.
'path' specifies the path to the package source directory.
`,
Example: ` linuxkit pkg build [options] pkg/dir/`,
Args: cobra.MinimumNArgs(1),
}
return addCmdRunPkgBuildPush(cmd, false)
}
func buildPlatformBuildersMap(inputs string, existing map[string]string) (map[string]string, error) { func buildPlatformBuildersMap(inputs string, existing map[string]string) (map[string]string, error) {
if inputs == "" { if inputs == "" {

View File

@ -1,18 +1,35 @@
package main package main
import "github.com/spf13/cobra" import (
"fmt"
func pkgPushCmd() *cobra.Command { "github.com/spf13/cobra"
)
func pkgPushCmd(buildCmd *cobra.Command) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "push", Use: "push",
Short: "build and push an OCI package from a directory with a yaml configuration file", Short: "Alias for 'pkg build --push'",
Long: `Build and push an OCI package from a directory with a yaml configuration file. Long: `Build and push an OCI package from a directory with a yaml configuration file.
'path' specifies the path to the package source directory. 'path' specifies the path to the package source directory.
The package may or may not be built first, depending on options The package may or may not be built first, depending on options
`, `,
Example: ` linuxkit pkg push [options] pkg/dir/`, Example: ` linuxkit pkg push [options] pkg/dir/`,
SuggestFor: []string{"build"},
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Deprecated: "use 'pkg build --push' instead",
RunE: func(cmd *cobra.Command, args []string) error {
// Create a copy of buildCmd with push=true
if err := buildCmd.Flags().Set("push", "true"); err != nil {
return fmt.Errorf("'pkg push' unable to set 'pkg build --push': %w", err)
} }
return addCmdRunPkgBuildPush(cmd, true)
// Pass the args to the build command
buildCmd.SetArgs(args)
return buildCmd.RunE(buildCmd, args)
},
}
cmd.Flags().AddFlagSet(buildCmd.Flags())
return cmd
} }