github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/build/layerutils/utils.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 layerutils
    16  
    17  import (
    18  	"bufio"
    19  	"strings"
    20  
    21  	"github.com/sirupsen/logrus"
    22  )
    23  
    24  // DecodeImages decode image from yaml content
    25  func DecodeImages(body string) []string {
    26  	var list []string
    27  
    28  	reader := strings.NewReader(body)
    29  	scanner := bufio.NewScanner(reader)
    30  	for scanner.Scan() {
    31  		l := decodeLine(scanner.Text())
    32  		if l != "" {
    33  			list = append(list, l)
    34  		}
    35  	}
    36  	if err := scanner.Err(); err != nil {
    37  		logrus.Errorf(err.Error())
    38  		return list
    39  	}
    40  
    41  	return list
    42  }
    43  
    44  func decodeLine(line string) string {
    45  	l := strings.Replace(line, `"`, "", -1)
    46  	l = strings.Replace(l, ",", "", -1)
    47  	l = strings.Replace(l, " ", "", -1)
    48  	ss := strings.SplitN(l, ":", 2)
    49  	if len(ss) != 2 {
    50  		return ""
    51  	}
    52  	if !strings.HasSuffix(ss[0], "image") || strings.Contains(ss[0], "#") {
    53  		return ""
    54  	}
    55  
    56  	return strings.Replace(ss[1], " ", "", -1)
    57  }