Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (101 sloc) 2.568 kb
fe79f56 @falconindy initial commit
authored
1 package main
2
3 import (
8321e13 @falconindy Use spaces for indent instead of tabs
authored
4 "exec"
5 "flag"
6 "fmt"
7 "io/ioutil"
8 "container/vector"
9 "os"
fe79f56 @falconindy initial commit
authored
10 )
11
12 const (
8321e13 @falconindy Use spaces for indent instead of tabs
authored
13 etcrcd = "/etc/rc.d/"
14 varrun = "/var/run/daemons/"
fe79f56 @falconindy initial commit
authored
15 )
16
8321e13 @falconindy Use spaces for indent instead of tabs
authored
17 var color = map[string]string{
18 "red": "\033[1;31m",
19 "green": "\033[1;32m",
20 "yellow": "\033[1;33m",
21 "blue": "\033[1;34m",
22 "magenta": "\033[1;35m",
23 "cyan": "\033[1;36m",
24 "white": "\033[1;37m",
25 "none": "\033[0m",
fe79f56 @falconindy initial commit
authored
26 }
27
fdf4839 @falconindy use vectors to store daemon info
authored
28 func parseDaemons() (*vector.StringVector, *vector.StringVector, os.Error) {
8321e13 @falconindy Use spaces for indent instead of tabs
authored
29 files, err := ioutil.ReadDir(etcrcd)
30
31 if err != nil {
32 return nil, nil, err
33 }
34
35 running := new(vector.StringVector)
36 stopped := new(vector.StringVector)
37
38 for _, file := range files {
39 if file.Name == "functions" || !file.IsRegular() {
40 continue
41 }
42 if _, err := os.Stat(fmt.Sprintf("%s%s", varrun, file.Name)); err == nil {
43 running.Push(file.Name)
44 } else {
45 stopped.Push(file.Name)
46 }
47 }
48
49 return running, stopped, nil
fe79f56 @falconindy initial commit
authored
50 }
51
52 func modifyDaemon(argv []string) (int, os.Error) {
8321e13 @falconindy Use spaces for indent instead of tabs
authored
53 daemon := fmt.Sprintf("%s%s", etcrcd, argv[0])
fe79f56 @falconindy initial commit
authored
54
8321e13 @falconindy Use spaces for indent instead of tabs
authored
55 p, err := exec.Run(daemon, argv, os.Environ(), etcrcd, 0, 1, 2)
56 w, err := p.Wait(0)
fe79f56 @falconindy initial commit
authored
57
8321e13 @falconindy Use spaces for indent instead of tabs
authored
58 return w.ExitStatus(), err
fe79f56 @falconindy initial commit
authored
59 }
60
0ab6d96 @falconindy use a function to find in a vector
authored
61 func printDaemonStatus(daemon string, running bool) {
8321e13 @falconindy Use spaces for indent instead of tabs
authored
62 if running {
63 fmt.Printf("%s[RUNNING]\t%s%v%s\n", color["green"], color["blue"], daemon, color["none"])
64 } else {
65 fmt.Printf("%s[STOPPED]\t%s%v%s\n", color["red"], color["blue"], daemon, color["none"])
66 }
fe79f56 @falconindy initial commit
authored
67 }
68
8321e13 @falconindy Use spaces for indent instead of tabs
authored
69 func findInVector(vector *vector.StringVector, needle string) bool {
70 for _, v := range *vector {
71 if v == needle {
72 return true
73 }
74 }
75 return false
0ab6d96 @falconindy use a function to find in a vector
authored
76 }
77
fe79f56 @falconindy initial commit
authored
78 func main() {
8321e13 @falconindy Use spaces for indent instead of tabs
authored
79 flag.Parse()
80 n := flag.NArg()
81
82 running, stopped, err := parseDaemons()
83 if err != nil {
84 fmt.Fprintf(os.Stderr, "%s\n", err)
85 os.Exit(1)
86 }
87
88 if n > 0 {
89 if _, err := os.Stat(fmt.Sprintf("%s%s", etcrcd, flag.Arg(0))); err != nil {
90 fmt.Fprintf(os.Stderr, "%s%s%s is not a valid daemon\n", color["red"], flag.Arg(0), color["none"])
91 os.Exit(2)
92 }
93 }
94
95 switch {
96 case n == 0:
97 for _, v := range *stopped {
98 printDaemonStatus(v, false)
99 }
100 for _, v := range *running {
101 printDaemonStatus(v, true)
102 }
103 case n == 1:
104 if findInVector(running, flag.Arg(0)) {
105 printDaemonStatus(flag.Arg(0), true)
106 os.Exit(0)
107 }
108 if findInVector(stopped, flag.Arg(0)) {
109 printDaemonStatus(flag.Arg(0), false)
110 os.Exit(0)
111 }
112 case n > 1:
113 ret, err := modifyDaemon(flag.Args())
114 if ret > 0 || err != nil {
115 fmt.Fprintf(os.Stderr, "%s\n", err.String())
116 }
117 }
fe79f56 @falconindy initial commit
authored
118 }
119
Something went wrong with that request. Please try again.