-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.go
145 lines (107 loc) · 2.9 KB
/
shell.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package shellframework
import (
"os"
"github.com/eshu0/shellframework/defaultcmds"
"github.com/eshu0/shellframework/interfaces"
"github.com/eshu0/simplelogger/interfaces"
)
type Shell struct {
sfinterfaces.IShell
commands []sfinterfaces.ICommand
environment *sfinterfaces.IEnvironment
version string
session *sfinterfaces.ISession
log *slinterfaces.ISimpleLogger
in *os.File
out *os.File
err *os.File
}
func NewShellFromFile(session sfinterfaces.ISession,scriptinfilepath string, Out *os.File, Err *os.File, logi slinterfaces.ISimpleLogger) *Shell {
var input *os.File
// script file been provided as input to the cli?
if(scriptinfilepath != "") {
// open the file if we cannot then crash out
inf, err := os.Open(scriptinfilepath)
if err != nil {
// should write an error here
return nil
}
// replace the Stdin with the file we have read
input = inf
// we don't want an interactive session as we are running a script
session.SetInteractiveMethod(DefaultNonInteractiveMethod)
} else {
// should write an error here
return nil
}
shell := NewShell(session,input, Out, Err, logi)
return shell
}
func NewShell(session sfinterfaces.ISession, In *os.File, Out *os.File, Err *os.File, logi slinterfaces.ISimpleLogger) *Shell {
shell := &Shell{}
env := NewEnvironment(shell)
shell.SetEnvironment(env)
shell.SetSession(session)
shell.version = sfinterfaces.Version
shell.in = In
shell.out = Out
shell.err = Err
shell.log = &logi
shell.commands = []sfinterfaces.ICommand{}
// Add the default commands
mcmd := dcmds.ManCommand{}
mcmd.Register(shell)
hcmd := dcmds.HelpCommand{}
hcmd.Register(shell)
excmd := dcmds.ExitCommand{}
excmd.Register(shell)
ccmd := dcmds.CmdCommand{}
ccmd.Register(shell)
envcmd := dcmds.EnvCommand{}
envcmd.Register(shell)
echocmd := dcmds.EchoCommand{}
echocmd.Register(shell)
return shell
}
// Get Methods
func (shell *Shell) SetCommands(cmds []sfinterfaces.ICommand) {
shell.commands = cmds
}
func (shell *Shell) GetCommands() []sfinterfaces.ICommand {
return shell.commands
}
func (shell *Shell) SetEnvironment(env sfinterfaces.IEnvironment) {
shell.environment = &env
}
func (shell *Shell) GetEnvironment() sfinterfaces.IEnvironment {
return *shell.environment
}
func (shell *Shell) SetSession(sess sfinterfaces.ISession) {
shell.session = &sess
}
func (shell *Shell) GetSession() sfinterfaces.ISession {
return *shell.session
}
func (shell *Shell) GetVersion() string {
return shell.version
}
func (shell *Shell) GetIn() *os.File {
return shell.in
}
func (shell *Shell) GetOut() *os.File {
return shell.out
}
func (shell *Shell) GetErr() *os.File {
return shell.err
}
//
// SHELL Logging
//
// these function provide logging to the choosen logfile
//
func (shell *Shell) SetLog(logger slinterfaces.ISimpleLogger) {
shell.log = &logger
}
func (shell *Shell) GetLog() *slinterfaces.ISimpleLogger {
return shell.log
}