/
output.go
54 lines (46 loc) · 1.17 KB
/
output.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
package cmd
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/hokaccha/go-prettyjson"
"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/config"
"github.com/spf13/cobra"
)
var outputCmd = &cobra.Command{
Use: "output",
Short: "Show the output variables",
Long: `Show the output variables`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// load the stack
cfg, err := config.LoadState()
if err != nil {
cmd.Println("Error: Unable to load state, ", err)
os.Exit(1)
}
out := map[string]interface{}{}
// get the output variables
for _, r := range cfg.Resources {
if r.Metadata().ResourceType == types.TypeOutput {
// don't output when disabled
if r.Metadata().Disabled {
continue
}
if r.Metadata().ResourceModule != "" {
continue
}
out[r.Metadata().ResourceName] = r.(*types.Output).Value
if len(args) > 0 && strings.EqualFold(args[0], r.Metadata().ResourceName) {
d, _ := json.Marshal(r.(*types.Output).Value)
fmt.Printf("%s", string(d))
return
}
}
}
d, _ := prettyjson.Marshal(out)
fmt.Printf("%s", string(d))
},
}