github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/source.go (about)

     1  package main
     2  
     3  import (
     4  	"compress/gzip"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/lmorg/murex/builtins/pipes/term"
     9  	"github.com/lmorg/murex/config/defaults"
    10  	"github.com/lmorg/murex/debug"
    11  	"github.com/lmorg/murex/integrations"
    12  	"github.com/lmorg/murex/lang"
    13  	"github.com/lmorg/murex/lang/ref"
    14  	"github.com/lmorg/murex/utils"
    15  	"github.com/lmorg/murex/utils/ansi"
    16  	"github.com/lmorg/murex/utils/consts"
    17  )
    18  
    19  func diskSource(filename string) ([]byte, error) {
    20  	var b []byte
    21  
    22  	file, err := os.Open(filename)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	if len(filename) > 3 && filename[len(filename)-3:] == ".gz" {
    28  		gz, err := gzip.NewReader(file)
    29  		if err != nil {
    30  			file.Close()
    31  			return nil, err
    32  		}
    33  		b, err = io.ReadAll(gz)
    34  
    35  		file.Close()
    36  		gz.Close()
    37  
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  	} else {
    43  		b, err = io.ReadAll(file)
    44  		file.Close()
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  	}
    49  
    50  	return b, nil
    51  }
    52  
    53  func execSource(source []rune, sourceRef *ref.Source, exitOnError bool) {
    54  	if sourceRef == nil {
    55  		panic("sourceRef is not defined")
    56  	}
    57  
    58  	if debug.Enabled {
    59  		os.Stderr.WriteString("Loading profile `" + sourceRef.Module + "`" + utils.NewLineString)
    60  	}
    61  
    62  	var stdin int
    63  	if os.Getenv(consts.EnvMethod) != consts.EnvTrue {
    64  		stdin = lang.F_NO_STDIN
    65  	}
    66  	fork := lang.ShellProcess.Fork(lang.F_PARENT_VARTABLE | stdin)
    67  	fork.Stdout = new(term.Out)
    68  	fork.Stderr = term.NewErr(ansi.IsAllowed())
    69  	fork.FileRef.Source = sourceRef
    70  	fork.RunMode = lang.ShellProcess.RunMode
    71  	exitNum, err := fork.Execute(source)
    72  
    73  	if err != nil {
    74  		if exitNum == 0 {
    75  			exitNum = 1
    76  		}
    77  		os.Stderr.WriteString(err.Error() + utils.NewLineString)
    78  		lang.Exit(exitNum)
    79  	}
    80  
    81  	if exitNum != 0 && exitOnError {
    82  		lang.Exit(exitNum)
    83  	}
    84  }
    85  
    86  func defaultProfile() {
    87  	defaults.AddMurexProfile()
    88  
    89  	for _, profile := range defaults.DefaultProfiles {
    90  		ref := ref.History.AddSource("(builtin)", "builtin/"+profile.Name, profile.Block)
    91  		execSource([]rune(string(profile.Block)), ref, false)
    92  	}
    93  
    94  	for _, profile := range integrations.Profiles() {
    95  		ref := ref.History.AddSource("(builtin)", "builtin/integrations_"+profile.Name, profile.Block)
    96  		execSource([]rune(string(profile.Block)), ref, false)
    97  	}
    98  }