Skip to content

Commit

Permalink
feat: initium project language flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Pehesi97 committed Sep 8, 2023
1 parent b4910a3 commit b207827
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/onbranch.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build on branch

on:
push:
branches-ignore:
- "main"
push:
branches-ignore:
- "main"

jobs:
cli:
Expand All @@ -23,4 +23,5 @@ jobs:
env:
INITIUM_APP_NAME: initium-cli
INITIUM_REGISTRY_USER: ${{ github.actor }}
INITIUM_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
INITIUM_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
INITIUM_PROJECT_LANGUAGE: go
5 changes: 3 additions & 2 deletions .github/workflows/onmain.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Deploy on main

on:
on:
push:
branches:
- main
Expand All @@ -21,4 +21,5 @@ jobs:
env:
INITIUM_APP_NAME: initium-cli
INITIUM_REGISTRY_USER: ${{ github.actor }}
INITIUM_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
INITIUM_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
INITIUM_PROJECT_LANGUAGE: go
2 changes: 2 additions & 0 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (c icli) baseBeforeFunc(ctx *cli.Context) error {
func (c *icli) init(cCtx *cli.Context) error {
appName := cCtx.String(appNameFlag)
version := cCtx.String(appVersionFlag)
projectLanguage := cCtx.String(projectLanguageFlag)
projectDirectory := cCtx.String(projectDirectoryFlag)
absProjectDirectory, err := filepath.Abs(cCtx.String(projectDirectoryFlag))
registry := cCtx.String(repoNameFlag)
Expand All @@ -80,6 +81,7 @@ func (c *icli) init(cCtx *cli.Context) error {

project := project.New(
appName,
projectLanguage,
projectDirectory,
cCtx.String(runtimeVersionFlag),
version,
Expand Down
7 changes: 7 additions & 0 deletions src/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
appNameFlag string = "app-name"
appVersionFlag string = "app-version"
projectDirectoryFlag string = "project-directory"
projectLanguageFlag string = "project-language"
repoNameFlag string = "container-registry"
dockerFileNameFlag string = "dockerfile-name"
configFileFlag string = "config-file"
Expand Down Expand Up @@ -136,6 +137,12 @@ func InitFlags() flags {
Value: defaults.ProjectDirectory,
EnvVars: []string{"INITIUM_PROJECT_DIRECTORY"},
},
&cli.StringFlag{
Name: projectLanguageFlag,
Usage: "The project language (node, go)",
Value: defaults.ProjectLanguage,
EnvVars: []string{"INITIUM_PROJECT_LANGUAGE"},
},
&cli.StringFlag{
Name: configFileFlag,
Usage: "read parameters from config",
Expand Down
1 change: 1 addition & 0 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (c icli) InitConfigCMD(ctx *cli.Context) error {
appVersionFlag,
namespaceFlag,
configFileFlag,
projectLanguageFlag,
projectDirectoryFlag,
destinationFolderFlag,
tokenFlag,
Expand Down
25 changes: 23 additions & 2 deletions src/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
type Project struct {
Name string
Version string
Language string
Directory string
RuntimeVersion string
DefaultRuntimeVersion string
Expand All @@ -43,9 +44,10 @@ func GuessAppName() *string {
return &name
}

func New(name string, directory string, runtimeVersion string, version string, resources fs.FS) Project {
func New(name string, language string, directory string, runtimeVersion string, version string, resources fs.FS) Project {
return Project{
Name: name,
Language: language,
Directory: directory,
RuntimeVersion: runtimeVersion,
Resources: resources,
Expand All @@ -65,8 +67,27 @@ func (proj *Project) detectType() (ProjectType, error) {
}
}

func (proj *Project) matchType() (ProjectType, error) {
switch proj.Language {
case "node":
proj.DefaultRuntimeVersion = defaults.DefaultNodeRuntimeVersion
return NodeProject, nil
case "go":
proj.DefaultRuntimeVersion = defaults.DefaultGoRuntimeVersion
return GoProject, nil
default:
return "", fmt.Errorf("cannot detect project type %s", proj.Language)
}
}

func (proj Project) loadDockerfile() ([]byte, error) {
projectType, err := proj.detectType()
var projectType ProjectType
var err error
if proj.Language == "auto" {
projectType, err = proj.detectType()
} else {
projectType, err = proj.matchType()
}
if err != nil {
return []byte{}, err
}
Expand Down
8 changes: 7 additions & 1 deletion src/services/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ func TestDetectType(t *testing.T) {
test_proj_type := Project{Name: string(project_type),
Directory: path.Join(root, props["directory"])}

proj_type, err := test_proj_type.detectType()
var proj_type ProjectType
var err error
if test_proj_type.Language == "auto" {
proj_type, err = test_proj_type.detectType()
} else {
proj_type, err = test_proj_type.matchType()
}

// if we cannot autodetect a project we will return an error
if project_type == "invalid" && err != nil {
Expand Down
1 change: 1 addition & 0 deletions src/utils/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package defaults

const (
ProjectDirectory string = "."
ProjectLanguage string = "auto"
RepoName string = "ghcr.io/nearform"
GithubActionFolder string = ".github/workflows"
GithubDefaultBranch string = "main"
Expand Down

0 comments on commit b207827

Please sign in to comment.