github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/search.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/alibaba/sealer/common"
    22  	"github.com/alibaba/sealer/pkg/image/reference"
    23  	save2 "github.com/alibaba/sealer/pkg/image/save"
    24  	reference2 "github.com/distribution/distribution/v3/reference"
    25  	"github.com/olekukonko/tablewriter"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  // searchCmd represents the search command
    30  var searchCmd = &cobra.Command{
    31  	Use:   "search",
    32  	Short: "sealer search kubernetes",
    33  	Example: `
    34  sealer search <imageDomain>/<imageRepo>/<imageName> ...
    35  ## default imageDomain: 'registry.cn-qingdao.aliyuncs.com', default imageRepo: 'sealer-io'
    36  ex.:
    37    sealer search kubernetes seadent/rootfs docker.io/library/hello-world`,
    38  	Args: cobra.MinimumNArgs(1),
    39  	RunE: func(cmd *cobra.Command, args []string) error {
    40  
    41  		table := tablewriter.NewWriter(common.StdOut)
    42  		table.SetHeader([]string{imageName, "version"})
    43  		for _, imgName := range args {
    44  			named, err := reference.ParseToNamed(imgName)
    45  			if err != nil {
    46  				return err
    47  			}
    48  			ns, err := save2.NewProxyRegistry(context.Background(), "", named.Domain())
    49  			if err != nil {
    50  				return err
    51  			}
    52  			rNamed, err := reference2.WithName(named.Repo())
    53  			if err != nil {
    54  				return fmt.Errorf("get repository name error: %v", err)
    55  			}
    56  			repo, err := ns.Repository(context.Background(), rNamed)
    57  			if err != nil {
    58  				return err
    59  			}
    60  			tags, err := repo.Tags(context.Background()).All(context.Background())
    61  			if err != nil {
    62  				return err
    63  			}
    64  			for _, tag := range tags {
    65  				table.Append([]string{named.String(), tag})
    66  			}
    67  		}
    68  		table.Render()
    69  		return nil
    70  	},
    71  }
    72  
    73  func init() {
    74  	rootCmd.AddCommand(searchCmd)
    75  }