11const core = require ( '@actions/core' ) ;
2- const exec = require ( '@actions/exec' )
3- const path = require ( 'path' )
2+ const exec = require ( '@actions/exec' ) ;
3+ const path = require ( 'path' ) ;
44const fs = require ( 'fs' ) ;
55
6+ // Static version - update manually when new func releases are available
7+ const DEFAULT_FUNC_VERSION = 'knative-v1.20.1' ;
68
7- // change this accordingly
8- const DEFAULT_FUNC_VERSION = 'knative-v1.16.1'
9-
10- // detect os system in Github Actions and determine binary name
9+ // Returns the binary name for the current OS/arch from GitHub releases
1110function getOsBinName ( ) {
1211 const runnerOS = process . env . RUNNER_OS ;
1312 const runnerArch = process . env . RUNNER_ARCH ;
@@ -23,102 +22,79 @@ function getOsBinName() {
2322 } else if ( runnerOS === 'macOS' ) {
2423 return runnerArch === 'X64' ? 'func_darwin_amd64' : 'func_darwin_arm64' ;
2524 } else if ( runnerOS === 'Windows' ) {
26- return 'func_windows_amd64' ;
25+ return 'func_windows_amd64.exe ' ;
2726 } else {
2827 return 'unknown' ;
2928 }
3029}
3130
32- // smartVersionParse will check validity of given version and fill in the parts
33- // to make it correct if possible.
34- // Ex.: '1.16' or 'v1.16' will return 'v1.16.0'
31+ // Normalizes version to release tag format: knative-vX.Y.Z
32+ // Ex.: '1.16' or 'v1.16' will return 'knative-v1.16.0'
3533function smartVersionUpdate ( version ) {
36- versionRegex = / ^ (?< knprefix > k n a t i v e - ) ? (?< prefix > v ? ) (?< major > \d + ) \. (?< minor > \d + ) ( .(?< patch > \d + ) ) ? $ / ;
34+ const versionRegex = / ^ (?< knprefix > k n a t i v e - ) ? (?< prefix > v ? ) (?< major > \d + ) \. (?< minor > \d + ) ( .(?< patch > \d + ) ) ? $ / ;
3735 let match = version . match ( versionRegex ) ;
38- if ( match ) {
39- if ( match . groups . knprefix == undefined ) {
40- match . groups . knprefix = "" ;
41- }
36+ if ( match ) {
37+ const knprefix = 'knative-' ;
4238 const prefix = 'v' ;
43- if ( match . groups . patch == undefined ) {
44- match . groups . patch = 0 ;
45- }
46- return `${ match . groups . knprefix } ${ prefix } ${ match . groups . major } .${ match . groups . minor } .${ match . groups . patch } ` ;
39+ const patch = match . groups . patch ?? 0 ;
40+ return `${ knprefix } ${ prefix } ${ match . groups . major } .${ match . groups . minor } .${ patch } ` ;
4741 }
4842
4943 core . setFailed ( `Invalid version format (${ version } ). Expected format: "1.16[.X]" or "v1.16[.X]"` ) ;
5044 return undefined ;
5145}
5246
53- /**
54- * @param {string } url - Full url to be curled
55- * @param {string } binPath - Full target path of the binary
56- */
57- // download func, set as executable
58- async function cmdConstructAndRun ( url , binPath ) {
59- const cmd = `curl -L -o "${ binPath } " "${ url } "` ;
60- await exec . exec ( cmd ) ;
47+ // Downloads binary and makes it executable
48+ async function cmdConstructAndRun ( url , binPath ) {
49+ await exec . exec ( 'curl' , [ '-L' , '-o' , binPath , url ] ) ;
6150
62- //check if downloaded successfully
63- if ( ! fs . existsSync ( binPath ) ) {
51+ if ( ! fs . existsSync ( binPath ) ) {
6452 core . setFailed ( "Download failed, couldn't find the binary on disk" ) ;
53+ return ;
6554 }
6655
67- await exec . exec ( `chmod +x ${ binPath } ` ) ;
56+ if ( process . env . RUNNER_OS !== 'Windows' ) {
57+ await exec . exec ( 'chmod' , [ '+x' , binPath ] ) ;
58+ }
6859}
6960
70- /**
71- * @param {string } binPath - full path to Func binary
72- * */
73- async function addBinToPath ( binPath ) {
74- dir = path . dirname ( binPath )
75- // Write to $GITHUB_PATH, making it available for subsequent steps
61+ // Adds binary directory to PATH for current and subsequent steps
62+ async function addBinToPath ( binPath ) {
63+ const dir = path . dirname ( binPath ) ;
7664 fs . appendFileSync ( process . env . GITHUB_PATH , `\n${ dir } ` ) ;
7765
78- // add only if its not in PATH yet
79- if ( ! process . env . PATH . includes ( dir ) ) {
66+ if ( ! process . env . PATH . includes ( dir ) ) {
8067 process . env . PATH = process . env . PATH + path . delimiter + dir ;
81- core . info ( `dir ${ dir } added to $ PATH` ) ;
68+ core . info ( `${ dir } added to PATH` ) ;
8269 }
83- }
70+ }
8471
85- // -------------------------------------------------------------------------- \\
86- async function run ( ) {
72+ async function run ( ) {
8773 try {
88-
89- // Fetch value of inputs specified in action.yml or use defaults
90-
91- // osBin refers to the exact name match of an existing binary available to
92- // download
9374 const osBin = core . getInput ( 'binary' ) || getOsBinName ( ) ;
94- if ( osBin == "unknown" ) {
75+ if ( osBin === "unknown" ) {
9576 core . setFailed ( "Invalid os binary determination, try setting it specifically using 'binary'" ) ;
77+ return ;
9678 }
97- // version to be downloaded
98- let version = core . getInput ( 'version' ) || DEFAULT_FUNC_VERSION
99- // destination is a directory where to download the Func
79+
80+ let version = core . getInput ( 'version' ) || DEFAULT_FUNC_VERSION ;
10081 const destination = core . getInput ( 'destination' ) || process . cwd ( ) ;
101- // bin refers to the name of the binary (Func) that will be downloaded (this
102- // is what it will be called)
103- const bin = core . getInput ( 'name' ) || 'func' ;
82+ let bin = core . getInput ( 'name' ) || 'func' ;
83+ if ( process . env . RUNNER_OS === 'Windows' && ! bin . endsWith ( '.exe' ) ) {
84+ bin += '.exe' ;
85+ }
10486
10587 version = smartVersionUpdate ( version ) ;
88+ if ( ! version ) return ;
10689
107- var url = `https://github.com/knative/func/releases/download/${ version } /${ osBin } ` ;
108- console . log ( `URL: ${ url } ` ) ;
109-
110- fullPathBin = path . resolve ( destination , bin ) ;
90+ const url = `https://github.com/knative/func/releases/download/${ version } /${ osBin } ` ;
91+ core . info ( `URL: ${ url } ` ) ;
11192
112- // download Func
113- await cmdConstructAndRun ( url , fullPathBin ) ;
93+ const fullPathBin = path . resolve ( destination , bin ) ;
11494
115- // add final binary to PATH (directory where the bin is ) and add it to
116- // GITHUB_PATH so it can be used bo subsequent 'runs'
95+ await cmdConstructAndRun ( url , fullPathBin ) ;
11796 await addBinToPath ( fullPathBin ) ;
118-
119- await exec . exec ( "ls -la" )
120- // run 'func version' as a test
121- await exec . exec ( `${ bin } version` ) ;
97+ await exec . exec ( fullPathBin , [ 'version' ] ) ;
12298
12399 } catch ( error ) {
124100 core . setFailed ( error . message ) ;
0 commit comments