mirror of
				https://github.com/actions/setup-go.git
				synced 2025-10-30 23:06:22 +00:00 
			
		
		
		
	Add go-version-file option (#62)
This commit is contained in:
		| @@ -4,6 +4,7 @@ import * as path from 'path'; | ||||
| import * as semver from 'semver'; | ||||
| import * as httpm from '@actions/http-client'; | ||||
| import * as sys from './system'; | ||||
| import fs from 'fs'; | ||||
| import os from 'os'; | ||||
|  | ||||
| type InstallationType = 'dist' | 'manifest'; | ||||
| @@ -298,3 +299,14 @@ export function makeSemver(version: string): string { | ||||
|   } | ||||
|   return fullVersion; | ||||
| } | ||||
|  | ||||
| export function parseGoVersionFile(versionFilePath: string): string { | ||||
|   const contents = fs.readFileSync(versionFilePath).toString(); | ||||
|  | ||||
|   if (path.basename(versionFilePath) === 'go.mod') { | ||||
|     const match = contents.match(/^go (\d+(\.\d+)*)/m); | ||||
|     return match ? match[1] : ''; | ||||
|   } | ||||
|  | ||||
|   return contents.trim(); | ||||
| } | ||||
|   | ||||
							
								
								
									
										28
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/main.ts
									
									
									
									
									
								
							| @@ -13,7 +13,7 @@ export async function run() { | ||||
|     // versionSpec is optional.  If supplied, install / use from the tool cache | ||||
|     // If not supplied then problem matchers will still be setup.  Useful for self-hosted. | ||||
|     // | ||||
|     let versionSpec = core.getInput('go-version'); | ||||
|     const versionSpec = resolveVersionInput(); | ||||
|  | ||||
|     core.info(`Setup go version spec ${versionSpec}`); | ||||
|  | ||||
| @@ -104,3 +104,29 @@ export function parseGoVersion(versionString: string): string { | ||||
|   // expecting go<version> for runtime.Version() | ||||
|   return versionString.split(' ')[2].slice('go'.length); | ||||
| } | ||||
|  | ||||
| function resolveVersionInput(): string { | ||||
|   let version = core.getInput('go-version'); | ||||
|   const versionFilePath = core.getInput('go-version-file'); | ||||
|  | ||||
|   if (version && versionFilePath) { | ||||
|     core.warning( | ||||
|       'Both go-version and go-version-file inputs are specified, only go-version will be used' | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   if (version) { | ||||
|     return version; | ||||
|   } | ||||
|  | ||||
|   if (versionFilePath) { | ||||
|     if (!fs.existsSync(versionFilePath)) { | ||||
|       throw new Error( | ||||
|         `The specified go version file at: ${versionFilePath} does not exist` | ||||
|       ); | ||||
|     } | ||||
|     version = installer.parseGoVersionFile(versionFilePath); | ||||
|   } | ||||
|  | ||||
|   return version; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user