Skip to content

Commit

Permalink
GH Action Build Trigger Selection Added and improved with lipgloss
Browse files Browse the repository at this point in the history
  • Loading branch information
chamodshehanka committed Apr 30, 2024
1 parent db4ce97 commit 359cbb7
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 40 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ require (
github.com/charmbracelet/bubbletea v0.25.0 // indirect
github.com/charmbracelet/huh v0.3.0 // indirect
github.com/charmbracelet/lipgloss v0.10.0 // indirect
github.com/charmbracelet/log v0.4.0 // indirect
github.com/containerd/console v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ github.com/charmbracelet/huh v0.3.0 h1:CxPplWkgW2yUTDDG0Z4S5HH8SJOosWHd4LxCvi0Xs
github.com/charmbracelet/huh v0.3.0/go.mod h1:fujUdKX8tC45CCSaRQdw789O6uaCRwx8l2NDyKfC4jA=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
25 changes: 7 additions & 18 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
package constants

import "github.com/charmbracelet/huh"

const (
AppCI = "ci"
Deployment = "deployment"
Publish = "publish"
)
ActionTypeAppCI = "Continuous Integration (CI)"
ActionTypeDeployment = "Continuous Deployment (CD)"
ActionTypePublish = "Publish Package"

func GetActionTypeOptions(actionType string) []huh.Option[string] {
switch actionType {
case AppCI:
return GetAppCIOptions()
case Deployment:
return nil
case Publish:
return nil
default:
return nil
}
}
TriggerOnCommit = "OnCommit"
TriggerOnWorkflowDispatch = "OnWorkflowDispatch"
TriggerOnPullRequest = "OnPullRequest"
)
48 changes: 37 additions & 11 deletions internal/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ import (
"fmt"
"github.com/chamodshehanka/gh-action-gen/internal/constants"
"github.com/chamodshehanka/gh-action-gen/internal/directory"
"github.com/chamodshehanka/gh-action-gen/internal/options"
"github.com/chamodshehanka/gh-action-gen/internal/utils"
"github.com/chamodshehanka/gh-action-gen/internal/workflow"
"github.com/charmbracelet/huh"
"log"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
)

var (
actionType string
applicationType string
extensions []string
actionTriggers []string
name string
branch string
)

func LoadInitForm() {
form := huh.NewForm(
huh.NewGroup(
// Ask the user for the type of GitHub Action they want to create.
huh.NewSelect[string]().
Title("Choose your GitHub Action Type").
Title("Choose the GitHub Action type you want to create:").
Options(
huh.NewOption("App CI", "ci"),
huh.NewOption("Deployment", "deployment"),
huh.NewOption("Publish", "publish"),
huh.NewOption(constants.ActionTypeAppCI, constants.ActionTypeAppCI),
huh.NewOption(constants.ActionTypeDeployment, constants.ActionTypeDeployment),
huh.NewOption(constants.ActionTypePublish, constants.ActionTypePublish),
).
Value(&actionType), // store the chosen option in the "actionType" variable
Value(&actionType),
),
)

Expand All @@ -36,7 +39,14 @@ func LoadInitForm() {
log.Fatal(err)
}

actionTypeOptions := constants.GetActionTypeOptions(actionType)
style := lipgloss.NewStyle().
Foreground(lipgloss.Color("205")).
Bold(true).PaddingLeft(2)

// Use the style
fmt.Println(style.Render("GitHub Action type: "), actionType)

actionTypeOptions := options.GetActionTypeOptions(actionType)
if actionTypeOptions == nil {
log.Fatal("Not implemented yet!")
return
Expand All @@ -55,9 +65,25 @@ func LoadInitForm() {
log.Fatal(err)
}

fmt.Println("Application Type: ", applicationType)
fmt.Println("Action Type: ", actionType)
fmt.Println(style.Render("Application technology: "), applicationType)

form = huh.NewForm(
huh.NewGroup(
huh.NewMultiSelect[string]().Title("When should this action be triggered?").Options(
huh.NewOption("On a new commit", constants.TriggerOnCommit),
huh.NewOption("On a Pull Request", constants.TriggerOnPullRequest),
huh.NewOption("On Workflow Dispatch", constants.TriggerOnWorkflowDispatch),
).Value(&actionTriggers),

huh.NewInput().Title("What's the branch name? (default 'main' will be used)").Value(&branch),
),
)
err = form.Run()
if err != nil {
log.Fatal(err)
}

fmt.Println(style.Render("GitHub Action will be triggered on: "), utils.StringArrayToString(actionTriggers))
err = createWorkflowFile()
if err != nil {
log.Fatal(err)
Expand Down
8 changes: 1 addition & 7 deletions internal/constants/app_ci.go → internal/options/app_ci.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package constants
package options

import "github.com/charmbracelet/huh"

type Option[T comparable] struct {
Key string
Value T
selected bool
}

func GetAppCIOptions() []huh.Option[string] {
return []huh.Option[string]{
huh.NewOption("Golang", "golang"),
Expand Down
19 changes: 19 additions & 0 deletions internal/options/options_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package options

import (
"github.com/chamodshehanka/gh-action-gen/internal/constants"
"github.com/charmbracelet/huh"
)

func GetActionTypeOptions(actionType string) []huh.Option[string] {
switch actionType {
case constants.ActionTypeAppCI:
return GetAppCIOptions()
case constants.ActionTypeDeployment:
return nil
case constants.ActionTypePublish:
return nil
default:
return nil
}
}
15 changes: 15 additions & 0 deletions internal/utils/common_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

func StringArrayToString(array []string) string {
triggerString := ""
for _, trigger := range array {
triggerString += trigger + ", "
}

// Remove the trailing comma and space
if len(triggerString) > 0 {
triggerString = triggerString[:len(triggerString)-2]
}

return triggerString
}
41 changes: 37 additions & 4 deletions internal/workflow/workflow.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package workflow

import (
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"strings"
)

func CreateWorkflowFile(applicationType string, actionType string) error {
filePath := ".github/workflows/" + applicationType + ".yml"
filePath, err := generateFileName(applicationType, actionType)
if err != nil {
return err
}

yamlContent := `name: Go CI
on:
Expand All @@ -26,10 +32,10 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up Go 1.21
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.21
Expand All @@ -42,11 +48,38 @@ jobs:
- name: Install dependencies
run: go mod tidy
`
err := ioutil.WriteFile(filePath, []byte(yamlContent), 0644)
err = ioutil.WriteFile(filePath, []byte(yamlContent), 0644)
if err != nil {
return err
}

fmt.Println("Config file created successfully at: ", filePath)
return nil
}

func generateFileName(applicationType string, actionType string) (string, error) {
filePath := ".github/workflows/" + applicationType + "-" + actionType

filePath = strings.ReplaceAll(filePath, " ", "-")
filePath = strings.ReplaceAll(filePath, "(", "")
filePath = strings.ReplaceAll(filePath, ")", "")
filePath = strings.ReplaceAll(filePath, "", "")
filePath = strings.ToLower(filePath)

randomString, err := generateRandomString(8)
if err != nil {
return "", err
}

filePath = filePath + "-" + randomString + ".yml"

return filePath, nil
}

func generateRandomString(length int8) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes)[:length], nil
}

0 comments on commit 359cbb7

Please sign in to comment.