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