Skip to content

Commit 93ba295

Browse files
authored
Merge pull request #74 from tinkerbell/lint-fix
Fix errors from lint-install run.
2 parents b01299e + 2533946 commit 93ba295

File tree

3 files changed

+64
-92
lines changed

3 files changed

+64
-92
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ run:
5050
sudo ~/go/bin/linuxkit run qemu --mem 2048 out/hook-${ARCH}
5151

5252
dev-bootkitBuild:
53-
cd bootkit; docker buildx build -load -t $(ORG)/hook-bootkit:0.0 .
53+
cd bootkit; docker buildx build --load -t $(ORG)/hook-bootkit:0.0 .
5454

5555
bootkitBuild:
5656
cd bootkit; docker buildx build --platform linux/amd64,linux/arm64 --push -t $(ORG)/hook-bootkit:0.0 .
5757

5858
dev-tink-dockerBuild:
59-
cd tink-docker; docker buildx build -load -t $(ORG)/hook-docker:0.0 .
59+
cd tink-docker; docker buildx build --load -t $(ORG)/hook-docker:0.0 .
6060

6161
tink-dockerBuild:
6262
cd tink-docker; docker buildx build --platform linux/amd64,linux/arm64 --push -t $(ORG)/hook-docker:0.0 .
@@ -145,4 +145,4 @@ out/linters/golangci-lint-$(GOLINT_VERSION):
145145
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLINT_VERSION)
146146
mv out/linters/golangci-lint out/linters/golangci-lint-$(GOLINT_VERSION)
147147

148-
# END: lint-install ../hook
148+
# END: lint-install ../hook

bootkit/main.go

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tink-docker/main.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func main() {
2323
fmt.Println("Starting Tink-Docker")
2424
go rebootWatch()
2525

26-
// Parse the cmdline in order to find the urls for the repostiory and path to the cert
26+
// Parse the cmdline in order to find the urls for the repository and path to the cert
2727
content, err := ioutil.ReadFile("/proc/cmdline")
2828
if err != nil {
2929
panic(err)
3030
}
31-
cmdlines := strings.Split(string(content), " ")
32-
cfg, _ := parsecmdline(cmdlines)
31+
cmdLines := strings.Split(string(content), " ")
32+
cfg := parseCmdLine(cmdLines)
3333

3434
path := fmt.Sprintf("/etc/docker/certs.d/%s/", cfg.registry)
3535

@@ -39,7 +39,7 @@ func main() {
3939
panic(err)
4040
}
4141
// Download the configuration
42-
err = DownloadFile(path+"ca.crt", cfg.baseURL+"/ca.pem")
42+
err = downloadFile(path+"ca.crt", cfg.baseURL+"/ca.pem")
4343
if err != nil {
4444
panic(err)
4545
}
@@ -55,29 +55,30 @@ func main() {
5555
}
5656
}
5757

58-
func parsecmdline(cmdlines []string) (cfg tinkConfig, err error) {
58+
// parseCmdLine will parse the command line.
59+
func parseCmdLine(cmdLines []string) (cfg tinkConfig) {
60+
for i := range cmdLines {
61+
cmdLine := strings.Split(cmdLines[i], "=")
62+
if len(cmdLine) == 0 {
63+
continue
64+
}
5965

60-
for i := range cmdlines {
61-
cmdline := strings.Split(cmdlines[i], "=")
62-
if len(cmdline) != 0 {
63-
if cmdline[0] == "docker_registry" {
64-
cfg.registry = cmdline[1]
65-
}
66-
if cmdline[0] == "packet_base_url" {
67-
cfg.baseURL = cmdline[1]
68-
}
69-
if cmdline[0] == "tinkerbell" {
70-
cfg.tinkerbell = cmdline[1]
71-
}
66+
switch cmd := cmdLine[0]; cmd {
67+
// Find Registry configuration
68+
case "docker_registry":
69+
cfg.registry = cmdLine[1]
70+
case "packet_base_url":
71+
cfg.baseURL = cmdLine[1]
72+
case "tinkerbell":
73+
cfg.tinkerbell = cmdLine[1]
7274
}
7375
}
74-
return
76+
return cfg
7577
}
7678

77-
// DownloadFile will download a url to a local file. It's efficient because it will
79+
// downloadFile will download a url to a local file. It's efficient because it will
7880
// write as it downloads and not load the whole file into memory.
79-
func DownloadFile(filepath string, url string) error {
80-
81+
func downloadFile(filepath string, url string) error {
8182
// Get the data
8283
resp, err := http.Get(url)
8384
if err != nil {
@@ -103,7 +104,6 @@ func rebootWatch() {
103104
// Forever loop
104105
for {
105106
if fileExists("/worker/reboot") {
106-
//Build the command, and execute
107107
cmd := exec.Command("/sbin/reboot")
108108
cmd.Stdout = os.Stdout
109109
cmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)