Skip to content

Commit

Permalink
signing the commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidya2606 committed Oct 23, 2024
1 parent 118621f commit 135afd0
Show file tree
Hide file tree
Showing 154 changed files with 2,377 additions and 1,761 deletions.
16 changes: 8 additions & 8 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.
Please include the related issue if relevant, and motivation/context. Please also list any dependencies required for this change.

Fixes # (issue)
Feature # (details)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update
- [ ] Refactor

# How Has This Been Tested?
## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Is it a breaking change which will impact consuming tool(s).
Provide instructions so we can reproduce, and list any relevant details for your test configuration. Please mention if this is a breaking change which will impact consuming tool(s).

- [ ] Test A
- [ ] Test B
- [ ] Unit Test:
- [ ] E2E Test:
- [ ] Other Test:


# Checklist:
## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
name: draft Unit Tests
on:
pull_request:
paths:
- '**.go'
- 'go.mod'
- 'go.sum'
branches: [ main ]

jobs:
build:
Expand All @@ -16,4 +13,4 @@ jobs:
with:
go-version: 1.22
- name: make
run: make run-unit-tests
run: make run-unit-tests
86 changes: 44 additions & 42 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ import (

"gopkg.in/yaml.v3"

"github.com/Azure/draft/pkg/handlers"
"github.com/Azure/draft/pkg/reporeader"
"github.com/Azure/draft/pkg/reporeader/readers"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/deployments"
dryrunpkg "github.com/Azure/draft/pkg/dryrun"
"github.com/Azure/draft/pkg/filematches"
"github.com/Azure/draft/pkg/languages"
"github.com/Azure/draft/pkg/linguist"
"github.com/Azure/draft/pkg/prompts"
"github.com/Azure/draft/pkg/templatewriter"
"github.com/Azure/draft/pkg/templatewriter/writers"
"github.com/Azure/draft/template"
)

