-
Notifications
You must be signed in to change notification settings - Fork 472
Add Go (Golang) language support to func init via the Native worker runtime #4854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harshivcodes
wants to merge
5
commits into
feature/native-lang-support
Choose a base branch
from
hakkaraj/func-init-native
base: feature/native-lang-support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b047392
feat: Add Go language support to func init via Native worker runtime
03a6b24
Add TryNormalizeLanguage to preserve language inference for existing …
fda4b68
fix: add missing FluentAssertions using in NativeGoInitTests
52d7822
Address pr feedback
ce8db8a
Address pr feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Cli/func/Actions/LocalActions/InitAction/InitNativeSubcommandAction.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using Fclp; | ||
|
|
||
| namespace Azure.Functions.Cli.Actions.LocalActions | ||
| { | ||
| [Action(Name = "init native", ParentCommandName = "init", ShowInHelp = true, HelpText = "Options specific to native runtime apps when running func init")] | ||
| internal class InitNativeSubcommandAction : BaseAction | ||
| { | ||
| public override ICommandLineParserResult ParseArgs(string[] args) | ||
| { | ||
| Parser | ||
| .Setup<string>('l', "language") | ||
| .WithDescription("The language for the function app. Options: golang.") | ||
|
harshivcodes marked this conversation as resolved.
Outdated
|
||
| .Callback(_ => { }); | ||
|
|
||
| Parser | ||
| .Setup<bool>("skip-go-mod-tidy") | ||
| .WithDescription("Skip running 'go mod tidy' after project creation.") | ||
|
harshivcodes marked this conversation as resolved.
Outdated
|
||
| .Callback(_ => { }); | ||
|
|
||
| return base.ParseArgs(args); | ||
| } | ||
|
|
||
| public override Task RunAsync() | ||
| { | ||
| // This method is never called - the main InitAction handles execution | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
| using Azure.Functions.Cli.Common; | ||
| using Colors.Net; | ||
| using static Azure.Functions.Cli.Common.OutputTheme; | ||
|
|
||
| namespace Azure.Functions.Cli.Helpers | ||
| { | ||
| public static class GoHelpers | ||
| { | ||
| private const int MinimumGoMajorVersion = 1; | ||
| private const int MinimumGoMinorVersion = 24; | ||
|
|
||
| public static async Task<WorkerLanguageVersionInfo> GetEnvironmentGoVersion() | ||
| { | ||
| return await GetVersion("go"); | ||
| } | ||
|
|
||
| public static void AssertGoVersion(WorkerLanguageVersionInfo goVersion) | ||
| { | ||
| if (goVersion?.Version == null) | ||
| { | ||
| throw new CliException( | ||
| $"Could not find a Go installation. Go {MinimumGoMajorVersion}.{MinimumGoMinorVersion} or later is required. " + | ||
| "Please install Go from https://go.dev/dl/"); | ||
| } | ||
|
|
||
| if (GlobalCoreToolsSettings.IsVerbose) | ||
| { | ||
| ColoredConsole.WriteLine(VerboseColor($"Found Go version {goVersion.Version} ({goVersion.ExecutablePath}).")); | ||
| } | ||
|
|
||
| if (goVersion.Major == null || goVersion.Minor == null) | ||
| { | ||
| throw new CliException( | ||
| $"Unable to parse Go version '{goVersion.Version}'. " + | ||
| $"Go {MinimumGoMajorVersion}.{MinimumGoMinorVersion} or later is required."); | ||
| } | ||
|
|
||
| // Accept any major version > 1, or major == 1 with minor >= 24 | ||
| if (goVersion.Major > MinimumGoMajorVersion) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (goVersion.Major == MinimumGoMajorVersion && goVersion.Minor >= MinimumGoMinorVersion) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| throw new CliException( | ||
| $"Go version {goVersion.Version} is not supported. " + | ||
| $"Go {MinimumGoMajorVersion}.{MinimumGoMinorVersion} or later is required. " + | ||
| "Please update Go from https://go.dev/dl/"); | ||
| } | ||
|
|
||
| public static async Task SetupProject(string moduleName, bool skipGoModTidy) | ||
| { | ||
| var goVersion = await GetEnvironmentGoVersion(); | ||
| AssertGoVersion(goVersion); | ||
|
|
||
| // Initialize: Run go mod init | ||
| var modInitExe = new Executable("go", $"mod init {moduleName}"); | ||
| var modInitExitCode = await modInitExe.RunAsync( | ||
| l => ColoredConsole.WriteLine(l), | ||
| e => ColoredConsole.Error.WriteLine(ErrorColor(e))); | ||
|
harshivcodes marked this conversation as resolved.
Outdated
|
||
| if (modInitExitCode != 0) | ||
| { | ||
| throw new CliException($"Failed to initialize Go module. 'go mod init {moduleName}' exited with code {modInitExitCode}."); | ||
| } | ||
|
|
||
| // Fetch the Azure Functions Go worker dependency | ||
| var goGetExe = new Executable("go", "get github.com/azure/azure-functions-golang-worker"); | ||
| var goGetExitCode = await goGetExe.RunAsync( | ||
| l => ColoredConsole.WriteLine(l), | ||
| e => ColoredConsole.Error.WriteLine(ErrorColor(e))); | ||
| if (goGetExitCode != 0) | ||
| { | ||
| throw new CliException("Failed to add Azure Functions Go worker dependency. 'go get' exited with a non-zero code."); | ||
| } | ||
|
|
||
| if (!skipGoModTidy) | ||
| { | ||
| var tidyExe = new Executable("go", "mod tidy"); | ||
| var tidyExitCode = await tidyExe.RunAsync( | ||
| l => ColoredConsole.WriteLine(l), | ||
| e => ColoredConsole.Error.WriteLine(ErrorColor(e))); | ||
| if (tidyExitCode != 0) | ||
| { | ||
| ColoredConsole.WriteLine(WarningColor("Warning: 'go mod tidy' exited with a non-zero code. You may need to run it manually.")); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| ColoredConsole.WriteLine(AdditionalInfoColor("Skipped \"go mod tidy\". You must run \"go mod tidy\" manually.")); | ||
| } | ||
| } | ||
|
|
||
| private static async Task<WorkerLanguageVersionInfo> GetVersion(string goExe) | ||
| { | ||
| try | ||
| { | ||
| var exe = new Executable(goExe, "version"); | ||
|
harshivcodes marked this conversation as resolved.
|
||
| var sb = new StringBuilder(); | ||
| var exitCode = await exe.RunAsync(l => sb.AppendLine(l), e => sb.AppendLine(e)); | ||
|
|
||
| if (exitCode == 0) | ||
| { | ||
| var output = sb.ToString().Trim(); | ||
|
|
||
| // Parse "go version go1.24.2 linux/amd64" format | ||
| var match = Regex.Match(output, @"go(\d+\.\d+(?:\.\d+)?)"); | ||
| if (match.Success) | ||
| { | ||
| return new WorkerLanguageVersionInfo(WorkerRuntime.Native, match.Groups[1].Value, goExe); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // Go is not installed or not on PATH | ||
|
harshivcodes marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.