Skip to content

Commit 6c82d9d

Browse files
committed
Add/improve code comments
1 parent b9c4887 commit 6c82d9d

File tree

18 files changed

+123
-46
lines changed

18 files changed

+123
-46
lines changed

check/check.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package check runs checks on a project.
12
package check
23

34
import (
@@ -17,6 +18,7 @@ import (
1718
"github.com/arduino/arduino-cli/cli/feedback"
1819
)
1920

21+
// shouldRun returns whether a given check should be run for the given project under the current tool configuration.
2022
func shouldRun(checkConfiguration checkconfigurations.Type, currentProject project.Type) bool {
2123
configurationCheckModes := configuration.CheckModes(currentProject.SuperprojectType)
2224

@@ -53,6 +55,7 @@ func shouldRun(checkConfiguration checkconfigurations.Type, currentProject proje
5355
return false
5456
}
5557

58+
// message fills the message template provided by the check configuration with the check output.
5659
// TODO: make checkOutput a struct to allow for more advanced message templating
5760
func message(templateText string, checkOutput string) string {
5861
messageTemplate := template.Must(template.New("messageTemplate").Parse(templateText))
@@ -63,6 +66,7 @@ func message(templateText string, checkOutput string) string {
6366
return messageBuffer.String()
6467
}
6568

69+
// RunChecks runs all checks for the given project and outputs the results.
6670
func RunChecks(project project.Type) {
6771
fmt.Printf("Checking %s in %s\n", project.ProjectType.String(), project.Path.String())
6872

check/checkconfigurations/checkconfigurations.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
Package checkconfigurations defines the configuration of each check:
3+
- metadata
4+
- output template
5+
- under which conditions it's enabled
6+
- the level of a failure
7+
- which function implements it
8+
*/
19
package checkconfigurations
210

311
import (
@@ -6,29 +14,24 @@ import (
614
"github.com/arduino/arduino-check/project/projecttype"
715
)
816

17+
// Type is the type for check configurations.
918
type Type struct {
10-
ProjectType projecttype.Type
11-
// Arbitrary text for the log
12-
Category string
13-
Subcategory string
14-
// Unique check identifier
15-
ID string
16-
Name string
17-
Description string
18-
// The warning/error message template displayed when the check fails
19-
// The check function output will be filled into the template
20-
MessageTemplate string
21-
// The following fields define under which tool configuration modes the check will run
22-
// Check is disabled when tool is in any of these modes
23-
DisableModes []checkmode.Type
24-
// Check is only enabled when tool is in one of these modes
25-
EnableModes []checkmode.Type
26-
// The following fields define the check level in each configuration mode
27-
InfoModes []checkmode.Type
28-
WarningModes []checkmode.Type
29-
ErrorModes []checkmode.Type
30-
// The function that implements the check
31-
CheckFunction checkfunctions.Type
19+
ProjectType projecttype.Type // The project type the check applies to.
20+
// The following fields provide arbitrary text for the tool output associated with each check:
21+
Category string
22+
Subcategory string
23+
ID string // Unique check identifier: <project type identifier (L|S|P|I)><category identifier><number>
24+
Name string // Short description of the check.
25+
Description string // Supplemental information about the check.
26+
MessageTemplate string // The warning/error message template displayed when the check fails. Will be filled by check function output.
27+
// The following fields define under which tool configuration modes the check will run:
28+
DisableModes []checkmode.Type // Check is disabled when tool is in any of these modes.
29+
EnableModes []checkmode.Type // Check is only enabled when tool is in one of these modes.
30+
// The following fields define the check level in each configuration mode:
31+
InfoModes []checkmode.Type // Failure of the check only results in an informational message.
32+
WarningModes []checkmode.Type // Failure of the check is considered a warning.
33+
ErrorModes []checkmode.Type // Failure of the check is considered an error.
34+
CheckFunction checkfunctions.Type // The function that implements the check.
3235
}
3336

3437
// Checks is an array of structs that define the configuration of each check.

check/checkdata/checkdata.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Package checkdata handles the collection of data specific to a project before running the checks on it.
3+
This is for data required by multiple checks.
4+
*/
15
package checkdata
26

37
import (
@@ -11,34 +15,41 @@ import (
1115

1216
var projectType projecttype.Type
1317

18+
// ProjectType returns the type of the project being checked.
1419
func ProjectType() projecttype.Type {
1520
return projectType
1621
}
1722

1823
var projectPath *paths.Path
1924

25+
// ProjectPath returns the path to the project being checked.
2026
func ProjectPath() *paths.Path {
2127
return projectPath
2228
}
2329

2430
var libraryPropertiesLoadError error
2531

32+
// LibraryPropertiesLoadError returns the error output from loading the library.properties metadata file.
2633
func LibraryPropertiesLoadError() error {
2734
return libraryPropertiesLoadError
2835
}
2936

3037
var libraryProperties *properties.Map
3138

39+
// LibraryProperties returns the data from the library.properties metadata file
3240
func LibraryProperties() *properties.Map {
3341
return libraryProperties
3442
}
3543

3644
var libraryPropertiesSchemaValidationResult *gojsonschema.Result
3745

46+
// LibraryPropertiesSchemaValidationResult returns the result of validating library.properties against the JSON schema.
47+
// See: https://github.com/xeipuuv/gojsonschema
3848
func LibraryPropertiesSchemaValidationResult() *gojsonschema.Result {
3949
return libraryPropertiesSchemaValidationResult
4050
}
4151

52+
// Initialize gathers the check data for the specified project.
4253
func Initialize(project project.Type) {
4354
projectType = project.ProjectType
4455
projectPath = project.Path
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// Package checkfunctions contains the functions that implement each check.
12
package checkfunctions
23

34
import (
45
"github.com/arduino/arduino-check/check/checkresult"
56
)
67

7-
// output is the contextual information that will be added to the stock message
8+
// Type is the function signature for the check functions.
9+
// The `output` result is the contextual information that will be inserted into the check's message template
810
type Type func() (result checkresult.Type, output string)

check/checkfunctions/library.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package checkfunctions
22

3+
// The check functions for libraries.
4+
35
import (
46
"github.com/arduino/arduino-check/check/checkdata"
57
"github.com/arduino/arduino-check/check/checkresult"

check/checkfunctions/sketch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package checkfunctions
22

3+
// The check functions for sketches.
4+
35
import (
46
"github.com/arduino/arduino-check/check/checkdata"
57
"github.com/arduino/arduino-check/check/checkresult"

check/checklevel/checklevel.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package checklevel defines the level assigned to a check failure.
12
package checklevel
23

34
import (
@@ -8,17 +9,19 @@ import (
89
"github.com/arduino/arduino-check/configuration/checkmode"
910
)
1011

12+
// Type is the type for the check levels.
1113
//go:generate stringer -type=Type -linecomment
1214
type Type int
1315

14-
// Line comments set the string for each level
16+
// The line comments set the string for each level.
1517
const (
1618
Info Type = iota // info
1719
Warning // warning
1820
Error // error
1921
Notice // notice
2022
)
2123

24+
// CheckLevel determines the check level assigned to failure of the given check under the current tool configuration.
2225
func CheckLevel(checkConfiguration checkconfigurations.Type) (Type, error) {
2326
configurationCheckModes := configuration.CheckModes(checkConfiguration.ProjectType)
2427
for _, errorMode := range checkConfiguration.ErrorModes {

check/checkresult/checkresult.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package checkresult defines the possible result values returned by a check.
12
package checkresult
23

34
//go:generate stringer -type=Type -linecomment

configuration/checkmode/checkmode.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// Package checkmode defines the tool configuration options that affect checks.
12
package checkmode
23

34
import (
45
"github.com/arduino/arduino-check/project/projecttype"
56
"github.com/sirupsen/logrus"
67
)
78

9+
// Type is the type for check modes.
810
type Type int
911

1012
//go:generate stringer -type=Type -linecomment
@@ -17,10 +19,10 @@ const (
1719
Default // default
1820
)
1921

22+
// Modes merges the default check mode values for the given superproject type with any user-specified check mode settings.
2023
func Modes(defaultCheckModes map[projecttype.Type]map[Type]bool, customCheckModes map[Type]bool, superprojectType projecttype.Type) map[Type]bool {
2124
checkModes := make(map[Type]bool)
2225

23-
// Merge the default settings with any custom settings specified by the user
2426
for key, defaultValue := range defaultCheckModes[superprojectType] {
2527
customCheckModeValue, customCheckModeIsConfigured := customCheckModes[key]
2628
if customCheckModeIsConfigured {

configuration/configuration.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package configuration handles the configuration of the arduino-check tool.
12
package configuration
23

34
import (
@@ -8,6 +9,7 @@ import (
89
"github.com/sirupsen/logrus"
910
)
1011

12+
// Initialize sets up the tool configuration according to defaults and user-specified options.
1113
func Initialize() {
1214
setDefaults()
1315
// TODO configuration according to command line input
@@ -33,24 +35,28 @@ func Initialize() {
3335

3436
var customCheckModes = make(map[checkmode.Type]bool)
3537

38+
// CheckModes returns the check modes configuration for the given project type.
3639
func CheckModes(superprojectType projecttype.Type) map[checkmode.Type]bool {
3740
return checkmode.Modes(defaultCheckModes, customCheckModes, superprojectType)
3841
}
3942

4043
var superprojectType projecttype.Type
4144

45+
// SuperprojectType returns the superproject type filter configuration.
4246
func SuperprojectType() projecttype.Type {
4347
return superprojectType
4448
}
4549

4650
var recursive bool
4751

52+
// Recursive returns the recursive project search configuration value.
4853
func Recursive() bool {
4954
return recursive
5055
}
5156

5257
var targetPath *paths.Path
5358

59+
// TargetPath returns the projects search path.
5460
func TargetPath() *paths.Path {
5561
return targetPath
5662
}

configuration/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package configuration
22

3+
// The default configuration settings.
4+
35
import (
46
"github.com/arduino/arduino-check/configuration/checkmode"
57
"github.com/arduino/arduino-check/project/projecttype"

project/library/library.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package library provides functions specific to checking Arduino libraries.
12
package library
23

34
import (
@@ -6,13 +7,14 @@ import (
67

78
var empty struct{}
89

9-
// reference: https://github.com/arduino/arduino-cli/blob/0.13.0/arduino/libraries/libraries.go#L167
10+
// Reference: https://github.com/arduino/arduino-cli/blob/0.13.0/arduino/libraries/libraries.go#L167
1011
var headerFileValidExtensions = map[string]struct{}{
1112
".h": empty,
1213
".hpp": empty,
1314
".hh": empty,
1415
}
1516

17+
// HasHeaderFileValidExtension returns whether the file at the given path has a valid library header file extension.
1618
func HasHeaderFileValidExtension(filePath *paths.Path) bool {
1719
_, hasHeaderFileValidExtension := headerFileValidExtensions[filePath.Ext()]
1820
if hasHeaderFileValidExtension {
@@ -21,10 +23,12 @@ func HasHeaderFileValidExtension(filePath *paths.Path) bool {
2123
return false
2224
}
2325

26+
// See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-metadata
2427
var metadataFilenames = map[string]struct{}{
2528
"library.properties": empty,
2629
}
2730

31+
// IsMetadataFile returns whether the file at the given path is an Arduino library metadata file.
2832
func IsMetadataFile(filePath *paths.Path) bool {
2933
_, isMetadataFile := metadataFilenames[filePath.Base()]
3034
if isMetadataFile {

project/library/libraryproperties/libraryproperties.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package libraryproperties provides functions for working with the library.properties Arduino library metadata file.
12
package libraryproperties
23

34
import (
@@ -10,6 +11,7 @@ import (
1011
"github.com/xeipuuv/gojsonschema"
1112
)
1213

14+
// Properties parses the library.properties from the given path and returns the data.
1315
func Properties(libraryPath *paths.Path) (*properties.Map, error) {
1416
libraryProperties, err := properties.Load(libraryPath.Join("library.properties").String())
1517
if err != nil {
@@ -18,6 +20,7 @@ func Properties(libraryPath *paths.Path) (*properties.Map, error) {
1820
return libraryProperties, nil
1921
}
2022

23+
// Validate validates library.properties data against the JSON schema.
2124
func Validate(libraryProperties *properties.Map) *gojsonschema.Result {
2225
workingPath, _ := os.Getwd()
2326
schemaPath := paths.New(workingPath).Join("arduino-library-properties-schema.json")
@@ -38,14 +41,18 @@ func Validate(libraryProperties *properties.Map) *gojsonschema.Result {
3841
return result
3942
}
4043

44+
// FieldMissing returns whether the given required field is missing from library.properties.
4145
func FieldMissing(fieldName string, validationResult *gojsonschema.Result) bool {
4246
return ValidationErrorMatch("required", "(root)", fieldName+" is required", validationResult)
4347
}
4448

49+
// FieldPatternMismatch returns whether the given field did not match the regular expression defined in the JSON schema.
4550
func FieldPatternMismatch(fieldName string, validationResult *gojsonschema.Result) bool {
4651
return ValidationErrorMatch("pattern", fieldName, "", validationResult)
4752
}
4853

54+
// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error.
55+
// See: https://github.com/xeipuuv/gojsonschema#working-with-errors
4956
func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQuery string, validationResult *gojsonschema.Result) bool {
5057
if validationResult.Valid() {
5158
// No error, so nothing to match

project/packageindex/packageindex.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Package packageindex provides functions specific to checking the package index files of the Arduino Boards Manager.
3+
See: https://arduino.github.io/arduino-cli/latest/package_index_json-specification
4+
*/
15
package packageindex
26

37
import (
@@ -8,10 +12,12 @@ import (
812

913
var empty struct{}
1014

15+
// Reference: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/#naming-of-the-json-index-file
1116
var validExtensions = map[string]struct{}{
1217
".json": empty,
1318
}
1419

20+
// HasValidExtension returns whether the file at the given path has a valid package index extension.
1521
func HasValidExtension(filePath *paths.Path) bool {
1622
_, hasValidExtension := validExtensions[filePath.Ext()]
1723
if hasValidExtension {
@@ -27,6 +33,7 @@ var validFilenameRegex = map[bool]*regexp.Regexp{
2733
false: regexp.MustCompile(`^package_(.+_)+index.json$`),
2834
}
2935

36+
// HasValidFilename returns whether the file at the given path has a valid package index filename.
3037
func HasValidFilename(filePath *paths.Path, officialCheckMode bool) bool {
3138
regex := validFilenameRegex[officialCheckMode]
3239
filename := filePath.Base()

project/platform/platform.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Package packageindex provides functions specific to checking Arduino boards platforms.
3+
See: https://arduino.github.io/arduino-cli/latest/platform-specification/
4+
*/
15
package platform
26

37
import (
@@ -14,6 +18,7 @@ var configurationFilenames = map[string]struct{}{
1418
"programmers.txt": empty,
1519
}
1620

21+
// IsConfigurationFile returns whether the file at the given path has a boards platform configuration file filename.
1722
func IsConfigurationFile(filePath *paths.Path) bool {
1823
_, isConfigurationFile := configurationFilenames[filePath.Base()]
1924
if isConfigurationFile {
@@ -27,6 +32,7 @@ var requiredConfigurationFilenames = map[string]struct{}{
2732
"boards.txt": empty,
2833
}
2934

35+
// IsRequiredConfigurationFile returns whether the file at the given path has the filename of a required boards platform configuration file.
3036
func IsRequiredConfigurationFile(filePath *paths.Path) bool {
3137
_, isRequiredConfigurationFile := requiredConfigurationFilenames[filePath.Base()]
3238
if isRequiredConfigurationFile {

0 commit comments

Comments
 (0)