-
-
Save gesellix/e4eb99faad520496bd612123929718b5 to your computer and use it in GitHub Desktop.
ansible module written in go-lang. This is almost same as http://ansible.cc/docs/moduledev.html#reading-input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"io/ioutil" | |
"strings" | |
"time" | |
"encoding/json" | |
) | |
type Time struct { | |
Time string `json:"time"` // this will be changed to {"time": string} | |
} | |
func main() { | |
// the format is a little bit wired. see http://golang.org/pkg/time/#pkg-constants | |
f := "2006-01-02 15:04:06" | |
data, err:= ioutil.ReadFile(os.Args[1]) | |
if err != nil {panic(err)} | |
arg := strings.Split(string(data), " ")[0] | |
if strings.Contains(arg, "="){ | |
f = strings.Split(arg, "=")[1] | |
} | |
t := time.Now() | |
m := Time{t.Format(f)} // create Time struct with string | |
s, err := json.Marshal(m) // produce JSON from Time struct | |
if err != nil { | |
fmt.Println(err) | |
}else{ | |
fmt.Printf("%s", s) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment