github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/debug/images.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 debug
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  type ImagesManagement interface {
    24  	ShowDefaultImages() error
    25  	GetDefaultImage() (string, error)
    26  }
    27  
    28  const DefaultSealerRegistryURL = "registry.cn-qingdao.aliyuncs.com/sealer-apps/"
    29  
    30  var ShowImagesCMD = &cobra.Command{
    31  	Use:   "show-images",
    32  	Short: "List default images",
    33  	Args:  cobra.NoArgs,
    34  	RunE: func(cmd *cobra.Command, args []string) error {
    35  		manager := NewDebugImagesManager()
    36  		manager.RegistryURL = DefaultSealerRegistryURL
    37  
    38  		if err := manager.ShowDefaultImages(); err != nil {
    39  			return err
    40  		}
    41  		return nil
    42  	},
    43  }
    44  
    45  // ImagesManager holds the default images information.
    46  type ImagesManager struct {
    47  	RegistryURL string
    48  
    49  	DefaultImagesMap map[string]string // "RichToolsOnUbuntu": "debug:ubuntu"
    50  	DefaultImageKey  string            // "RichToolsOnUbuntu"
    51  
    52  	DefaultImage string // RegistryURL + DefaultImagesMap[DefaultImageName]
    53  }
    54  
    55  func NewDebugImagesManager() *ImagesManager {
    56  	return &ImagesManager{
    57  		DefaultImagesMap: map[string]string{
    58  			"RichToolsOnUbuntu": "debug:ubuntu",
    59  		},
    60  
    61  		DefaultImageKey: "RichToolsOnUbuntu",
    62  	}
    63  }
    64  
    65  // ShowDefaultImages shows default images provided by debug.
    66  func (manager *ImagesManager) ShowDefaultImages() error {
    67  	if len(manager.RegistryURL) == 0 {
    68  		manager.RegistryURL = DefaultSealerRegistryURL
    69  	}
    70  	fmt.Println("There are several default images you can use:")
    71  	for key, value := range manager.DefaultImagesMap {
    72  		fmt.Println(key + ":  " + manager.RegistryURL + value)
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  // GetDefaultImage return the default image provide by debug.
    79  func (manager *ImagesManager) GetDefaultImage() (string, error) {
    80  	if len(manager.RegistryURL) == 0 {
    81  		manager.RegistryURL = DefaultSealerRegistryURL
    82  	}
    83  	return manager.RegistryURL + manager.DefaultImagesMap[manager.DefaultImageKey], nil
    84  }