summaryrefslogtreecommitdiff
path: root/scripts/gofmtcheck.ps1
blob: 7a15861c258853f47ed9f4ee8aad15ce61762898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[CmdletBinding()]
param (
    [switch]
    $Fix
)

$script:PSDefaultParameterValues = @{
    '*:Confirm'           = $false
    '*:ErrorAction'       = 'Stop'
}

. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)

if ($Fix) {
    # Check gofmt
    echo "==> Fixing gofmt deviations..."

    # This filter should match the search filter in ../GNUMakefile
    $null = Get-ChildItem -Path $SOURCE_DIR -Recurse -Filter '*.go' `
    | Select-Object -ExpandProperty FullName `
    | Select-String -NotMatch -SimpleMatch vendor `
    | ForEach-Object -Process { gofmt.exe -s -w $_ }
}
else {
    # Check gofmt
    echo "==> Checking that code complies with gofmt requirements..."

    # This filter should match the search filter in ../GNUMakefile
    $gofmt_files= Get-ChildItem -Path $SOURCE_DIR -Recurse -Filter '*.go' `
    | Select-Object -ExpandProperty FullName `
    | Select-String -NotMatch -SimpleMatch vendor `
    | ForEach-Object -Process { gofmt.exe -s -l $_ }

    if ($gofmt_files) {
        echo 'gofmt needs running on the following files:'
        echo "${gofmt_files}"
        echo "You can use this command and pass the -Fix parameter to reformat code."
        exit 1
    }
}