Skip to content

Commit 8dae142

Browse files
committed
Use flag.StringVar instead of flag.String
1 parent 53547ee commit 8dae142

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

pack.go

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -81,49 +81,51 @@ func archiveBytesFromReader(r io.Reader, manifestBytes, bridgeCfgBytes []byte) [
8181
// Flags for the pack subcommand.
8282
type packFlags struct {
8383
// The flags proper:
84-
pkgDef, imageFile, image, outFilename, altAppKey *string
84+
pkgDef, imageFile, image, outFilename, altAppKey string
8585

8686
// The two logical parts of pkgDef:
8787
pkgDefFile, pkgDefVar string
8888
}
8989

90-
func registerPackFlags() *packFlags {
91-
return &packFlags{
92-
pkgDef: flag.String(
93-
"pkg-def",
94-
"sandstorm-pkgdef.capnp:pkgdef",
95-
"The location from which to read the package definition, of the form\n"+
96-
"<def-file>:<name>. <def-file> is the name of the file to look in,\n"+
97-
"and <name> is the name of the constant defining the package\n"+
98-
"definition.",
99-
),
100-
imageFile: flag.String("imagefile", "",
101-
"File containing Docker image to convert (output of \"docker save\")",
102-
),
103-
image: flag.String("image", "",
104-
"Name of the image to convert (fetched from the running docker daemon).",
105-
),
106-
outFilename: flag.String("out", "",
107-
"File name of the resulting spk (default inferred from package metadata)",
108-
),
109-
altAppKey: flag.String("appkey", "",
110-
"Sign the package with the specified app key, instead of the one\n"+
111-
"defined in the package definition. This can be useful if e.g.\n"+
112-
"you do not have access to the key with which the final app is\n"+
113-
"published."),
114-
}
90+
func (p *packFlags) Register() {
91+
flag.StringVar(&p.pkgDef,
92+
"pkg-def",
93+
"sandstorm-pkgdef.capnp:pkgdef",
94+
"The location from which to read the package definition, of the form\n"+
95+
"<def-file>:<name>. <def-file> is the name of the file to look in,\n"+
96+
"and <name> is the name of the constant defining the package\n"+
97+
"definition.",
98+
)
99+
flag.StringVar(&p.imageFile,
100+
"imagefile", "",
101+
"File containing Docker image to convert (output of \"docker save\")",
102+
)
103+
flag.StringVar(&p.image,
104+
"image", "",
105+
"Name of the image to convert (fetched from the running docker daemon).",
106+
)
107+
flag.StringVar(&p.outFilename,
108+
"out", "",
109+
"File name of the resulting spk (default inferred from package metadata)",
110+
)
111+
flag.StringVar(&p.altAppKey,
112+
"appkey", "",
113+
"Sign the package with the specified app key, instead of the one\n"+
114+
"defined in the package definition. This can be useful if e.g.\n"+
115+
"you do not have access to the key with which the final app is\n"+
116+
"published.")
115117
}
116118

117119
func (p *packFlags) Parse() {
118120
flag.Parse()
119-
if *p.imageFile == "" && *p.image == "" {
121+
if p.imageFile == "" && p.image == "" {
120122
usageErr("Missing option: -image or -imagefile")
121123
}
122-
if *p.imageFile != "" && *p.image != "" {
124+
if p.imageFile != "" && p.image != "" {
123125
usageErr("Only one of -image or -imagefile may be specified.")
124126
}
125127

126-
pkgDefParts := strings.SplitN(*p.pkgDef, ":", 2)
128+
pkgDefParts := strings.SplitN(p.pkgDef, ":", 2)
127129
if len(pkgDefParts) != 2 {
128130
usageErr("-pkg-def's argument must be of the form <def-file>:<name>")
129131
}
@@ -132,17 +134,18 @@ func (p *packFlags) Parse() {
132134
}
133135

134136
func packCmd() {
135-
pFlags := registerPackFlags()
137+
pFlags := &packFlags{}
138+
pFlags.Register()
136139
pFlags.Parse()
137140

138141
metadata := getPkgMetadata(pFlags.pkgDefFile, pFlags.pkgDefVar)
139142

140143
keyring, err := loadKeyring(*keyringPath)
141144
chkfatal("loading the sandstorm keyring", err)
142145

143-
if *pFlags.altAppKey != "" {
146+
if pFlags.altAppKey != "" {
144147
// The user has requested we use a different key.
145-
metadata.appId = *pFlags.altAppKey
148+
metadata.appId = pFlags.altAppKey
146149
}
147150

148151
appPubKey, err := SandstormBase32Encoding.DecodeString(metadata.appId)
@@ -152,22 +155,22 @@ func packCmd() {
152155
chkfatal("Fetching the app private key", err)
153156

154157
var archiveBytes []byte
155-
if *pFlags.imageFile != "" {
156-
archiveBytes = archiveBytesFromFilename(*pFlags.imageFile, metadata.manifest, metadata.bridgeCfg)
157-
} else if *pFlags.image != "" {
158-
archiveBytes = archiveBytesFromDocker(*pFlags.image, metadata.manifest, metadata.bridgeCfg)
158+
if pFlags.imageFile != "" {
159+
archiveBytes = archiveBytesFromFilename(pFlags.imageFile, metadata.manifest, metadata.bridgeCfg)
160+
} else if pFlags.image != "" {
161+
archiveBytes = archiveBytesFromDocker(pFlags.image, metadata.manifest, metadata.bridgeCfg)
159162
} else {
160163
// pFlags.Parse() should have ruled this out.
161164
panic("impossible")
162165
}
163166
sigBytes := signatureMessage(appKeyFile, archiveBytes)
164167

165-
if *pFlags.outFilename == "" {
168+
if pFlags.outFilename == "" {
166169
// infer output file from app metadata:
167-
*pFlags.outFilename = metadata.name + "-" + metadata.version + ".spk"
170+
pFlags.outFilename = metadata.name + "-" + metadata.version + ".spk"
168171
}
169172

170-
outFile, err := os.Create(*pFlags.outFilename)
173+
outFile, err := os.Create(pFlags.outFilename)
171174
chkfatal("opening output file", err)
172175
defer outFile.Close()
173176

0 commit comments

Comments
 (0)