Debian Codegraph @

test/integration/targets/binary_modules/library/helloworld.go

raw
1 // This file is part of Ansible
2 //
3 // Ansible is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Ansible is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19 "encoding/json"
20 "fmt"
21 "io/ioutil"
22 "os"
23 )
24
25 type ModuleArgs struct {
26 Name string
27 }
28
29 type Response struct {
30 Msg string `json:"msg"`
31 Changed bool `json:"changed"`
32 Failed bool `json:"failed"`
33 }
34
35 func ExitJson(responseBody Response) {
36 returnResponse(responseBody)
37 }
38
39 func FailJson(responseBody Response) {
40 responseBody.Failed = true
41 returnResponse(responseBody)
42 }
43
44 func returnResponse(responseBody Response) {
45 var response []byte
46 var err error
47 response, err = json.Marshal(responseBody)
48 if err != nil {
49 response, _ = json.Marshal(Response{Msg: "Invalid response object"})
50 }
51 fmt.Println(string(response))
52 if responseBody.Failed {
53 os.Exit(1)
54 } else {
55 os.Exit(0)
56 }
57 }
58
59 func main() {
60 var response Response
61
62 if len(os.Args) != 2 {
63 response.Msg = "No argument file provided"
64 FailJson(response)
65 }
66
67 argsFile := os.Args[1]
68
69 text, err := ioutil.ReadFile(argsFile)
70 if err != nil {
71 response.Msg = "Could not read configuration file: " + argsFile
72 FailJson(response)
73 }
74
75 var moduleArgs ModuleArgs
76 err = json.Unmarshal(text, &moduleArgs)
77 if err != nil {
78 response.Msg = "Configuration file not valid JSON: " + argsFile
79 FailJson(response)
80 }
81
82 var name string = "World"
83 if moduleArgs.Name != "" {
84 name = moduleArgs.Name
85 }
86
87 response.Msg = "Hello, " + name + "!"
88 ExitJson(response)
89 }