/
filegather.go
55 lines (45 loc) · 1.26 KB
/
filegather.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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-facts-app/gatherers"
)
var gatheredFileName string
// gatherCmd represents the gather command
var filegatherCmd = &cobra.Command{
Use: "filegather",
Short: "Running this command will invoke only invoke the filesystem gatherer",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
var facts []gatherers.GatheredFact
//we can just unmarshall the file data into the facts ...
if gatheredFileName == "" {
fmt.Errorf("Filename should be passed as argument")
os.Exit(1)
}
file, _ := ioutil.ReadFile(gatheredFileName)
_ = json.Unmarshal([]byte(file), &facts)
if !dryRun {
err := gatherers.Writefacts(projectName, environmentName, facts)
if err != nil {
log.Println(err.Error())
}
}
if dryRun {
if facts != nil {
log.Println("---- Dry run ----")
log.Printf("Would post the follow facts to '%s:%s'", projectName, environmentName)
s, _ := json.MarshalIndent(facts, "", "\t")
log.Println(string(s))
}
}
},
}
func init() {
filegatherCmd.PersistentFlags().StringVarP(&gatheredFileName, "file-name", "f", "", "The file containing the gathered facts")
gatherCmd.AddCommand(filegatherCmd)
}