-
Notifications
You must be signed in to change notification settings - Fork 1
/
buraq.go
105 lines (83 loc) · 1.68 KB
/
buraq.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
package main
/*
// Author: Aliasgar Khimani (NovusEdge)
// Project: github.com/NovusEdge/buraq
//
// Copyright: GNU General Public License v3.0
// See the LICENSE file for more info.
//
// All Rights Reserved
*/
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
buraqlib "github.com/NovusEdge/buraq/buraqlib"
src "github.com/NovusEdge/buraq/src"
)
// ENV is the environment variables for the program
var ENV = buraqlib.GetEnv()
func main() {
checkCommand(os.Args)
executeCommand(os.Args)
}
func checkCommand(args []string) {
var ok bool
if len(args) < 2 {
help()
os.Exit(0)
}
cmd := args[1]
for _, c := range src.ValidCommands() {
if cmd == c {
ok = true
}
}
if !ok {
fmt.Println(buraqlib.ColorIt(buraqlib.ColorRed, "[E]: Invalid command!\nUse \"buraq help\" to show the usage for buraq."))
os.Exit(1)
}
}
func executeCommand(args []string) {
var cmd *exec.Cmd
// var out bytes.Buffer
command := args[1]
commandString := filepath.Join(ENV["BURAQCMDBIN"], command)
if len(args) > 1 {
cmd = exec.Command(commandString, args[2:]...)
} else {
cmd = exec.Command(commandString, args...)
}
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// if s := out.String(); s != "" {
// fmt.Println(s)
// }
}
func env() {
var out bytes.Buffer
cmd := exec.Command(fmt.Sprintf("%s/env", ENV["BURAQCMDBIN"]))
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println(out.String())
}
func help() {
var out bytes.Buffer
cmd := exec.Command(fmt.Sprintf("%s/help", ENV["BURAQCMDBIN"]))
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println(out.String())
}