Skip to content

Commit

Permalink
Add login judgment before push
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Mar 14, 2023
1 parent 265f599 commit df8710c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions cmd/sealer/cmd/image/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package image

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/sealerio/sealer/pkg/auth"
Expand All @@ -34,18 +38,29 @@ var exampleForPushCmd = `
func NewPushCmd() *cobra.Command {
pushCmd := &cobra.Command{
Use: "push",
Short: "push ClusterImage to remote registry",
Short: "push sealer image to remote registry",
// TODO: add long description.
Long: longNewPushCmdDescription,
Example: exampleForPushCmd,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
engine, err := imageengine.NewImageEngine(options.EngineGlobalConfigurations{})

login, err := checkIsLogin(pushOpts.Authfile)
if err != nil {
return err
}
pushOpts.Image = args[0]
return engine.Push(pushOpts)

if login {
engine, err := imageengine.NewImageEngine(options.EngineGlobalConfigurations{})
if err != nil {
return err
}
pushOpts.Image = args[0]
return engine.Push(pushOpts)
}

return nil

},
}
pushOpts = &options.PushOptions{}
Expand All @@ -56,3 +71,15 @@ func NewPushCmd() *cobra.Command {
pushCmd.Flags().BoolVar(&pushOpts.All, "all", true, "also push the images in the list")
return pushCmd
}

func checkIsLogin(filePath string) (bool, error) {
authInfo, err := os.ReadFile(filePath)
if err != nil {
return false, err
}
count := strings.Count(string(authInfo), "auth")
if count > 1 {
return true, nil
}
return false, fmt.Errorf("please login registry")
}

0 comments on commit df8710c

Please sign in to comment.