-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactorings and new features
Signed-off-by: Thorsten Hans <[email protected]>
- Loading branch information
1 parent
8182b31
commit 3f596f2
Showing
14 changed files
with
542 additions
and
242 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,70 +1,89 @@ | ||
package gh | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/thorstenhans/spin-gh-plugin/internal/detective" | ||
gh "github.com/thorstenhans/spin-gh-plugin/internal/github" | ||
) | ||
|
||
type CreateActionOptions struct { | ||
Output string | ||
Name string | ||
ToolVersions gh.ToolVersions | ||
TemplatePath string | ||
Plugins []string | ||
Overwrite bool | ||
BranchName string | ||
DryRun bool | ||
gh.ActionTriggers | ||
Name string | ||
OperatingSystem string | ||
Output string | ||
Overwrite bool | ||
Plugins []string | ||
EnvironmentVariables []string | ||
TemplatePath string | ||
Tools gh.Tools | ||
} | ||
|
||
var options CreateActionOptions = CreateActionOptions{ | ||
ToolVersions: gh.DefaultToolVersions(), | ||
Tools: gh.DefaultTools(), | ||
} | ||
|
||
var createActionCmd = &cobra.Command{ | ||
Use: "create-action", | ||
Short: "Examines your Spin App and creates a working GitHub Action for CI", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
apps := detective.FindAllSpinApps() | ||
if len(apps) == 0 { | ||
fmt.Println("Could not find Spin App(s) under the current directory") | ||
os.Exit(1) | ||
log.Fatal("Could not find Spin App(s) under the current directory") | ||
} | ||
|
||
envVars, err := gh.ParseEnvVars(options.EnvironmentVariables) | ||
if err != nil { | ||
log.Fatalf("Invalid Environment variable provided %v", err) | ||
} | ||
|
||
renderOptions := gh.RenderActionOptions{ | ||
SpinApps: apps, | ||
Output: options.Output, | ||
Overwrite: options.Overwrite, | ||
Plugins: options.Plugins, | ||
TargetBranch: options.BranchName, | ||
CustomTemplatePath: options.TemplatePath, | ||
Name: options.Name, | ||
ToolVersions: options.ToolVersions, | ||
CustomTemplatePath: options.TemplatePath, | ||
DryRun: options.DryRun, | ||
Name: options.Name, | ||
OperatingSystem: options.OperatingSystem, | ||
Output: options.Output, | ||
Overwrite: options.Overwrite, | ||
Plugins: options.Plugins, | ||
SpinApps: apps, | ||
ActionTriggers: options.ActionTriggers, | ||
Tools: options.Tools, | ||
EnvironmentVariables: envVars, | ||
} | ||
err := gh.RenderAction(renderOptions) | ||
err = gh.RenderAction(renderOptions) | ||
if err != nil { | ||
fmt.Printf("Error while rendering template %v", err) | ||
os.Exit(1) | ||
log.Fatalf("Error while rendering template %v", err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
defaultPlugins := []string{"js2wasm", "py2wasm", "kube"} | ||
|
||
createActionCmd.Flags().StringVarP(&options.Output, "output", "o", ".github/workflows/ci.yaml", "Path where the GitHub Action will be created") | ||
|
||
createActionCmd.Flags().StringVarP(&options.Name, "name", "n", "CI", "Name for the GitHub Action") | ||
createActionCmd.Flags().BoolVarP(&options.ActionTriggers.ManualDispatch, "manual", "", false, "Trigger Action on workflow dispatch") | ||
createActionCmd.Flags().StringVarP(&options.ActionTriggers.Schedule, "cron", "", "", "Trigger Action on cron schedule") | ||
createActionCmd.Flags().StringVarP(&options.ActionTriggers.Push, "ci", "", "main", "Trigger Action for every push on the specified branch") | ||
createActionCmd.Flags().StringVarP(&options.ActionTriggers.PullRequest, "pr", "", "", "Trigger Action for every PR targeting the specified branch") | ||
|
||
createActionCmd.Flags().StringVarP(&options.ToolVersions.Rust, "rust-version", "", options.ToolVersions.Rust, "Set Rust version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.ToolVersions.Go, "go-version", "", options.ToolVersions.Go, "Set Go version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.ToolVersions.TinyGo, "tinygo-version", "", options.ToolVersions.TinyGo, "Set TinyGo version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.ToolVersions.Node, "node-version", "", options.ToolVersions.Node, "Set Node.js version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.ToolVersions.Python, "python-version", "", options.ToolVersions.Python, "Set Python version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.ToolVersions.Spin, "spin-version", "", "", "Set Spin version for GitHub Actions (default: current stable Spin release)") | ||
createActionCmd.Flags().StringVarP(&options.Tools.Rust, "rust-version", "", options.Tools.Rust, "Set Rust version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.Tools.Go, "go-version", "", options.Tools.Go, "Set Go version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.Tools.TinyGo, "tinygo-version", "", options.Tools.TinyGo, "Set TinyGo version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.Tools.Node, "node-version", "", options.Tools.Node, "Set Node.js version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.Tools.Python, "python-version", "", options.Tools.Python, "Set Python version for GitHub Actions") | ||
createActionCmd.Flags().StringVarP(&options.Tools.Spin, "spin-version", "", "", "Set Spin version for GitHub Actions (default: current stable Spin release)") | ||
|
||
createActionCmd.Flags().BoolVarP(&options.Overwrite, "overwrite", "", false, "Overwrite existing output file") | ||
createActionCmd.Flags().StringVarP(&options.TemplatePath, "template", "t", "", "Specify the path to a custom template for creating the GitHub Action") | ||
createActionCmd.Flags().StringSliceVarP(&options.Plugins, "plugins", "p", defaultPlugins, "Specify required Spin plugins") | ||
createActionCmd.Flags().StringVarP(&options.BranchName, "branch", "b", "main", "Specify the desired branch") | ||
createActionCmd.Flags().StringSliceVarP(&options.EnvironmentVariables, "env", "", []string{}, "Specify Environment Variables (format key=value)") | ||
createActionCmd.Flags().StringSliceVarP(&options.Plugins, "plugin", "p", []string{}, "Specify required Spin plugins") | ||
|
||
createActionCmd.Flags().StringVarP(&options.OperatingSystem, "os", "", "ubuntu-latest", "Specify the desired operating system for the GitHub Action") | ||
createActionCmd.Flags().BoolVarP(&options.DryRun, "dry-run", "", false, "Print GitHub Action to stdout instead of writing to a file") | ||
|
||
rootCmd.AddCommand(createActionCmd) | ||
} |
This file contains 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 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 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.