forked from linuxdeepin/startdde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch_group.go
66 lines (53 loc) · 1.36 KB
/
launch_group.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
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/linuxdeepin/go-lib/xdg/basedir"
)
type Cmd struct {
Command string `json:"Command"`
Wait bool `json:"Wait"`
Args []string `json:"Args"`
}
type launchGroup struct {
Priority uint32 `json:"Priority"`
Group []Cmd `json:"Group"`
}
type launchGroups []*launchGroup
const (
sysLaunchGroupFile = "/usr/share/startdde/auto_launch.json"
userLaunchGroupFile = "startdde/auto_launch.json"
)
func (infos launchGroups) Len() int {
return len(infos)
}
func (infos launchGroups) Less(i, j int) bool {
return infos[i].Priority > infos[j].Priority
}
func (infos launchGroups) Swap(i, j int) {
infos[i], infos[j] = infos[j], infos[i]
}
func loadGroupFile() (launchGroups, error) {
userFile := filepath.Join(basedir.GetUserConfigDir(), userLaunchGroupFile)
infos, err := doLoadGroupFile(userFile)
if err != nil {
infos, err = doLoadGroupFile(sysLaunchGroupFile)
}
return infos, err
}
func doLoadGroupFile(filename string) (launchGroups, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var infos launchGroups
err = json.Unmarshal(contents, &infos)
if err != nil {
return nil, err
}
return infos, nil
}