github.com/sapplications/sb@v0.0.0-20240116135441-1a13cafe3497/smartcreator.go (about)

     1  // Copyright 2023 Vitalii Noha vitalii.noga@gmail.com. All rights reserved.
     2  
     3  package sb
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  )
     9  
    10  // Create creates an application by generating smart application unit (.sa file).
    11  func (c *SmartCreator) Create(application string) error {
    12  	if application == "" {
    13  		return fmt.Errorf(AppIsNotSpecified)
    14  	}
    15  	defer handleError()
    16  	logInfo(c.Logger, fmt.Sprintf("creating \"%s\" application", application))
    17  	reader, err := c.ModManager.ReadAll()
    18  	if err != nil && !strings.HasPrefix(err.Error(), fmt.Sprintf(ModuleFilesMissingF, ModKind.SA)) {
    19  		return err
    20  	}
    21  	items := reader.Items()
    22  	apps, err := getApps(items)
    23  	if err != nil && !strings.HasPrefix(err.Error(), fmt.Sprintf(ItemIsMissingF, AppsItemName)) {
    24  		return err
    25  	}
    26  	if apps != nil {
    27  		for _, row := range apps {
    28  			if row[0] == application {
    29  				return fmt.Errorf(AppIsExistF, application)
    30  			}
    31  		}
    32  	}
    33  	if _, ok := items[AppsItemName]; !ok {
    34  		err = c.ModManager.AddItem(fmt.Sprintf("%s.%s", DefaultModuleName, ModKind.SA), AppsItemName)
    35  		if err != nil {
    36  			return err
    37  		}
    38  	}
    39  	return c.ModManager.AddDependency(AppsItemName, application, "", false)
    40  }