-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
95 lines (77 loc) · 2.59 KB
/
session.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
package shellframework
import (
"math/rand"
"time"
"github.com/eshu0/shellframework/interfaces"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
type Session struct {
sfinterfaces.ISession
id string
//interactive bool
shell sfinterfaces.IShell
builidmethod func(ss sfinterfaces.ISession, shell sfinterfaces.IShell)
interactiveMethod func(ss sfinterfaces.ISession) bool
}
func DefaultBuildIDMethod(ss sfinterfaces.ISession, shell sfinterfaces.IShell) {
if(ss.ID() == ""){
rand.Seed(time.Now().UnixNano())
b := make([]rune, 10)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
ss.SetID(string(b))
}
}
func DefaultNonInteractiveMethod(ss sfinterfaces.ISession) bool {
// ss.interactive = false
// return ss.interactive
return false
}
func DefaultInteractiveMethod(ss sfinterfaces.ISession) bool {
// ss.interactive = true
// return ss.interactive
return true
}
// this is a very simple random string
// this is just to uniquely identify each session
// this is meant to be over written if needs be
func NewSession(buildidmethod func(ss sfinterfaces.ISession, shell sfinterfaces.IShell), interactiveMethod func(ss sfinterfaces.ISession) bool ) sfinterfaces.ISession {
ss := new(Session)
//ss.interactive = false
ss.SetInteractiveMethod(interactiveMethod)
ss.SetBuildIDMethod(buildidmethod)
return ss
}
func NewDefaultInteractiveSession(buildidmethod func(ss sfinterfaces.ISession, shell sfinterfaces.IShell)) sfinterfaces.ISession {
ss := NewSession(buildidmethod, DefaultInteractiveMethod)
return ss
}
func NewDefaultNonInteractiveSession(buildidmethod func(ss sfinterfaces.ISession, shell sfinterfaces.IShell)) sfinterfaces.ISession {
ss := NewSession(buildidmethod, DefaultNonInteractiveMethod)
return ss
}
func (session *Session) SetShell(shell sfinterfaces.IShell) {
session.shell = shell
}
func (session *Session) GetShell() sfinterfaces.IShell {
return session.shell
}
func (session *Session) SetInteractiveMethod(interactiveMethod func(ss sfinterfaces.ISession) bool) {
session.interactiveMethod = interactiveMethod
}
func (session *Session) GetInteractiveMethod() func(ss sfinterfaces.ISession) bool {
return session.interactiveMethod
}
func (session *Session) SetBuildIDMethod(builidmethod func(ss sfinterfaces.ISession, shell sfinterfaces.IShell)) {
session.builidmethod = builidmethod
}
func (session *Session) CallBuildIDMethod(shell sfinterfaces.IShell) {
session.builidmethod(session, shell)
}
func (session *Session) ID() string {
return session.id
}
func (session *Session) SetID(id string) {
session.id = id
}