@@ -78,51 +78,71 @@ func archiveBytesFromReader(r io.Reader, manifestBytes, bridgeCfgBytes []byte) [
78
78
return bytes
79
79
}
80
80
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
+ }
105
89
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 == "" {
107
120
usageErr ("Missing option: -image or -imagefile" )
108
121
}
109
- if * imageFile != "" && * image != "" {
122
+ if * p . imageFile != "" && * p . image != "" {
110
123
usageErr ("Only one of -image or -imagefile may be specified." )
111
124
}
112
125
113
- pkgDefParts := strings .SplitN (* pkgDef , ":" , 2 )
126
+ pkgDefParts := strings .SplitN (* p . pkgDef , ":" , 2 )
114
127
if len (pkgDefParts ) != 2 {
115
128
usageErr ("-pkg-def's argument must be of the form <def-file>:<name>" )
116
129
}
130
+ p .pkgDefFile = pkgDefParts [0 ]
131
+ p .pkgDefVar = pkgDefParts [1 ]
132
+ }
133
+
134
+ func packCmd () {
135
+ pFlags := registerPackFlags ()
136
+ pFlags .Parse ()
117
137
118
- metadata := getPkgMetadata (pkgDefParts [ 0 ], pkgDefParts [ 1 ] )
138
+ metadata := getPkgMetadata (pFlags . pkgDefFile , pFlags . pkgDefVar )
119
139
120
140
keyring , err := loadKeyring (* keyringPath )
121
141
chkfatal ("loading the sandstorm keyring" , err )
122
142
123
- if * altAppKey != "" {
143
+ if * pFlags . altAppKey != "" {
124
144
// The user has requested we use a different key.
125
- metadata .appId = * altAppKey
145
+ metadata .appId = * pFlags . altAppKey
126
146
}
127
147
128
148
appPubKey , err := SandstormBase32Encoding .DecodeString (metadata .appId )
@@ -132,21 +152,22 @@ func packCmd() {
132
152
chkfatal ("Fetching the app private key" , err )
133
153
134
154
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 )
139
159
} else {
160
+ // pFlags.Parse() should have ruled this out.
140
161
panic ("impossible" )
141
162
}
142
163
sigBytes := signatureMessage (appKeyFile , archiveBytes )
143
164
144
- if * outFilename == "" {
165
+ if * pFlags . outFilename == "" {
145
166
// infer output file from app metadata:
146
- * outFilename = metadata .name + "-" + metadata .version + ".spk"
167
+ * pFlags . outFilename = metadata .name + "-" + metadata .version + ".spk"
147
168
}
148
169
149
- outFile , err := os .Create (* outFilename )
170
+ outFile , err := os .Create (* pFlags . outFilename )
150
171
chkfatal ("opening output file" , err )
151
172
defer outFile .Close ()
152
173
0 commit comments