github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/utils/meta_image.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 utils
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/spf13/cobra"
    21  
    22  	"github.com/alibaba/sealer/pkg/image/store"
    23  )
    24  
    25  func SimilarImageListByName(imgName string) ([]string, error) {
    26  	return similarImageList(imgName, true)
    27  }
    28  
    29  func similarImageList(imageArg string, byName bool) (similarImageList []string, err error) {
    30  	is, err := store.NewDefaultImageStore()
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	metadataMap, err := is.GetImageMetadataMap()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	for name := range metadataMap {
    40  		if byName && (strings.Contains(name, imageArg) || imageArg == "") {
    41  			similarImageList = append(similarImageList, name)
    42  		}
    43  	}
    44  	return
    45  }
    46  
    47  func ImageListFuncForCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    48  	similarImages, err := SimilarImageListByName(toComplete)
    49  	if err != nil {
    50  		return nil, cobra.ShellCompDirectiveDefault
    51  	}
    52  	return similarImages, cobra.ShellCompDirectiveNoFileComp
    53  }