@@ -51,11 +51,11 @@ func main() {
5151 panic (err )
5252 }
5353
54- cmdlines := strings .Split (string (content ), " " )
55- cfg , _ := parsecmdline ( cmdlines )
54+ cmdLines := strings .Split (string (content ), " " )
55+ cfg := parseCmdLine ( cmdLines )
5656
5757 // Get the ID from the metadata service
58- err = cfg .MetaDataQuery ()
58+ err = cfg .metaDataQuery ()
5959 if err != nil {
6060 panic (err )
6161 }
@@ -131,7 +131,11 @@ func main() {
131131 if err != nil {
132132 panic (err )
133133 }
134- io .Copy (os .Stdout , out )
134+
135+ _ , err = io .Copy (os .Stdout , out )
136+ if err != nil {
137+ panic (err )
138+ }
135139
136140 resp , err := cli .ContainerCreate (ctx , tinkContainer , tinkHostConfig , nil , nil , "" )
137141 if err != nil {
@@ -145,74 +149,42 @@ func main() {
145149 fmt .Println (resp .ID )
146150}
147151
148- func parsecmdline (cmdlines []string ) (cfg tinkConfig , err error ) {
149-
150- for i := range cmdlines {
151- cmdline := strings .Split (cmdlines [i ], "=" )
152- if len (cmdline ) != 0 {
153-
154- // Find Registry configuration
155- if cmdline [0 ] == "docker_registry" {
156- cfg .registry = cmdline [1 ]
157- }
158- if cmdline [0 ] == "registry_username" {
159- cfg .username = cmdline [1 ]
160- }
161- if cmdline [0 ] == "registry_password" {
162- cfg .password = cmdline [1 ]
163- }
164-
165- // Find Tinkerbell servers settings
166- if cmdline [0 ] == "packet_base_url" {
167- cfg .baseURL = cmdline [1 ]
168- }
169- if cmdline [0 ] == "tinkerbell" {
170- cfg .tinkerbell = cmdline [1 ]
171- }
172-
173- // Find GRPC configuration
174- if cmdline [0 ] == "grpc_authority" {
175- cfg .grpcAuthority = cmdline [1 ]
176- }
177- if cmdline [0 ] == "grpc_cert_url" {
178- cfg .grpcCertURL = cmdline [1 ]
179- }
180-
181- // Find the worker configuration
182- if cmdline [0 ] == "worker_id" {
183- cfg .workerID = cmdline [1 ]
184- }
152+ // parseCmdLine will parse the command line.
153+ func parseCmdLine (cmdLines []string ) (cfg tinkConfig ) {
154+ for i := range cmdLines {
155+ cmdLine := strings .Split (cmdLines [i ], "=" )
156+ if len (cmdLine ) == 0 {
157+ continue
185158 }
186- }
187- return
188- }
189-
190- // DownloadFile will download a url to a local file. It's efficient because it will
191- // write as it downloads and not load the whole file into memory.
192- func DownloadFile (filepath string , url string ) error {
193159
194- // Get the data
195- resp , err := http .Get (url )
196- if err != nil {
197- return err
198- }
199- defer resp .Body .Close ()
200-
201- // Create the file
202- out , err := os .Create (filepath )
203- if err != nil {
204- return err
160+ switch cmd := cmdLine [0 ]; cmd {
161+ // Find Registry configuration
162+ case "docker_registry" :
163+ cfg .registry = cmdLine [1 ]
164+ case "registry_username" :
165+ cfg .username = cmdLine [1 ]
166+ case "registry_password" :
167+ cfg .password = cmdLine [1 ]
168+ // Find Tinkerbell servers settings
169+ case "packet_base_url" :
170+ cfg .baseURL = cmdLine [1 ]
171+ case "tinkerbell" :
172+ cfg .tinkerbell = cmdLine [1 ]
173+ // Find GRPC configuration
174+ case "grpc_authority" :
175+ cfg .grpcAuthority = cmdLine [1 ]
176+ case "grpc_cert_url" :
177+ cfg .grpcCertURL = cmdLine [1 ]
178+ // Find the worker configuration
179+ case "worker_id" :
180+ cfg .workerID = cmdLine [1 ]
181+ }
205182 }
206- defer out .Close ()
207-
208- // Write the body to file
209- _ , err = io .Copy (out , resp .Body )
210- return err
183+ return cfg
211184}
212185
213- // MetaDataQuery will query the metadata
214- func (cfg * tinkConfig ) MetaDataQuery () error {
215-
186+ // metaDataQuery will query the metadata.
187+ func (cfg * tinkConfig ) metaDataQuery () error {
216188 spaceClient := http.Client {
217189 Timeout : time .Second * 60 , // Timeout after 60 seconds (seems massively long is this dial-up?)
218190 }
0 commit comments