-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
130 lines (120 loc) · 4 KB
/
conf.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
/*
* 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"
"gopkg.in/yaml.v2"
"io/ioutil"
"os/exec"
)
// CheckRequirements check system requirements to execute this tool
func CheckRequirements() bool {
// check if qdbus is available
_, qdbusErr := exec.LookPath("qdbus")
if qdbusErr != nil {
color.Errorf("qdbus command is missing - probably it is not installed.\n")
return false
}
_, yakuakeErr := exec.LookPath("yakuake")
if yakuakeErr != nil {
color.Errorf("yakuake command is missing - probably it is not installed.\n")
return false
}
// ping yakuake
executeCmdVoid(DbusPathSessions, DbusMethodPing)
return true
}
// ReadConfig read configuration and yaml stuff
func ReadConfig(filename string) (*YakCtlConfiguration, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c := &YakCtlConfiguration{}
err = yaml.Unmarshal(buf, c)
if err != nil {
return nil, fmt.Errorf("YAML syntax error in file: %q: %v", filename, err)
}
return c, nil
}
// PrintProfileList prints defined profiles to stdout
func PrintProfileList(configuration *YakCtlConfiguration) {
if len(*configuration.Profiles) > 0 {
color.Info.Printf("#\t\tName\n")
} else {
color.Warn.Printf("No profiles defined!\n")
}
for i, value := range *configuration.Profiles {
color.Info.Printf("%d\t\t%s\n", i+1, value.Name)
}
}
// PrintProfile print a single profile in yaml format
func PrintProfile(configuration *YakCtlConfiguration, number int64) error {
profile, err := GetProfile(configuration, number)
if err != nil {
return err
}
marshal, ymlErr := yaml.Marshal(profile)
if ymlErr != nil {
return fmt.Errorf("problem unmarshalling profile #%d to yaml format")
}
color.Info.Println(string(marshal))
return nil
}
// GetProfile retrieve session struct based on profile number
func GetProfile(configuration *YakCtlConfiguration, number int64) (*ProfileDescription, error) {
err := fmt.Errorf("profile #%d does not exist", number)
if number <= 0 || int(number-1) > len(*configuration.Profiles) || len(*configuration.Profiles) == 0 {
return nil, err
}
for i, v := range *configuration.Profiles {
if int64(i+1) == number {
return &v, nil
}
}
return nil, err
}
// YakCtlConfiguration this is the configuration object, yaml representation as struct
type YakCtlConfiguration struct {
Profiles *[]ProfileDescription `yaml:"profiles"`
}
// ProfileDescription represents a session description
type ProfileDescription struct {
Name string `yaml:"name"`
Tabs []TabDescription `yaml:"tabs"`
ClearAll bool `yaml:"clear,omitempty"`
ForceClear bool `yaml:"force,omitempty"`
}
// TabDescription represents a tab of a yakuake session
type TabDescription struct {
Name string `yaml:"name"`
// optional
Commands []string `yaml:"commands,omitempty"`
SplitMode string `yaml:"split,omitempty"`
Terminal1 []string `yaml:"terminal1,omitempty"`
Terminal2 []string `yaml:"terminal2,omitempty"`
Terminal3 []string `yaml:"terminal3,omitempty"`
Terminal4 []string `yaml:"terminal4,omitempty"`
// flags
Protected bool `yaml:"protected,omitempty"`
MonitorSilence bool `yaml:"monitorSilence,omitempty"`
MonitorActivity bool `yaml:"monitorActivity,omitempty"`
DisableKeyboardInput bool `yaml:"disableInput,omitempty"`
}