-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
110 lines (91 loc) · 2.33 KB
/
hello.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
package main
import (
"flag"
"fmt"
"io"
"log"
"log/syslog"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
//#import "github.com/ismadeandres/goini"
func main() {
//path search
flag.Parse()
root := flag.Arg(0)
err3 := filepath.Walk(root, visit)
fmt.Printf("filepath.Walk() returned %v\n", err3)
// Now from anywhere else in your program, you can use this:
log.Print("Hello Logs!")
//cmdRun
var (
cmdOut []byte
err error
)
fmt.Println("Hello, new gopher!")
cmd := "ls"
args := []string{"-l", "-a"}
if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil {
fmt.Println(err)
fmt.Println("Ha habido algun error")
os.Exit(1)
}
fmt.Println(string(cmdOut))
//# fini,_ := ini.Load("/tmp/example.ini")
//#fmt.Println( ini.GetString("Wine","Grape" ) )
cmd2 := exec.Command("ls", "/tmp")
var waitStatus syscall.WaitStatus
if err := cmd2.Run(); err != nil {
printError(err)
// Did the command fail because of an unsuccessful exit code
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
printOutput([]byte(fmt.Sprintf("%d", waitStatus.ExitStatus())))
}
} else {
// Command was successful
waitStatus = cmd2.ProcessState.Sys().(syscall.WaitStatus)
printOutput([]byte(fmt.Sprintf("%d", waitStatus.ExitStatus())))
}
}
func init() {
f, err := os.OpenFile("/var/log/hello.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Println("This is a test log entry")
//log2Syslog
logwriter, e := syslog.New(syslog.LOG_NOTICE, "hello")
if e == nil {
log.SetOutput(logwriter)
log.Print("Initialization Complete!")
}
mWriter := io.MultiWriter(f, logwriter)
data := []byte("Hello World MultiWriter!")
n, err := mWriter.Write(data)
if err == nil {
fmt.Printf("Multi write %d bytes to two files simultaneously.\n", n)
}
}
func printCommand(cmd *exec.Cmd) {
fmt.Printf("==> Executing: %s\n", strings.Join(cmd.Args, " "))
}
func printError(err error) {
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("==> Error: %s\n", err.Error()))
}
}
func printOutput(outs []byte) {
if len(outs) > 0 {
fmt.Printf("==> Output: %s\n", string(outs))
}
}
func visit(path string, f os.FileInfo, err error) error {
fmt.Printf("Visited: %s\n", path)
return nil
}