This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
141 lines (120 loc) · 3.86 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"github.com/rs/zerolog/log"
"net/http"
"os"
cmd_build "github.com/puppetlabs/pct/cmd/build"
"github.com/puppetlabs/pct/pkg/build"
"github.com/puppetlabs/pct/pkg/exec_runner"
"github.com/puppetlabs/pct/pkg/gzip"
"github.com/puppetlabs/pct/pkg/install"
"github.com/puppetlabs/pct/pkg/tar"
"github.com/puppetlabs/pct/pkg/telemetry"
"github.com/puppetlabs/prm/cmd/exec"
"github.com/puppetlabs/prm/cmd/explain"
"github.com/puppetlabs/prm/cmd/get"
cmd_install "github.com/puppetlabs/prm/cmd/install"
"github.com/puppetlabs/prm/cmd/root"
"github.com/puppetlabs/prm/cmd/set"
"github.com/puppetlabs/prm/cmd/status"
"github.com/puppetlabs/prm/cmd/validate"
appver "github.com/puppetlabs/prm/cmd/version"
"github.com/puppetlabs/prm/internal/pkg/config_processor"
"github.com/puppetlabs/prm/pkg/prm"
"github.com/puppetlabs/prm/pkg/utils"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
var (
version = "dev"
commit = "none"
date = "unknown"
honeycomb_api_key = "not_set"
honeycomb_dataset = "not_set"
)
func main() {
// Telemetry must be initialized before anything else;
// If the telemetry build tag was not passed, this is all null ops
ctx, traceProvider, parentSpan := telemetry.Start(context.Background(), honeycomb_api_key, honeycomb_dataset, "prm")
// Create PRM context
fs := afero.NewOsFs() // configure afero to use real filesystem
prmApi := &prm.Prm{
AFS: &afero.Afero{Fs: fs},
IOFS: &afero.IOFS{Fs: fs},
}
var rootCmd = root.CreateRootCommand(prmApi)
// Get the command called and its arguments;
// The arguments are only necessary if we want to
// hand them off as an attribute to the parent span:
// do we? Otherwise we just need the calledCommand
calledCommand, calledCommandArguments := root.GetCalledCommand(rootCmd)
telemetry.AddStringSpanAttribute(parentSpan, "arguments", calledCommandArguments)
var verCmd = appver.CreateVersionCommand(version, date, commit)
v := appver.Format(version, date, commit)
rootCmd.Version = v
rootCmd.SetVersionTemplate(v)
rootCmd.AddCommand(verCmd)
// set command
sc := set.SetCommand{Utils: &utils.Utils{}}
rootCmd.AddCommand(sc.CreateSetCommand())
// get command
rootCmd.AddCommand(get.CreateGetCommand(prmApi))
// exec command
rootCmd.AddCommand(exec.CreateCommand(prmApi))
// validate command
rootCmd.AddCommand(validate.CreateCommand(prmApi))
// status command
rootCmd.AddCommand(status.CreateStatusCommand(prmApi))
// build
buildCmd := cmd_build.BuildCommand{
ProjectType: "tool",
Builder: &build.Builder{
Tar: &tar.Tar{AFS: prmApi.AFS},
Gzip: &gzip.Gzip{AFS: prmApi.AFS},
AFS: prmApi.AFS,
ConfigProcessor: &config_processor.ConfigProcessor{
AFS: prmApi.AFS,
},
ConfigFile: "prm-config.yml",
},
}
rootCmd.AddCommand(buildCmd.CreateCommand())
// install command
installCmd := cmd_install.InstallCommand{
PrmInstaller: &install.Installer{
Tar: &tar.Tar{AFS: prmApi.AFS},
Gunzip: &gzip.Gunzip{AFS: prmApi.AFS},
AFS: prmApi.AFS,
IOFS: prmApi.IOFS,
HTTPClient: &http.Client{},
Exec: &exec_runner.Exec{},
ConfigProcessor: &config_processor.ConfigProcessor{
AFS: prmApi.AFS,
},
ConfigFileName: "prm-config.yml",
},
AFS: prmApi.AFS,
}
rootCmd.AddCommand(installCmd.CreateCommand())
// explain
rootCmd.AddCommand(explain.CreateCommand())
// initialize
cobra.OnInitialize(root.InitLogger, root.InitConfig)
// instrument & execute called command
ctx, childSpan := telemetry.NewSpan(ctx, calledCommand)
err := rootCmd.ExecuteContext(ctx)
telemetry.RecordSpanError(childSpan, err)
telemetry.EndSpan(childSpan)
// Send all events
telemetry.ShutDown(ctx, traceProvider, parentSpan)
// Handle exiting with/out errors.
//cobra.CheckErr(err)
checkErr(err)
}
func checkErr(err error) {
if err != nil {
log.Error().Msg(err.Error())
os.Exit(1)
}
}