-
Notifications
You must be signed in to change notification settings - Fork 0
/
yakctl.go
224 lines (216 loc) · 6.23 KB
/
yakctl.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* yakctl - control the yakuake terminal
*
* 2020 emschu https://github.com/emschu/yakctl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"github.com/gookit/color"
"github.com/urfave/cli/v2"
"os"
"path"
"strconv"
"strings"
)
func main() {
var configFile = ".yakctl.yml"
var configFilePath string
var configuration *YakCtlConfiguration
var verbose bool
var forceDeletion bool
hd, homeDirErr := os.UserHomeDir()
if homeDirErr != nil {
color.Errorf("%v", homeDirErr)
return
}
configFilePath = path.Join(hd, configFile)
app := &cli.App{
EnableBashCompletion: true,
Name: "yakctl",
Usage: "Control the yakuake terminal and your terminal sessions",
Version: "1.0.0",
HideHelp: false,
HideVersion: false,
Copyright: "yakctl\t2020\thttps://github.com/emschu/yakctl\n\n" +
" This program comes with ABSOLUTELY NO WARRANTY.\n" +
" This is free software, and you are welcome\n" +
" to redistribute it under the conditions of the\n" +
" GPLv3 (https://www.gnu.org/licenses/gpl-3.0.txt).",
Before: func(context *cli.Context) error {
// general startup logic
if verbose {
fmt.Printf("Using configuration file at: '%s'\n", configFilePath)
}
configuration = initApplication(&configFilePath)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "configuration file",
Value: path.Join(hd, ".yakctl.yml"),
Destination: &configFilePath,
},
&cli.BoolFlag{
Name: "verbose",
Usage: "verbose log output",
Value: false,
Destination: &verbose,
},
},
Commands: []*cli.Command{
{
Name: "clear",
Aliases: []string{"c"},
Usage: "Clear all sessions and terminals",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Usage: "force deletion of ALL tabs",
Value: false,
Destination: &forceDeletion,
},
},
Action: func(context *cli.Context) error {
ClearSession(forceDeletion)
return nil
},
},
{
Name: "profile",
Aliases: []string{"p"},
Usage: "Manage defined profiles, default: list available profiles",
Action: func(context *cli.Context) error {
PrintProfileList(configuration)
return nil
},
Subcommands: []*cli.Command{
{
Name: "show",
Aliases: []string{"s"},
Usage: "Shows all details of a defined profile",
ArgsUsage: "profile_id",
Action: func(context *cli.Context) error {
profileID, done, _ := getProfileID(context)
if done {
return fmt.Errorf("empty profile_id")
}
profilePrintErr := PrintProfile(configuration, profileID)
if profilePrintErr != nil {
color.Errorf("%v\n", profilePrintErr)
}
return nil
},
},
{
Name: "open",
Aliases: []string{"o"},
Usage: "Opens a defined profile",
ArgsUsage: "profile_id",
Action: func(context *cli.Context) error {
profileID, done, err := getProfileID(context)
if done {
return err
}
if verbose {
profilePrintErr := PrintProfile(configuration, profileID)
if profilePrintErr != nil {
fmt.Printf("%v\n", profilePrintErr)
}
}
err2 := LoadSession(configuration, profileID)
if err2 != nil {
return err
}
return nil
},
},
},
},
{
Name: "exec",
Aliases: []string{"e"},
Usage: "Execute a command in all or specific terminals",
ArgsUsage: "command to be executed in all or specified terminals",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "terminal",
Aliases: []string{"t"},
Usage: "list Ids of terminals separated by comma and without space",
},
},
Action: func(context *cli.Context) error {
command := strings.Join(context.Args().Slice(), " ")
if len(command) == 0 {
color.Error.Printf("Invalid empty command input detected\n")
return nil
}
color.Info.Printf("Execute '%s' in all terminals\n", command)
terminalIDInput := strings.Trim(context.String("terminal"), " ")
var terminalIDs []string
if len(terminalIDInput) > 0 {
terminalIDs = strings.Split(terminalIDInput, ",")
}
if len(terminalIDs) > 0 {
color.Info.Printf("Affected terminal ids: %v\n", strings.Join(terminalIDs, ","))
}
ExecuteCommand(command, &terminalIDs)
return nil
},
},
{
Name: "status",
Aliases: []string{"s"},
Usage: "List status (=sessions, terminals) of the current yakuake instance",
Action: func(context *cli.Context) error {
ShowStatus()
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
color.Errorf("%v\n", err)
}
}
// get profile_id out of cli arguments
func getProfileID(context *cli.Context) (int64, bool, error) {
if 0 == context.Args().Len() {
return 0, true, fmt.Errorf("missing argument 'profile_id'")
}
profileID, err := strconv.ParseInt(context.Args().First(), 10, 32)
if err != nil {
color.Errorf("Invalid argument 'profile_id'\n")
return 0, true, err
}
return profileID, false, nil
}
// method to handle startup of the application
func initApplication(configFile *string) *YakCtlConfiguration {
isValid := CheckRequirements()
if !isValid {
color.Errorf("Problems detected. yakctl is unable to start.\n")
os.Exit(1)
}
c, confErr := ReadConfig(*configFile)
if confErr != nil {
color.Errorf("Invalid configuration detected in configuration file '%s'\n%v\n", *configFile, confErr)
os.Exit(1)
}
return c
}