github.com/gopinath-langote/1build@v1.7.0/cmd/config/parse.go (about)

     1  package config
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/gopinath-langote/1build/cmd/utils"
     7  	"strings"
     8  
     9  	"gopkg.in/yaml.v3"
    10  )
    11  
    12  const (
    13  	// BeforeCommand contains before command definition
    14  	BeforeCommand = "before"
    15  
    16  	// AfterCommand contains after command definition
    17  	AfterCommand = "after"
    18  )
    19  
    20  // OneBuildConfigFileName one global declaration of config file name
    21  var OneBuildConfigFileName = "1build.yaml"
    22  
    23  // OneBuildConfiguration is a representation of yaml configuration as struct
    24  type OneBuildConfiguration struct {
    25  	Project  string              `yaml:"project"`
    26  	Before   string              `yaml:"before,omitempty"`
    27  	After    string              `yaml:"after,omitempty"`
    28  	Commands []map[string]string `yaml:"commands"`
    29  }
    30  
    31  // LoadOneBuildConfiguration returns the config from file as struct.
    32  // If there is an error, it will be of type *Error.
    33  func LoadOneBuildConfiguration() (OneBuildConfiguration, error) {
    34  	var configuration OneBuildConfiguration
    35  	fileContent, err := ReadFile()
    36  	if err != nil {
    37  		return OneBuildConfiguration{}, err
    38  	}
    39  	yamlError := yaml.Unmarshal([]byte(fileContent), &configuration)
    40  	if yamlError != nil {
    41  		message :=
    42  			`Sample format is:
    43  
    44  ------------------------------------------------------------------------
    45  project: Sample Project
    46  commands:
    47    - build: npm run build
    48    - lint: eslint
    49  ------------------------------------------------------------------------
    50  `
    51  		message = "Unable to parse '" + OneBuildConfigFileName + "' config file. Make sure file is in correct format.\n" +
    52  			message
    53  		return configuration, errors.New(message)
    54  	}
    55  	return configuration, nil
    56  }
    57  
    58  // GetCommand return command by name
    59  func (oneBuildConfiguration *OneBuildConfiguration) GetCommand(name string) (value string) {
    60  	for _, command := range oneBuildConfiguration.Commands {
    61  		for k, v := range command {
    62  			if k == name {
    63  				return v
    64  			}
    65  		}
    66  	}
    67  	return
    68  }
    69  
    70  // Print prints the configuration to the console
    71  func (oneBuildConfiguration *OneBuildConfiguration) Print() {
    72  	fmt.Println(utils.Dash() + "\nproject: " + oneBuildConfiguration.Project)
    73  	if oneBuildConfiguration.Before != "" {
    74  		fmt.Println("before: " + oneBuildConfiguration.Before)
    75  	}
    76  	if oneBuildConfiguration.After != "" {
    77  		fmt.Println("after: " + oneBuildConfiguration.After)
    78  	}
    79  	fmt.Println("commands:")
    80  
    81  	for _, command := range oneBuildConfiguration.Commands {
    82  		for k, v := range command {
    83  			fmt.Println(strings.TrimSpace(k) + " | " + strings.TrimSpace(v))
    84  		}
    85  	}
    86  	fmt.Println(utils.Dash())
    87  }