Skip to content

Commit

Permalink
fix: requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pehesi97 committed Oct 16, 2023
1 parent bcee7e3 commit 09896e2
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 40 deletions.
1 change: 0 additions & 1 deletion .github/workflows/onbranch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ jobs:
INITIUM_APP_NAME: initium-cli
INITIUM_REGISTRY_USER: ${{ github.actor }}
INITIUM_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
INITIUM_PROJECT_LANGUAGE: go
1 change: 0 additions & 1 deletion .github/workflows/onmain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ jobs:
INITIUM_APP_NAME: initium-cli
INITIUM_REGISTRY_USER: ${{ github.actor }}
INITIUM_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
INITIUM_PROJECT_LANGUAGE: go
4 changes: 2 additions & 2 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +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)
projectType := cCtx.String(projectTypeFlag)
projectDirectory := cCtx.String(projectDirectoryFlag)
absProjectDirectory, err := filepath.Abs(cCtx.String(projectDirectoryFlag))
registry := cCtx.String(repoNameFlag)
Expand All @@ -82,7 +82,7 @@ func (c *icli) init(cCtx *cli.Context) error {

project := project.New(
appName,
projectLanguage,
project.ProjectType(projectType),
projectDirectory,
cCtx.String(runtimeVersionFlag),
version,
Expand Down
10 changes: 5 additions & 5 deletions src/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
appNameFlag string = "app-name"
appVersionFlag string = "app-version"
projectDirectoryFlag string = "project-directory"
projectLanguageFlag string = "project-language"
projectTypeFlag string = "project-type"
repoNameFlag string = "container-registry"
dockerFileNameFlag string = "dockerfile-name"
configFileFlag string = "config-file"
Expand Down Expand Up @@ -152,10 +152,10 @@ func InitFlags() flags {
EnvVars: []string{"INITIUM_PROJECT_DIRECTORY"},
},
&cli.StringFlag{
Name: projectLanguageFlag,
Usage: "The project language (node, go)",
Value: defaults.ProjectLanguage,
EnvVars: []string{"INITIUM_PROJECT_LANGUAGE"},
Name: projectTypeFlag,
Usage: "The project type (node, go)",
Value: defaults.ProjectType,
EnvVars: []string{"INITIUM_PROJECT_TYPE"},
},
&cli.StringFlag{
Name: configFileFlag,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func excludedFlagsFromConfig() []string {
appVersionFlag,
namespaceFlag,
configFileFlag,
projectLanguageFlag,
projectTypeFlag,
projectDirectoryFlag,
destinationFolderFlag,
tokenFlag,
Expand Down
3 changes: 1 addition & 2 deletions src/cli/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ func TestShouldRenderDockerTemplate(t *testing.T) {

cCtx := cli.Context{}
instance := icli{
project: project.Project{
project: &project.Project{
Name: string(projectType),
Directory: path.Join(root, props["directory"]),
Resources: os.DirFS(root),
Language: "auto",
},
Writer: &buffer,
}
Expand Down
46 changes: 24 additions & 22 deletions src/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
type Project struct {
Name string
Version string
Language string
Type ProjectType
Directory string
RuntimeVersion string
DefaultRuntimeVersion string
Expand All @@ -45,58 +45,60 @@ func GuessAppName() *string {
return &name
}

func New(name string, language string, directory string, runtimeVersion string, version string, imagePullSecrets []string, resources fs.FS) Project {
func New(name string, projectType ProjectType, directory string, runtimeVersion string, version string, imagePullSecrets []string, resources fs.FS) Project {
return Project{
Name: name,
Language: language,
Directory: directory,
RuntimeVersion: runtimeVersion,
ImagePullSecrets: imagePullSecrets,
Resources: resources,
Version: version,
Name: name,
Type: projectType,
Directory: directory,
RuntimeVersion: runtimeVersion,
ImagePullSecrets: imagePullSecrets,
Resources: resources,
Version: version,
}
}

func (proj *Project) detectType() (ProjectType, error) {
detectedRuntimes := 0
var detectedRuntimes []ProjectType
var projectType ProjectType
if _, err := os.Stat(path.Join(proj.Directory, "package.json")); err == nil {
proj.DefaultRuntimeVersion = defaults.DefaultNodeRuntimeVersion
detectedRuntimes++
detectedRuntimes = append(detectedRuntimes, NodeProject)
projectType = NodeProject
} else if _, err := os.Stat(path.Join(proj.Directory, "go.mod")); err == nil {
}
if _, err := os.Stat(path.Join(proj.Directory, "go.mod")); err == nil {
proj.DefaultRuntimeVersion = defaults.DefaultGoRuntimeVersion
detectedRuntimes++
detectedRuntimes = append(detectedRuntimes, GoProject)
projectType = GoProject
} else {
return "", fmt.Errorf("cannot detect project type %v", err)
}
if detectedRuntimes > 1 {
return "", fmt.Errorf("more than one project runtime detected, use --project-language flag or the INITIUM_PROJECT_LANGUAGE env variable to set the desired runtime")
if len(detectedRuntimes) == 0 {
return "", fmt.Errorf("cannot detect project type")
}
if len(detectedRuntimes) > 1 {
return "", fmt.Errorf("more than one project runtimes detected (%v), use --project-type flag or the INITIUM_PROJECT_TYPE env variable to set the desired runtime", detectedRuntimes)
}
return projectType, nil
}

func (proj *Project) matchType() (ProjectType, error) {
switch proj.Language {
switch proj.Type {
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)
return "", fmt.Errorf("cannot detect project type %s", proj.Type)
}
}

func (proj Project) loadDockerfile() ([]byte, error) {
var projectType ProjectType
var err error
if proj.Language == "auto" {
projectType, err = proj.detectType()
} else {
if proj.Type != "" {
projectType, err = proj.matchType()
} else {
projectType, err = proj.detectType()
}
if err != nil {
return []byte{}, err
Expand Down
7 changes: 2 additions & 5 deletions src/services/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ func TestDetectType(t *testing.T) {

for project_type, props := range projects {
test_proj_type := Project{Name: string(project_type),
Directory: path.Join(root, props["directory"]),
Language: "auto"}
Directory: path.Join(root, props["directory"])}

var proj_type ProjectType
var err error
if test_proj_type.Language == "auto" {
if test_proj_type.Type == "" {
proj_type, err = test_proj_type.detectType()
} else {
proj_type, err = test_proj_type.matchType()
Expand All @@ -52,7 +51,6 @@ func TestLoadDockerfile(t *testing.T) {
for project_type, props := range projects {
proj_dockerfile := Project{Name: string(project_type),
Directory: path.Join(root, props["directory"]),
Language: "auto",
Resources: os.DirFS(root),
}
_, err := proj_dockerfile.loadDockerfile()
Expand All @@ -73,7 +71,6 @@ func TestCorrectRuntime(t *testing.T) {
proj_runtime := Project{
Name: "test",
Directory: path.Join(root, projects["node"]["directory"]),
Language: "auto",
Resources: os.DirFS(root),
RuntimeVersion: "30",
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package defaults

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

0 comments on commit 09896e2

Please sign in to comment.