forked from nocalhost/nocalhost
/
render.go
76 lines (65 loc) · 2.1 KB
/
render.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package cmds
import (
"fmt"
"github.com/lsutils/nocalhost/internal/nhctl/envsubst"
"github.com/lsutils/nocalhost/internal/nhctl/fp"
"github.com/lsutils/nocalhost/internal/nhctl/profile"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
customyaml3 "github.com/lsutils/nocalhost/pkg/nhctl/utils/custom_yaml_v3"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var renderOps = &RenderOps{}
type RenderOps struct {
envPath string
origin bool
free bool
}
func init() {
renderCmd.Flags().StringVarP(&renderOps.envPath, "env path", "e", "", "the env file for render injection")
renderCmd.Flags().BoolVar(&renderOps.origin, "origin", false, "return the origin result after rendered")
renderCmd.Flags().BoolVar(&renderOps.free, "free", false, "render the file other than nocalhost config.yaml")
rootCmd.AddCommand(renderCmd)
}
var renderCmd = &cobra.Command{
Use: "render [NAME]",
Short: "Render the file for debugging",
Long: `Render the nocalhost config.yaml file for debugging`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.Errorf("%q requires at least 1 argument\n", cmd.CommandPath())
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var renderTar interface{}
if renderOps.free {
renderTar = map[string]interface{}{}
} else {
renderTar = &profile.NocalHostAppConfigV2{}
}
render, err := envsubst.Render(
envsubst.LocalFileRenderItem{FilePathEnhance: fp.NewFilePath(args[0])},
fp.NewFilePath(renderOps.envPath),
)
must(errors.Wrap(err, ""))
if renderOps.origin {
fmt.Print(render)
} else {
err = customyaml3.Unmarshal([]byte(render), renderTar)
if v, ok := renderTar.(map[string]interface{}); (ok && len(v) == 0) || err != nil {
log.Fatalf(
"%s\n\n======\n\nRender Error: %s, Please check the render result above", render,
err.Error(),
)
} else {
marshal, _ := customyaml3.Marshal(renderTar)
fmt.Print(string(marshal))
}
}
},
}