...

Source file src/github.com/redhat-developer/odo/pkg/apiserver-impl/devstate/state.go

Documentation: github.com/redhat-developer/odo/pkg/apiserver-impl/devstate

     1  package devstate
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	apidevfile "github.com/devfile/api/v2/pkg/devfile"
     8  	"github.com/devfile/library/v2/pkg/devfile"
     9  	"github.com/devfile/library/v2/pkg/devfile/parser"
    10  	context "github.com/devfile/library/v2/pkg/devfile/parser/context"
    11  	"github.com/devfile/library/v2/pkg/testingutil/filesystem"
    12  
    13  	. "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
    14  
    15  	"k8s.io/utils/pointer"
    16  )
    17  
    18  type DevfileState struct {
    19  	Devfile parser.DevfileObj
    20  	FS      filesystem.Filesystem
    21  }
    22  
    23  func NewDevfileState() DevfileState {
    24  	s := DevfileState{
    25  		FS: filesystem.NewFakeFs(),
    26  	}
    27  	// this should never fail, as the parameters are constant
    28  	_, _ = s.SetDevfileContent(`schemaVersion: 2.2.0`)
    29  	return s
    30  }
    31  
    32  // SetDevfileContent replaces the devfile with a new content
    33  // If an error occurs, the Devfile is not modified
    34  func (o *DevfileState) SetDevfileContent(content string) (DevfileContent, error) {
    35  	parserArgs := parser.ParserArgs{
    36  		Data:                          []byte(content),
    37  		ConvertKubernetesContentInUri: pointer.Bool(false),
    38  		SetBooleanDefaults:            pointer.Bool(false),
    39  	}
    40  	var err error
    41  	devfile, _, err := devfile.ParseDevfileAndValidate(parserArgs)
    42  	if err != nil {
    43  		return DevfileContent{}, fmt.Errorf("error parsing devfile YAML: %w", err)
    44  	}
    45  	o.Devfile = devfile
    46  	o.Devfile.Ctx = context.FakeContext(o.FS, o.Devfile.Ctx.GetAbsPath())
    47  	return o.GetContent()
    48  }
    49  
    50  func (o *DevfileState) SetMetadata(
    51  	name string,
    52  	version string,
    53  	displayName string,
    54  	description string,
    55  	tags string,
    56  	architectures string,
    57  	icon string,
    58  	globalMemoryLimit string,
    59  	projectType string,
    60  	language string,
    61  	website string,
    62  	provider string,
    63  	supportUrl string,
    64  ) (DevfileContent, error) {
    65  	o.Devfile.Data.SetMetadata(apidevfile.DevfileMetadata{
    66  		Name:              name,
    67  		Version:           version,
    68  		DisplayName:       displayName,
    69  		Description:       description,
    70  		Tags:              splitTags(tags),
    71  		Architectures:     splitArchitectures(architectures),
    72  		Icon:              icon,
    73  		GlobalMemoryLimit: globalMemoryLimit,
    74  		ProjectType:       projectType,
    75  		Language:          language,
    76  		Website:           website,
    77  		Provider:          provider,
    78  		SupportUrl:        supportUrl,
    79  	})
    80  	return o.GetContent()
    81  }
    82  func splitArchitectures(architectures string) []apidevfile.Architecture {
    83  	if architectures == "" {
    84  		return nil
    85  	}
    86  	parts := strings.Split(architectures, SEPARATOR)
    87  	result := make([]apidevfile.Architecture, len(parts))
    88  	for i, arch := range parts {
    89  		result[i] = apidevfile.Architecture(strings.Trim(arch, " "))
    90  	}
    91  	return result
    92  }
    93  
    94  func splitTags(tags string) []string {
    95  	if tags == "" {
    96  		return nil
    97  	}
    98  	parts := strings.Split(tags, SEPARATOR)
    99  	result := make([]string, len(parts))
   100  	for i, tag := range parts {
   101  		result[i] = strings.Trim(tag, " ")
   102  	}
   103  	return result
   104  }
   105  

View as plain text