// ErrNoLanguageDetected is raised when `draft create` does not detect source
Expand Down Expand Up @@ -52,8 +50,6 @@ type createCmd struct {
createConfigPath string
createConfig *CreateConfig

supportedLangs *languages.Languages

templateWriter templatewriter.TemplateWriter
templateVariableRecorder config.TemplateVariableRecorder
repoReader reporeader.RepoReader
Expand Down Expand Up @@ -151,7 +147,7 @@ func (cc *createCmd) run() error {

// detectLanguage detects the language used in a project destination directory
// It returns the DraftConfig for that language and the name of the language
func (cc *createCmd) detectLanguage() (*config.DraftConfig, string, error) {
func (cc *createCmd) detectLanguage() (*handlers.Template, string, error) {
hasGo := false
hasGoMod := false
var langs []*linguist.Language
Expand Down Expand Up @@ -213,60 +209,64 @@ func (cc *createCmd) detectLanguage() (*config.DraftConfig, string, error) {
}
}

cc.supportedLangs = languages.CreateLanguagesFromEmbedFS(template.Dockerfiles, cc.dest)

if cc.createConfig.LanguageType != "" {
log.Debug("using configuration language")
lowerLang := strings.ToLower(cc.createConfig.LanguageType)
langConfig := cc.supportedLangs.GetConfig(lowerLang)
if langConfig == nil {
return nil, "", ErrNoLanguageDetected
langDockerfileTemplate, err := handlers.GetTemplate(fmt.Sprintf("dockerfile-%s", lowerLang), "", cc.dest, cc.templateWriter)
if err != nil {
return nil, "", err
}
if langDockerfileTemplate == nil {
return nil, "", fmt.Errorf("could not find a template for %s", cc.createConfig.LanguageType)
}

return langConfig, lowerLang, nil
return langDockerfileTemplate, lowerLang, nil
}

for _, lang := range langs {
detectedLang := linguist.Alias(lang)
log.Infof("--> Draft detected %s (%f%%)\n", detectedLang.Language, detectedLang.Percent)
lowerLang := strings.ToLower(detectedLang.Language)
if cc.supportedLangs.ContainsLanguage(lowerLang) {
if handlers.IsValidTemplate(fmt.Sprintf("dockerfile-%s", lowerLang)) {
if lowerLang == "go" && hasGo && hasGoMod {
log.Debug("detected go and go module")
lowerLang = "gomodule"
}
langConfig := cc.supportedLangs.GetConfig(lowerLang)
return langConfig, lowerLang, nil
langDockerfileTemplate, err := handlers.GetTemplate(fmt.Sprintf("dockerfile-%s", lowerLang), "", cc.dest, cc.templateWriter)
if err != nil {
return nil, "", err
}
if langDockerfileTemplate == nil {
return nil, "", fmt.Errorf("could not find a template for detected language %s", detectedLang.Language)
}
return langDockerfileTemplate, lowerLang, nil
}
log.Infof("--> Could not find a pack for %s. Trying to find the next likely language match...", detectedLang.Language)
}
return nil, "", ErrNoLanguageDetected
}

func (cc *createCmd) generateDockerfile(langConfig *config.DraftConfig, lowerLang string) error {
func (cc *createCmd) generateDockerfile(dockerfileTemplate *handlers.Template, lowerLang string) error {
log.Info("--- Dockerfile Creation ---")
if cc.supportedLangs == nil {
return errors.New("supported languages were loaded incorrectly")
}

// Extract language-specific defaults from repo
extractedValues, err := cc.supportedLangs.ExtractDefaults(lowerLang, cc.repoReader)
extractedValues, err := dockerfileTemplate.ExtractDefaults(lowerLang, cc.repoReader)
if err != nil {
return err
}

// Check for existing duplicate defaults
for k, v := range extractedValues {
variableExists := false
for i, variable := range langConfig.Variables {
for i, variable := range dockerfileTemplate.Config.Variables {
if k == variable.Name {
variableExists = true
langConfig.Variables[i].Default.Value = v
dockerfileTemplate.Config.Variables[i].Default.Value = v
break
}
}
if !variableExists {
langConfig.Variables = append(langConfig.Variables, &config.BuilderVar{
dockerfileTemplate.Config.Variables = append(dockerfileTemplate.Config.Variables, &config.BuilderVar{
Name: k,
Default: config.BuilderVarDefault{
Value: v,
Expand All @@ -276,25 +276,25 @@ func (cc *createCmd) generateDockerfile(langConfig *config.DraftConfig, lowerLan
}

if cc.createConfig.LanguageVariables == nil {
langConfig.VariableMapToDraftConfig(flagVariablesMap)
dockerfileTemplate.Config.VariableMapToDraftConfig(flagVariablesMap)

if err = prompts.RunPromptsFromConfigWithSkips(langConfig); err != nil {
if err = prompts.RunPromptsFromConfigWithSkips(dockerfileTemplate.Config); err != nil {
return err
}
} else {
err = validateConfigInputsToPrompts(langConfig, cc.createConfig.LanguageVariables)
err = validateConfigInputsToPrompts(dockerfileTemplate.Config, cc.createConfig.LanguageVariables)
if err != nil {
return err
}
}

if cc.templateVariableRecorder != nil {
for _, variable := range langConfig.Variables {
for _, variable := range dockerfileTemplate.Config.Variables {
cc.templateVariableRecorder.Record(variable.Name, variable.Value)
}
}

if err = cc.supportedLangs.CreateDockerfileForLanguage(lowerLang, langConfig, cc.templateWriter); err != nil {
if err = dockerfileTemplate.Generate(); err != nil {
return fmt.Errorf("there was an error when creating the Dockerfile for language %s: %w", cc.createConfig.LanguageType, err)
}

Expand All @@ -304,21 +304,20 @@ func (cc *createCmd) generateDockerfile(langConfig *config.DraftConfig, lowerLan

func (cc *createCmd) createDeployment() error {
log.Info("--- Deployment File Creation ---")
d := deployments.CreateDeploymentsFromEmbedFS(template.Deployments, cc.dest)
var deployType string
var deployConfig *config.DraftConfig
var deployTemplate *handlers.Template
var err error

if cc.createConfig.DeployType != "" {
deployType = strings.ToLower(cc.createConfig.DeployType)
deployConfig, err = d.GetConfig(deployType)
deployTemplate, err = handlers.GetTemplate(fmt.Sprintf("deployment-%s", deployType), "", cc.dest, cc.templateWriter)
if err != nil {
return err
}
if deployConfig == nil {
if deployTemplate == nil || deployTemplate.Config == nil {
return errors.New("invalid deployment type")
}
err = validateConfigInputsToPrompts(deployConfig, cc.createConfig.DeployVariables)
err = validateConfigInputsToPrompts(deployTemplate.Config, cc.createConfig.DeployVariables)
if err != nil {
return err
}
Expand All @@ -337,31 +336,34 @@ func (cc *createCmd) createDeployment() error {
deployType = cc.deployType
}

deployConfig, err = d.GetConfig(deployType)
deployTemplate, err = handlers.GetTemplate(fmt.Sprintf("deployment-%s", deployType), "", cc.dest, cc.templateWriter)
if err != nil {
return err
}

deployConfig.VariableMapToDraftConfig(flagVariablesMap)
if deployTemplate == nil || deployTemplate.Config == nil {
return errors.New("invalid deployment type")
}

deployTemplate.Config.VariableMapToDraftConfig(flagVariablesMap)

err = prompts.RunPromptsFromConfigWithSkips(deployConfig)
err = prompts.RunPromptsFromConfigWithSkips(deployTemplate.Config)
if err != nil {
return err
}
}

if cc.templateVariableRecorder != nil {
for _, variable := range deployConfig.Variables {
for _, variable := range deployTemplate.Config.Variables {
cc.templateVariableRecorder.Record(variable.Name, variable.Value)
}
}

log.Infof("--> Creating %s Kubernetes resources...\n", deployType)

return d.CopyDeploymentFiles(deployType, deployConfig, cc.templateWriter)
return deployTemplate.Generate()
}

func (cc *createCmd) createFiles(detectedLang *config.DraftConfig, lowerLang string) error {
func (cc *createCmd) createFiles(detectedLangTempalte *handlers.Template, lowerLang string) error {
// does no further checks without file detection

if cc.dockerfileOnly && cc.deploymentOnly {
Expand All @@ -370,7 +372,7 @@ func (cc *createCmd) createFiles(detectedLang *config.DraftConfig, lowerLang str

if cc.skipFileDetection {
if !cc.deploymentOnly {
err := cc.generateDockerfile(detectedLang, lowerLang)
err := cc.generateDockerfile(detectedLangTempalte, lowerLang)
if err != nil {
return err
}
Expand Down Expand Up @@ -410,7 +412,7 @@ func (cc *createCmd) createFiles(detectedLang *config.DraftConfig, lowerLang str
} else if hasDockerFile {
log.Info("--> Found Dockerfile in local directory, skipping Dockerfile creation...")
} else if !cc.deploymentOnly {
err := cc.generateDockerfile(detectedLang, lowerLang)
err := cc.generateDockerfile(detectedLangTempalte, lowerLang)
if err != nil {
return err
}
Expand Down
26 changes: 16 additions & 10 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import (
"github.com/stretchr/testify/assert"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/languages"
"github.com/Azure/draft/pkg/handlers"
"github.com/Azure/draft/pkg/linguist"
"github.com/Azure/draft/pkg/reporeader"
"github.com/Azure/draft/pkg/templatewriter/writers"
"github.com/Azure/draft/template"
)

func TestRun(t *testing.T) {
testCreateConfig := CreateConfig{LanguageVariables: []UserInputs{{Name: "PORT", Value: "8080"}}, DeployVariables: []UserInputs{{Name: "PORT", Value: "8080"}, {Name: "APPNAME", Value: "testingCreateCommand"}}}
flagVariablesMap = map[string]string{"PORT": "8080", "APPNAME": "testingCreateCommand", "VERSION": "1.18", "SERVICEPORT": "8080", "NAMESPACE": "testNamespace", "IMAGENAME": "testImage", "IMAGETAG": "latest"}
testCreateConfig := CreateConfig{LanguageVariables: []UserInputs{{Name: "PORT", Value: "8080"}}, DeployVariables: []UserInputs{{Name: "PORT", Value: "8080"}, {Name: "APPNAME", Value: "testingCreateCommand"}, {Name: "DOCKERFILENAME", Value: "Dockerfile"}}}
flagVariablesMap = map[string]string{"PORT": "8080", "APPNAME": "testingCreateCommand", "VERSION": "1.18", "SERVICEPORT": "8080", "NAMESPACE": "testNamespace", "IMAGENAME": "testImage", "IMAGETAG": "latest", "DOCKERFILENAME": "test.Dockerfile"}
mockCC := createCmd{
dest: "./..",
createConfig: &testCreateConfig,
Expand Down Expand Up @@ -199,7 +198,7 @@ func TestValidateConfigInputsToPromptsMissing(t *testing.T) {
assert.NotNil(t, err)
}

func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error) {
func (mcc *createCmd) mockDetectLanguage() (*handlers.Template, string, error) {
hasGo := false
hasGoMod := false
var langs []*linguist.Language
Expand Down Expand Up @@ -227,12 +226,13 @@ func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error)
}
}

mcc.supportedLangs = languages.CreateLanguagesFromEmbedFS(template.Dockerfiles, mcc.dest)

if mcc.createConfig.LanguageType != "" {
log.Debug("using configuration language")
lowerLang := strings.ToLower(mcc.createConfig.LanguageType)
langConfig := mcc.supportedLangs.GetConfig(lowerLang)
langConfig, err := handlers.GetTemplate(fmt.Sprintf("dockerfile-%s", lowerLang), "", mcc.dest, mcc.templateWriter)
if err != nil {
return nil, "", err
}
if langConfig == nil {
return nil, "", ErrNoLanguageDetected
}
Expand All @@ -245,13 +245,19 @@ func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error)
log.Infof("--> Draft detected %s (%f%%)\n", detectedLang.Language, detectedLang.Percent)
lowerLang := strings.ToLower(detectedLang.Language)

if mcc.supportedLangs.ContainsLanguage(lowerLang) {
if handlers.IsValidTemplate(fmt.Sprintf("dockerfile-%s", lowerLang)) {
if lowerLang == "go" && hasGo && hasGoMod {
log.Debug("detected go and go module")
lowerLang = "gomodule"
}

langConfig := mcc.supportedLangs.GetConfig(lowerLang)
langConfig, err := handlers.GetTemplate(fmt.Sprintf("dockerfile-%s", lowerLang), "", mcc.dest, mcc.templateWriter)
if err != nil {
return nil, "", err
}
if langConfig == nil {
return nil, "", ErrNoLanguageDetected
}
return langConfig, lowerLang, nil
}
log.Infof("--> Could not find a pack for %s. Trying to find the next likely language match...\n", detectedLang.Language)
Expand Down
Loading

0 comments on commit 135afd0

Please sign in to comment.