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(pkgPushCmd())
cmd.AddCommand(pkgPushCmd(buildCmd))
cmd.AddCommand(pkgShowTagCmd())
cmd.AddCommand(pkgManifestCmd())
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 --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 push - always builds unless is in cache
// pkg push --force - always builds even if is in cache
// pkg push --nobuild - skips build; if not in cache, fails
// pkg push --nobuild --force - nonsensical
// pkg build --push - always builds unless is in cache or published in registry; pushes to registry
// pkg build --push --force - always builds even if is in cache
// pkg build --push --nobuild - skips build; if not in cache, fails
// pkg build --push --nobuild --force - nonsensical
// pkg push - equivalent to pkg build --push
// addCmdRunPkgBuildPush adds the RunE function and flags to a cobra.Command
// for "pkg build" or "pkg push".
func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
func pkgBuildCmd() *cobra.Command {
var (
force bool
pull bool
push bool
ignoreCache bool
docker bool
platforms string
@ -53,8 +53,15 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
progress string
ssh []string
)
cmd.RunE = func(cmd *cobra.Command, args []string) error {
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),
RunE: func(cmd *cobra.Command, args []string) error {
pkgs, err := pkglib.NewFromConfig(pkglibConfig, args...)
if err != nil {
return err
@ -80,7 +87,7 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String()))
if withPush {
if push {
opts = append(opts, pkglib.WithBuildPush())
if nobuild {
opts = append(opts, pkglib.WithBuildSkip())
@ -245,7 +252,7 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
var msg, action string
switch {
case !withPush:
case !push:
msg = fmt.Sprintf("Building %q", p.Tag())
action = "building"
case nobuild:
@ -263,10 +270,11 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
}
}
return nil
},
}
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(&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(&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")
@ -287,18 +295,6 @@ func addCmdRunPkgBuildPush(cmd *cobra.Command, withPush bool) *cobra.Command {
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) {
if inputs == "" {

View File

@ -1,18 +1,35 @@
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{
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.
'path' specifies the path to the package source directory.
The package may or may not be built first, depending on options
`,
Example: ` linuxkit pkg push [options] pkg/dir/`,
SuggestFor: []string{"build"},
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
}