github.com/containers/podman/v4@v4.9.4/contrib/cirrus/win-lib.ps1 (about)

     1  #!/usr/bin/env powershell
     2  
     3  # This powershell script is intended to be "dot-sourced" by other scripts.
     4  # It's purpose is identical to that of the `lib.sh` script for Linux environments.
     5  
     6  # Behave similar to `set -e` in bash, but ONLY for powershell commandlets!
     7  # For all legacy, program, and script calls use Run-Command() or Check-Exit()
     8  $ErrorActionPreference = 'Stop'
     9  
    10  # Any golang compilation needs to know what it's building for.
    11  $Env:GOOS = "windows"
    12  $Env:GOARCH = "amd64"
    13  
    14  # Unnecessary and intrusive.  They claim parameter/variable
    15  # values aren't collected, but there could be a bug leading
    16  # to a concern over leaking of some sensitive-value.  Stop this.
    17  $Env:POWERSHELL_TELEMETRY_OPTOUT = "true"
    18  
    19  # Unnecessary and potentially disruptive.  Powershell will
    20  # never ever be updated during automation execution.  Stop this.
    21  $Env:POWERSHELL_UPDATECHECK = "off"
    22  
    23  # Color in output may confuse tooling and makes logs hard to read.
    24  # TODO: There are probably other places where color needs to be disabled
    25  # in a slightly different way :(
    26  $Env:NO_COLOR = "true"
    27  
    28  # Items only relevant in a CI environment.
    29  if ($Env:CI -eq "true") {
    30      # Defined by .cirrus.yml for use by all the linux tasks.
    31      # Drop all global envs which have unix paths, defaults are fine.
    32      Remove-Item Env:\GOPATH -ErrorAction:Ignore
    33      Remove-Item Env:\GOSRC -ErrorAction:Ignore
    34      Remove-Item Env:\GOCACHE -ErrorAction:Ignore
    35  
    36      # Defined by Cirrus-CI
    37      # Drop large known env variables (an env > 32k will break MSI/ICE validation)
    38      Remove-Item Env:\CIRRUS_COMMIT_MESSAGE -ErrorAction:Ignore
    39      Remove-Item Env:\CIRRUS_CHANGE_MESSAGE -ErrorAction:Ignore
    40      Remove-Item Env:\CIRRUS_PR_BODY -ErrorAction:Ignore
    41  }
    42  
    43  # Non-powershell commands do not halt execution on error!  This helper
    44  # should be called after every critical operation to check and halt on a
    45  # non-zero exit code.  Be careful not to use this for powershell commandlets
    46  # (builtins)!  They set '$?' to "True" (failed) or "False" success so calling
    47  # this would mask failures.  Rely on $ErrorActionPreference = 'Stop' instead.
    48  function Check-Exit {
    49      $result = $LASTEXITCODE  # WARNING: might not be a number!
    50      if ( ($result -ne $null) -and ($result -ne 0) ) {
    51          # https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe
    52          $caller = (Get-PSCallStack)[1]
    53          Write-Host "Exit code = '$result' from $($caller.ScriptName):$($caller.ScriptLineNumber)"
    54          Exit $result
    55      }
    56  }
    57  
    58  # Small helper to avoid needing to write 'Check-Exit' after every
    59  # non-powershell instruction.  It simply prints then executes the _QUOTED_
    60  # argument followed by Check-Exit.
    61  # N/B: Escape any nested quotes with back-tick ("`") characters.
    62  # WARNING: DO NOT use this with powershell builtins! It will not do what you expect!
    63  function Run-Command {
    64      param (
    65          [string] $command
    66      )
    67  
    68      Write-Host $command
    69  
    70      Invoke-Expression $command
    71      Check-Exit
    72  }