Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Forked from shirou/main.go
Created September 22, 2015 14:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alfonsodev/d7cb546f462b9ca9f95a 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
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