Skip to content
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

feat: initium project type flag #101

Merged
merged 21 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
projectType := cCtx.String(projectTypeFlag)
projectDirectory := cCtx.String(projectDirectoryFlag)
absProjectDirectory, err := filepath.Abs(cCtx.String(projectDirectoryFlag))
registry := cCtx.String(repoNameFlag)
Expand All @@ -81,6 +82,7 @@ func (c *icli) init(cCtx *cli.Context) error {

project := project.New(
appName,
project.ProjectType(projectType),
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"
projectTypeFlag string = "project-type"
repoNameFlag string = "container-registry"
dockerFileNameFlag string = "dockerfile-name"
configFileFlag string = "config-file"
Expand Down Expand Up @@ -150,6 +151,12 @@ func InitFlags() flags {
Value: defaults.ProjectDirectory,
EnvVars: []string{"INITIUM_PROJECT_DIRECTORY"},
},
&cli.StringFlag{
Name: projectTypeFlag,
Usage: "The project type (node, go)",
Value: defaults.ProjectType,
EnvVars: []string{"INITIUM_PROJECT_TYPE"},
},
&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 @@ -48,6 +48,7 @@ func excludedFlagsFromConfig() []string {
appVersionFlag,
namespaceFlag,
configFileFlag,
projectTypeFlag,
projectDirectoryFlag,
destinationFolderFlag,
tokenFlag,
Expand Down
9 changes: 5 additions & 4 deletions src/cli/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cli
import (
"bytes"
"fmt"
"github.com/nearform/initium-cli/src/services/project"
"github.com/urfave/cli/v2"
"gotest.tools/v3/assert"
"os"
"path"
"testing"

"github.com/nearform/initium-cli/src/services/project"
"github.com/urfave/cli/v2"
"gotest.tools/v3/assert"
)

const root = "../../"
Expand Down Expand Up @@ -61,7 +62,7 @@ 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),
Expand Down
41 changes: 36 additions & 5 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
Type ProjectType
Directory string
RuntimeVersion string
DefaultRuntimeVersion string
Expand All @@ -44,9 +45,10 @@ func GuessAppName() *string {
return &name
}

func New(name 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,
Type: projectType,
Directory: directory,
RuntimeVersion: runtimeVersion,
ImagePullSecrets: imagePullSecrets,
Expand All @@ -56,19 +58,48 @@ func New(name string, directory string, runtimeVersion string, version string, i
}

func (proj *Project) detectType() (ProjectType, error) {
var detectedRuntimes []ProjectType
var projectType ProjectType
if _, err := os.Stat(path.Join(proj.Directory, "package.json")); err == nil {
proj.DefaultRuntimeVersion = defaults.DefaultNodeRuntimeVersion
detectedRuntimes = append(detectedRuntimes, NodeProject)
projectType = NodeProject
}
if _, err := os.Stat(path.Join(proj.Directory, "go.mod")); err == nil {
proj.DefaultRuntimeVersion = defaults.DefaultGoRuntimeVersion
detectedRuntimes = append(detectedRuntimes, GoProject)
projectType = GoProject
}
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.Type {
case "node":
Pehesi97 marked this conversation as resolved.
Show resolved Hide resolved
proj.DefaultRuntimeVersion = defaults.DefaultNodeRuntimeVersion
return NodeProject, nil
} else if _, err := os.Stat(path.Join(proj.Directory, "go.mod")); err == nil {
case "go":
Pehesi97 marked this conversation as resolved.
Show resolved Hide resolved
proj.DefaultRuntimeVersion = defaults.DefaultGoRuntimeVersion
return GoProject, nil
} else {
return "", fmt.Errorf("cannot detect project type %v", err)
default:
return "", fmt.Errorf("cannot detect project type %s", proj.Type)
}
}

func (proj Project) loadDockerfile() ([]byte, error) {
projectType, err := proj.detectType()
var projectType ProjectType
var err error
if proj.Type != "" {
projectType, err = proj.matchType()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the IsValidProjectType here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 120 or 121?
Because the function only returns a bool that indicates whether the project type is valid or not

} else {
projectType, err = proj.detectType()
}
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.Type == "" {
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 = "."
ProjectType string = ""
RepoName string = "ghcr.io/nearform"
GithubActionFolder string = ".github/workflows"
GithubDefaultBranch string = "main"
Expand Down