@@ -30,27 +30,29 @@ function getOsBinName() {
3030
3131// Normalizes version to release tag format: knative-vX.Y.Z
3232// Ex.: '1.16' or 'v1.16' will return 'knative-v1.16.0'
33- function smartVersionUpdate ( version ) {
33+ function smartVersionUpdate ( version ) {
3434 const versionRegex = / ^ (?< knprefix > k n a t i v e - ) ? (?< prefix > v ? ) (?< major > \d + ) \. (?< minor > \d + ) ( .(?< patch > \d + ) ) ? $ / ;
35- let match = version . match ( versionRegex ) ;
36- if ( match ) {
37- const knprefix = 'knative-' ;
38- const prefix = 'v' ;
39- const patch = match . groups . patch ?? 0 ;
40- return `${ knprefix } ${ prefix } ${ match . groups . major } .${ match . groups . minor } .${ patch } ` ;
41- }
42-
43- core . setFailed ( `Invalid version format (${ version } ). Expected format: "1.16[.X]" or "v1.16[.X]"` ) ;
44- return undefined ;
35+ const match = version . match ( versionRegex ) ;
36+ if ( ! match ) {
37+ throw new Error ( `Invalid version format (${ version } ). Expected format: "1.16[.X]" or "v1.16[.X]"` ) ;
38+ }
39+ const knprefix = 'knative-' ;
40+ const prefix = 'v' ;
41+ const patch = match . groups . patch ?? 0 ;
42+ return `${ knprefix } ${ prefix } ${ match . groups . major } .${ match . groups . minor } .${ patch } ` ;
4543}
4644
47- // Downloads binary and makes it executable
48- async function cmdConstructAndRun ( url , binPath ) {
49- await exec . exec ( 'curl' , [ '-L' , '-o' , binPath , url ] ) ;
50-
45+ const DEFAULT_BINARY_SOURCE = 'https://github.com/knative/func/releases/download' ;
46+
47+ // Downloads binary from release URL and makes it executable
48+ async function downloadFuncBinary ( version , osBinName , binPath , binarySource ) {
49+ const url = `${ binarySource } /${ version } /${ osBinName } ` ;
50+ core . info ( `Downloading from: ${ url } ` ) ;
51+
52+ await exec . exec ( 'curl' , [ '-L' , '--fail' , '-o' , binPath , url ] ) ;
53+
5154 if ( ! fs . existsSync ( binPath ) ) {
52- core . setFailed ( "Download failed, couldn't find the binary on disk" ) ;
53- return ;
55+ throw new Error ( "Download failed, couldn't find the binary on disk" ) ;
5456 }
5557
5658 if ( process . env . RUNNER_OS !== 'Windows' ) {
@@ -70,35 +72,43 @@ async function addBinToPath(binPath) {
7072}
7173
7274async function run ( ) {
73- try {
74- const osBin = core . getInput ( 'binary' ) || getOsBinName ( ) ;
75- if ( osBin === "unknown" ) {
76- core . setFailed ( "Invalid os binary determination, try setting it specifically using 'binary'" ) ;
77- return ;
78- }
79-
80- let version = core . getInput ( 'version' ) || DEFAULT_FUNC_VERSION ;
81- const destination = core . getInput ( 'destination' ) || process . cwd ( ) ;
82- let bin = core . getInput ( 'name' ) || 'func' ;
83- if ( process . env . RUNNER_OS === 'Windows' && ! bin . endsWith ( '.exe' ) ) {
84- bin += '.exe' ;
85- }
75+ const osBinName = core . getInput ( 'binary' ) || getOsBinName ( ) ;
76+ if ( osBinName === "unknown" ) {
77+ core . setFailed ( "Invalid os binary determination, try setting it specifically using 'binary'" ) ;
78+ return ;
79+ }
8680
87- version = smartVersionUpdate ( version ) ;
88- if ( ! version ) return ;
81+ const versionInput = core . getInput ( 'version' ) || DEFAULT_FUNC_VERSION ;
82+ const destination = core . getInput ( 'destination' ) || process . cwd ( ) ;
83+ const binarySource = core . getInput ( 'binarySource' ) || DEFAULT_BINARY_SOURCE ;
84+ let bin = core . getInput ( 'name' ) || 'func' ;
85+ if ( process . env . RUNNER_OS === 'Windows' && ! bin . endsWith ( '.exe' ) ) {
86+ bin += '.exe' ;
87+ }
8988
90- const url = `https://github.com/knative/func/releases/download/${ version } /${ osBin } ` ;
91- core . info ( `URL: ${ url } ` ) ;
89+ let version ;
90+ try {
91+ version = smartVersionUpdate ( versionInput ) ;
92+ } catch ( error ) {
93+ core . setFailed ( error . message ) ;
94+ return ;
95+ }
9296
93- const fullPathBin = path . resolve ( destination , bin ) ;
97+ if ( ! fs . existsSync ( destination ) ) {
98+ fs . mkdirSync ( destination , { recursive : true } ) ;
99+ }
94100
95- await cmdConstructAndRun ( url , fullPathBin ) ;
96- await addBinToPath ( fullPathBin ) ;
97- await exec . exec ( fullPathBin , [ 'version' ] ) ;
101+ const fullPathBin = path . resolve ( destination , bin ) ;
98102
103+ try {
104+ await downloadFuncBinary ( version , osBinName , fullPathBin , binarySource ) ;
99105 } catch ( error ) {
100- core . setFailed ( error . message ) ;
106+ core . setFailed ( `Download failed: ${ error . message } ` ) ;
107+ return ;
101108 }
109+
110+ await addBinToPath ( fullPathBin ) ;
111+ await exec . exec ( fullPathBin , [ 'version' ] ) ;
102112}
103113
104114run ( ) ;
0 commit comments