-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.go
124 lines (108 loc) · 2.66 KB
/
Main.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
// Copyright (c) Christian Surlykke
//
// This file is part of the WindowArranger project.
// It is distributed under the GPL v2 license.
// Please refer to the GPL2 file for a copy of the license.
//
package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
)
func main() {
defer func() {
if cause := recover(); cause != nil {
fmt.Fprintln(os.Stderr, cause)
os.Exit(1)
}
}()
var dumpFile, waitSeconds, configFilePath = getCliArgs()
var bytes = getConfig(configFilePath)
var scriptFile = getScriptFile()
build(scriptFile, bytes, waitSeconds)
if err := scriptFile.Close(); err != nil{
panic(err)
}
defer os.Remove(scriptFile.Name())
if dumpFile == "" {
run(scriptFile.Name())
} else {
dump(scriptFile.Name(), dumpFile)
}
}
func getCliArgs() (dumpFile string, wait uint, configFilePath string) {
flag.Usage = func() {
var out = flag.CommandLine.Output()
fmt.Fprintln(out, "usage:")
fmt.Fprintln(out, " WindowWrapper [option]... [configfile]")
fmt.Fprintln(out, " If configfile is not given, configuration will be read from standard input")
fmt.Fprintln(out, "options:")
flag.PrintDefaults()
}
var df = flag.String("dump", "", "Dont execute generated script, but write it to a file. '-' means standard out.")
var ws = flag.Uint("wait", 0, "Wait <uint seconds> for all criteria in config to match a window")
flag.Parse()
if len(flag.Args()) > 1 {
flag.Usage()
os.Exit(1)
}
if len(flag.Args()) == 1 {
return *df, *ws, flag.Args()[0]
} else {
return *df, *ws, ""
}
}
func getConfig(configFilePath string) []byte {
var (
bytes []byte
err error
)
if configFilePath == "" {
bytes, err = io.ReadAll(os.Stdin)
} else {
bytes, err = os.ReadFile(configFilePath)
}
if err != nil {
panic(err)
}
return bytes
}
func getScriptFile() *os.File {
if f, err := os.CreateTemp("", "disp_config*.sh"); err != nil {
panic(err)
} else {
return f
}
}
func build(scriptFile *os.File, bytes []byte, waitSeconds uint) {
var workSpaces = Parse(bytes)
for _, line := range Generate(workSpaces, waitSeconds) {
if _, err := fmt.Fprintln(scriptFile, line); err != nil {
panic(err)
}
}
}
func run(scriptFilePath string) {
if err := os.Chmod(scriptFilePath, 0744); err != nil {
panic(err)
} else {
var cmd = exec.Command(scriptFilePath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
os.Exit(1)
}
}
}
func dump(scriptFilePath string, dumpFilePath string) {
if prog, err := os.ReadFile(scriptFilePath); err != nil {
panic(err)
} else if dumpFilePath == "-" {
fmt.Println(string(prog))
} else if err := os.WriteFile(dumpFilePath, prog, 0744); err != nil {
panic(err)
}
}