Skip to content

Commit 53547ee

Browse files
committed
Factor out parsing for pack subcommand flags from packCmd.
This is a step towards re-using some of them in a docker-spk build subcommand.
1 parent 5276b68 commit 53547ee

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

pack.go

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -78,51 +78,71 @@ func archiveBytesFromReader(r io.Reader, manifestBytes, bridgeCfgBytes []byte) [
7878
return bytes
7979
}
8080

81-
func packCmd() {
82-
pkgDef := flag.String(
83-
"pkg-def",
84-
"sandstorm-pkgdef.capnp:pkgdef",
85-
"The location from which to read the package definition, of the form\n"+
86-
"<def-file>:<name>. <def-file> is the name of the file to look in,\n"+
87-
"and <name> is the name of the constant defining the package\n"+
88-
"definition.",
89-
)
90-
imageFile := flag.String("imagefile", "",
91-
"File containing Docker image to convert (output of \"docker save\")",
92-
)
93-
image := flag.String("image", "",
94-
"Name of the image to convert (fetched from the running docker daemon).",
95-
)
96-
outFilename := flag.String("out", "",
97-
"File name of the resulting spk (default inferred from package metadata)",
98-
)
99-
altAppKey := flag.String("appkey", "",
100-
"Sign the package with the specified app key, instead of the one\n"+
101-
"defined in the package definition. This can be useful if e.g.\n"+
102-
"you do not have access to the key with which the final app is\n"+
103-
"published.")
104-
flag.Parse()
81+
// Flags for the pack subcommand.
82+
type packFlags struct {
83+
// The flags proper:
84+
pkgDef, imageFile, image, outFilename, altAppKey *string
85+
86+
// The two logical parts of pkgDef:
87+
pkgDefFile, pkgDefVar string
88+
}
10589

106-
if *imageFile == "" && *image == "" {
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+
}
115+
}
116+
117+
func (p *packFlags) Parse() {
118+
flag.Parse()
119+
if *p.imageFile == "" && *p.image == "" {
107120
usageErr("Missing option: -image or -imagefile")
108121
}
109-
if *imageFile != "" && *image != "" {
122+
if *p.imageFile != "" && *p.image != "" {
110123
usageErr("Only one of -image or -imagefile may be specified.")
111124
}
112125

113-
pkgDefParts := strings.SplitN(*pkgDef, ":", 2)
126+
pkgDefParts := strings.SplitN(*p.pkgDef, ":", 2)
114127
if len(pkgDefParts) != 2 {
115128
usageErr("-pkg-def's argument must be of the form <def-file>:<name>")
116129
}
130+
p.pkgDefFile = pkgDefParts[0]
131+
p.pkgDefVar = pkgDefParts[1]
132+
}
133+
134+
func packCmd() {
135+
pFlags := registerPackFlags()
136+
pFlags.Parse()
117137

118-
metadata := getPkgMetadata(pkgDefParts[0], pkgDefParts[1])
138+
metadata := getPkgMetadata(pFlags.pkgDefFile, pFlags.pkgDefVar)
119139

120140
keyring, err := loadKeyring(*keyringPath)
121141
chkfatal("loading the sandstorm keyring", err)
122142

123-
if *altAppKey != "" {
143+
if *pFlags.altAppKey != "" {
124144
// The user has requested we use a different key.
125-
metadata.appId = *altAppKey
145+
metadata.appId = *pFlags.altAppKey
126146
}
127147

128148
appPubKey, err := SandstormBase32Encoding.DecodeString(metadata.appId)
@@ -132,21 +152,22 @@ func packCmd() {
132152
chkfatal("Fetching the app private key", err)
133153

134154
var archiveBytes []byte
135-
if *imageFile != "" {
136-
archiveBytes = archiveBytesFromFilename(*imageFile, metadata.manifest, metadata.bridgeCfg)
137-
} else if *image != "" {
138-
archiveBytes = archiveBytesFromDocker(*image, metadata.manifest, metadata.bridgeCfg)
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)
139159
} else {
160+
// pFlags.Parse() should have ruled this out.
140161
panic("impossible")
141162
}
142163
sigBytes := signatureMessage(appKeyFile, archiveBytes)
143164

144-
if *outFilename == "" {
165+
if *pFlags.outFilename == "" {
145166
// infer output file from app metadata:
146-
*outFilename = metadata.name + "-" + metadata.version + ".spk"
167+
*pFlags.outFilename = metadata.name + "-" + metadata.version + ".spk"
147168
}
148169

149-
outFile, err := os.Create(*outFilename)
170+
outFile, err := os.Create(*pFlags.outFilename)
150171
chkfatal("opening output file", err)
151172
defer outFile.Close()
152173

0 commit comments

Comments
 (0)