Skip to content

Commit 0047b7a

Browse files
committed
Move URL checking code to dedicated function
Previously only used for a single library.properties function, multiple planned package index rules will need to do this as well. So it is best to share the code between all of them.
1 parent e462881 commit 0047b7a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Diff for: internal/rule/rulefunction/library.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package rulefunction
1919

2020
import (
2121
"fmt"
22-
"net/http"
2322
"os"
2423
"path/filepath"
2524
"regexp"
@@ -1010,17 +1009,12 @@ func LibraryPropertiesUrlFieldDeadLink() (result ruleresult.Type, output string)
10101009
return ruleresult.NotRun, "Field not present"
10111010
}
10121011

1013-
logrus.Tracef("Checking URL: %s", url)
1014-
httpResponse, err := http.Head(url)
1012+
err := checkURL(url)
10151013
if err != nil {
10161014
return ruleresult.Fail, err.Error()
10171015
}
10181016

1019-
if httpResponse.StatusCode == http.StatusOK {
1020-
return ruleresult.Pass, ""
1021-
}
1022-
1023-
return ruleresult.Fail, httpResponse.Status
1017+
return ruleresult.Pass, ""
10241018
}
10251019

10261020
// LibraryPropertiesArchitecturesFieldMissing checks for missing library.properties "architectures" field.

Diff for: internal/rule/rulefunction/rulefunction.go

+16
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ package rulefunction
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"net/http"
2223
"regexp"
2324
"strings"
2425

2526
"github.com/arduino/arduino-lint/internal/project/projectdata"
2627
"github.com/arduino/arduino-lint/internal/project/sketch"
2728
"github.com/arduino/arduino-lint/internal/rule/ruleresult"
2829
"github.com/arduino/go-paths-helper"
30+
"github.com/sirupsen/logrus"
2931
)
3032

3133
// Type is the function signature for the rule functions.
@@ -156,3 +158,17 @@ func isValidJSON(path *paths.Path) bool {
156158
}
157159
return json.Valid(data)
158160
}
161+
162+
func checkURL(url string) error {
163+
logrus.Tracef("Checking URL: %s", url)
164+
response, err := http.Head(url)
165+
if err != nil {
166+
return err
167+
}
168+
169+
if response.StatusCode != http.StatusOK {
170+
return fmt.Errorf("%s", response.Status)
171+
}
172+
173+
return nil
174+
}

0 commit comments

Comments
 (0)