-
Notifications
You must be signed in to change notification settings - Fork 46
/
info.go
155 lines (123 loc) · 3.86 KB
/
info.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cli
import (
"fmt"
"github.com/hashicorp/nomad-pack/internal/pkg/cache"
"github.com/hashicorp/nomad-pack/internal/pkg/flag"
"github.com/hashicorp/nomad-pack/internal/pkg/loader"
"github.com/hashicorp/nomad-pack/internal/pkg/variable/parser"
"github.com/hashicorp/nomad-pack/internal/pkg/variable/parser/config"
"github.com/mitchellh/go-glint"
"github.com/zclconf/go-cty/cty"
)
type InfoCommand struct {
*baseCommand
packConfig *cache.PackConfig
}
func (c *InfoCommand) Run(args []string) int {
c.cmdKey = "info" // Add cmdKey here to print out helpUsageMessage on Init error
// Initialize. If we fail, we just exit since Init handles the UI.
if err := c.Init(
WithExactArgs(1, args),
WithFlags(c.Flags()),
WithNoConfig(),
); err != nil {
c.ui.ErrorWithContext(err, ErrParsingArgsOrFlags)
c.ui.Info(c.helpUsageMessage())
return 1
}
c.packConfig.Name = c.args[0]
// Set the packConfig defaults if necessary and generate our UI error context.
errorContext := initPackCommand(c.packConfig)
// verify packs exist before running jobs
if err := cache.VerifyPackExists(c.packConfig, errorContext, c.ui); err != nil {
return 1
}
packPath := c.packConfig.Path
p, err := loader.Load(packPath)
if err != nil {
c.ui.ErrorWithContext(err, "failed to load pack from local directory", errorContext.GetAll()...)
return 1
}
variableParser, err := parser.NewParser(&config.ParserConfig{
ParentPack: p,
RootVariableFiles: p.RootVariableFiles(),
IgnoreMissingVars: c.baseCommand.ignoreMissingVars,
})
if err != nil {
return 1
}
parsedVars, diags := variableParser.Parse()
if diags != nil && diags.HasErrors() {
c.ui.Info(diags.Error())
return 1
}
// Create a new glint document to handle the outputting of information.
doc := glint.New()
doc.Append(glint.Layout(
glint.Style(glint.Text("Pack Name "), glint.Bold()),
glint.Text(p.Metadata.Pack.Name),
).Row())
doc.Append(glint.Layout(
glint.Style(glint.Text("Description "), glint.Bold()),
glint.Text(p.Metadata.Pack.Description),
).Row())
doc.Append(glint.Layout(
glint.Style(glint.Text("Application URL "), glint.Bold()),
glint.Text(p.Metadata.App.URL),
).Row())
for pName, variables := range parsedVars.GetVars() {
doc.Append(glint.Layout(
glint.Style(glint.Text(fmt.Sprintf("Pack %q Variables:", pName)), glint.Bold()),
).Row())
for _, v := range variables {
varType := "unknown"
if !v.Type.Equals(cty.NilType) {
varType = v.Type.FriendlyName()
}
doc.Append(glint.Layout(
glint.Style(glint.Text(fmt.Sprintf("\t- %q (%s) - %s",
v.Name, varType, v.Description))),
).Row())
}
glint.Text("\n")
}
doc.RenderFrame()
return 0
}
func (c *InfoCommand) Flags() *flag.Sets {
return c.flagSet(flagSetOperation, func(set *flag.Sets) {
c.packConfig = &cache.PackConfig{}
f := set.NewSet("Render Options")
f.StringVar(&flag.StringVar{
Name: "registry",
Target: &c.packConfig.Registry,
Default: "",
Usage: `Specific registry name containing the pack to retrieve info
about. If not specified, the default registry will be used.`,
})
f.StringVar(&flag.StringVar{
Name: "ref",
Target: &c.packConfig.Ref,
Default: "",
Usage: `Specific git ref of the pack to retrieve info about.
Supports tags, SHA, and latest. If no ref is specified,
defaults to latest.
Using ref with a file path is not supported.`,
})
})
}
func (c *InfoCommand) Help() string {
c.Example = `
# Get information on the "hello-world" pack
nomad-pack info hello-world
`
return formatHelp(`
Usage: nomad-pack info <pack-name>
Returns information on the given pack including name, description, and variable details.
` + c.GetExample() + c.Flags().Help())
}
func (c *InfoCommand) Synopsis() string {
return "Get information on a pack"
}