Permalink
Browse files

Use spaces for indent instead of tabs

  • Loading branch information...
1 parent 0ab6d96 commit 8321e1316c4760f5ab6813dbb5f489123d76c338 @falconindy committed Aug 14, 2010
Showing with 93 additions and 94 deletions.
  1. +93 −94 ghost.go
View
@@ -1,120 +1,119 @@
package main
import (
- "exec"
- "flag"
- "fmt"
- "io/ioutil"
- "container/vector"
- "os"
+ "exec"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "container/vector"
+ "os"
)
const (
- etcrcd = "/etc/rc.d/"
- varrun = "/var/run/daemons/"
+ etcrcd = "/etc/rc.d/"
+ varrun = "/var/run/daemons/"
)
-var color = map[string] string {
- "red": "\033[1;31m",
- "green": "\033[1;32m",
- "yellow": "\033[1;33m",
- "blue": "\033[1;34m",
- "magenta": "\033[1;35m",
- "cyan": "\033[1;36m",
- "white": "\033[1;37m",
- "none": "\033[0m",
+var color = map[string]string{
+ "red": "\033[1;31m",
+ "green": "\033[1;32m",
+ "yellow": "\033[1;33m",
+ "blue": "\033[1;34m",
+ "magenta": "\033[1;35m",
+ "cyan": "\033[1;36m",
+ "white": "\033[1;37m",
+ "none": "\033[0m",
}
func parseDaemons() (*vector.StringVector, *vector.StringVector, os.Error) {
- files, err := ioutil.ReadDir(etcrcd)
-
- if err != nil {
- return nil, nil, err
- }
-
- running := new(vector.StringVector)
- stopped := new(vector.StringVector)
-
- for _, file := range files {
- if file.Name == "functions" || !file.IsRegular() {
- continue
- }
- if _, err := os.Stat(fmt.Sprintf("%s%s", varrun, file.Name)); err == nil {
- running.Push(file.Name)
- } else {
- stopped.Push(file.Name)
- }
- }
-
- return running, stopped, nil
+ files, err := ioutil.ReadDir(etcrcd)
+
+ if err != nil {
+ return nil, nil, err
+ }
+
+ running := new(vector.StringVector)
+ stopped := new(vector.StringVector)
+
+ for _, file := range files {
+ if file.Name == "functions" || !file.IsRegular() {
+ continue
+ }
+ if _, err := os.Stat(fmt.Sprintf("%s%s", varrun, file.Name)); err == nil {
+ running.Push(file.Name)
+ } else {
+ stopped.Push(file.Name)
+ }
+ }
+
+ return running, stopped, nil
}
func modifyDaemon(argv []string) (int, os.Error) {
- daemon := fmt.Sprintf("%s%s", etcrcd, argv[0])
+ daemon := fmt.Sprintf("%s%s", etcrcd, argv[0])
- p, err := exec.Run(daemon, argv, os.Environ(), etcrcd, 0, 1, 2)
- w, err := p.Wait(0)
+ p, err := exec.Run(daemon, argv, os.Environ(), etcrcd, 0, 1, 2)
+ w, err := p.Wait(0)
- return w.ExitStatus(), err
+ return w.ExitStatus(), err
}
func printDaemonStatus(daemon string, running bool) {
- if running {
- fmt.Printf("%s[RUNNING]\t%s%v%s\n", color["green"], color["blue"], daemon, color["none"])
- } else {
- fmt.Printf("%s[STOPPED]\t%s%v%s\n", color["red"], color["blue"], daemon, color["none"])
- }
+ if running {
+ fmt.Printf("%s[RUNNING]\t%s%v%s\n", color["green"], color["blue"], daemon, color["none"])
+ } else {
+ fmt.Printf("%s[STOPPED]\t%s%v%s\n", color["red"], color["blue"], daemon, color["none"])
+ }
}
-func findInVector (vector *vector.StringVector, needle string) bool {
- for _, v := range *vector {
- if v == needle {
- return true
- }
- }
- return false
+func findInVector(vector *vector.StringVector, needle string) bool {
+ for _, v := range *vector {
+ if v == needle {
+ return true
+ }
+ }
+ return false
}
func main() {
- flag.Parse()
- n := flag.NArg()
-
- running, stopped, err := parseDaemons()
- if err != nil {
- fmt.Fprintf(os.Stderr, "%s\n", err)
- os.Exit(1)
- }
-
- if n > 0 {
- if _, err := os.Stat(fmt.Sprintf("%s%s", etcrcd, flag.Arg(0))); err != nil {
- fmt.Fprintf(os.Stderr, "%s%s%s is not a valid daemon\n", color["red"], flag.Arg(0), color["none"])
- os.Exit(2)
- }
- }
-
- switch {
- case n == 0:
- for _, v := range *stopped {
- printDaemonStatus(v, false)
- }
- for _, v := range *running {
- printDaemonStatus(v, true)
- }
- case n == 1:
- if findInVector(running, flag.Arg(0)) {
- printDaemonStatus(flag.Arg(0), true)
- os.Exit(0)
- }
- if findInVector(stopped, flag.Arg(0)) {
- printDaemonStatus(flag.Arg(0), false)
- os.Exit(0)
- }
- case n > 1:
- ret, err := modifyDaemon(flag.Args())
- if ret > 0 || err != nil {
- fmt.Fprintf(os.Stderr, "%s\n", err.String())
- }
- }
+ flag.Parse()
+ n := flag.NArg()
+
+ running, stopped, err := parseDaemons()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s\n", err)
+ os.Exit(1)
+ }
+
+ if n > 0 {
+ if _, err := os.Stat(fmt.Sprintf("%s%s", etcrcd, flag.Arg(0))); err != nil {
+ fmt.Fprintf(os.Stderr, "%s%s%s is not a valid daemon\n", color["red"], flag.Arg(0), color["none"])
+ os.Exit(2)
+ }
+ }
+
+ switch {
+ case n == 0:
+ for _, v := range *stopped {
+ printDaemonStatus(v, false)
+ }
+ for _, v := range *running {
+ printDaemonStatus(v, true)
+ }
+ case n == 1:
+ if findInVector(running, flag.Arg(0)) {
+ printDaemonStatus(flag.Arg(0), true)
+ os.Exit(0)
+ }
+ if findInVector(stopped, flag.Arg(0)) {
+ printDaemonStatus(flag.Arg(0), false)
+ os.Exit(0)
+ }
+ case n > 1:
+ ret, err := modifyDaemon(flag.Args())
+ if ret > 0 || err != nil {
+ fmt.Fprintf(os.Stderr, "%s\n", err.String())
+ }
+ }
}
-// vim: noet

0 comments on commit 8321e13

Please sign in to comment.