This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
137 lines (111 loc) · 3.11 KB
/
screen.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
/*
* screend
* Copyright (C) 2019 OkaeriPoland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"github.com/pkg/errors"
"strconv"
"strings"
"time"
)
type Screen struct {
id int
name string
}
func runScreen(userName string, screenName string, runDirectory string, command string, logging bool) (Screen, error) {
exists, err := doesScreenExists(userName, screenName)
if err != nil {
return Screen{}, err
}
if exists {
return Screen{}, errors.New("SCREEN_ALREADY_EXISTS")
}
args := []string{"screen"}
if logging {
args = append(args, "-L")
}
args = append(args, "-S", screenName, "-dm")
args = append(args, strings.Split(command, " ")...)
userInfo, err := getUserInfoByName(userName)
if err != nil {
return Screen{}, err
}
_, err = executeCommand(userInfo, false, runDirectory, args...)
if err != nil {
if err.Error() == "exit status 1" {
return Screen{}, errors.New("exit status 1: check command")
}
return Screen{}, err
}
time.Sleep(time.Second)
screen, err := getScreenByName(userName, screenName)
if err != nil {
if err.Error() == "SCREEN_NOT_FOUND" {
return Screen{}, errors.New("Screen " + screenName + " not present after creation")
}
return Screen{}, err
}
return screen, nil
}
func getRunningScreens(userName string) ([]Screen, error) {
userInfo, err := getUserInfoByName(userName)
if err != nil {
return []Screen{}, err
}
output, _ := executeCommand(userInfo, true, "", "screen", "-ls")
var screens []Screen
lines := strings.Split(output, "\n")
for _, line := range lines {
if !strings.HasPrefix(line, "\t") {
continue
}
lineParts := strings.Split(line, "\t")
fullScreenName := lineParts[1]
fullNameParts := strings.Split(fullScreenName, ".")
name := strings.Join(fullNameParts[1:], ".")
id, err := strconv.Atoi(fullNameParts[0])
if err != nil {
return []Screen{}, err
}
screens = append(screens, Screen{id, name})
}
return screens, nil
}
func doesScreenExists(userName string, screenName string) (bool, error) {
_, err := getScreenByName(userName, screenName)
if err != nil {
if err.Error() == "SCREEN_NOT_FOUND" {
return false, nil
}
return false, err
}
return true, nil
}
func getScreenByName(userName string, screenName string) (Screen, error) {
screens, err := getRunningScreens(userName)
if err != nil {
return Screen{}, err
}
for _, screen := range screens {
if screen.name != screenName {
continue
}
return screen, nil
}
return Screen{}, errors.New("SCREEN_NOT_FOUND")
}