Skip to content

Commit

Permalink
📝 Formatted Text
Browse files Browse the repository at this point in the history
  • Loading branch information
ritek01 committed May 29, 2024
1 parent e2a86d8 commit 1e78ded
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
41 changes: 30 additions & 11 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "harness/utils"
"os"
"strings"
"time"
)

func DeployProject(c *cli.Context) error {
Expand All @@ -21,8 +22,8 @@ func DeployProject(c *cli.Context) error {
if err != nil {
return err
}

fmt.Printf("Loaded framework: %s\n", GetColoredText(framework, color.FgCyan))
ProjectprogressBar("Loading Project Info...")
fmt.Printf("\nLoaded framework: %s\n", GetColoredText(framework, color.FgCyan))
fmt.Printf("Loaded language: %s\n", GetColoredText(language, color.FgCyan))

hasDockerfile, err := checkDockerfile()
Expand All @@ -44,8 +45,9 @@ func DeployProject(c *cli.Context) error {
if err != nil {
return err
}
ProjectprogressBar("Creating Dockerfile...")
color.Set(color.FgGreen)
fmt.Println("🐋 Dockerfile created.")
fmt.Println("\n🐋 Dockerfile created.")
color.Unset()
hasDockerfile = true
}
Expand All @@ -56,7 +58,7 @@ func DeployProject(c *cli.Context) error {
return err
}

fmt.Print("Do you want to proceed deploying using Harness? (y/n): ")
fmt.Print("\nDo you want to proceed deploying using Harness? (y/n): ")
var proceed string
fmt.Scanln(&proceed)

Expand All @@ -65,7 +67,7 @@ func DeployProject(c *cli.Context) error {
return nil
}

orgName := promptUser("Org Name (default)", "default")
orgName := promptUser("\nOrg Name (default)", "default")
projectName := promptUser("Project Name (default_project)", "default_project")

_, err = CheckOrgExistsAndCreate(c, orgName)
Expand All @@ -85,16 +87,16 @@ func DeployProject(c *cli.Context) error {
return err
}

fmt.Print("Do you want to use the Harness Code Repository for code hosting? (y/n): ")
fmt.Print("\n\nDo you want to use the Harness Code Repository for code hosting? (y/n): ")
var useHarnessRepo string
fmt.Scanln(&useHarnessRepo)

if useHarnessRepo == "y" {
fmt.Printf("Already using Harness Code Repository for code hosting ? (y/n): ")
fmt.Printf("\nAlready using Harness Code Repository for code hosting ? (y/n): ")
var useHarnessCodeRepo string
fmt.Scanln(&useHarnessCodeRepo)
if useHarnessCodeRepo == "y" {
fmt.Println("Provide the repository Name: ")
fmt.Println("\nProvide the repository Name: ")
repoName := promptUser("Repository Name", "default_repo")
globals.RepoName = repoName
} else {
Expand All @@ -108,8 +110,12 @@ func DeployProject(c *cli.Context) error {
}
CreatePipeline()
RemoveTempFiles()
fmt.Println("Deployment process initialized.")

color.Set(color.FgGreen)
fmt.Println("Deployment Done.")
color.Unset()
color.Set(color.FgYellow)
fmt.Println("Thank you for using Harness CLI.")
color.Unset()
return nil
}

Expand Down Expand Up @@ -143,7 +149,7 @@ func promptUser(prompt, defaultValue string) string {
func loadProjectInfo() (string, string, error) {
file, err := os.Open(defaults.TEMPFILEPATH)
if err != nil {
return "", "", fmt.Errorf("failed to open temp file: %v", err)
return "", "", fmt.Errorf("please run 'harness init' first")
}
defer file.Close()

Expand Down Expand Up @@ -256,3 +262,16 @@ func saveDockerfileInfo(hasDockerfile bool) error {

return nil
}

func ProjectprogressBar(info string) {
barLength := 20
spinChars := []string{"|", "/", "-", "\\"}
for i := 0; i <= barLength; i++ {
spinner := spinChars[i%len(spinChars)]
progress := strings.Repeat("=", i) + strings.Repeat(" ", barLength-i)
color.Set(color.FgCyan)
fmt.Printf("\r[%s] %s %s", progress, spinner, info)
color.Unset()
time.Sleep(time.Millisecond * 100)
}
}
25 changes: 25 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/fatih/color"
"os"
"os/exec"
"strings"
"time"

Expand Down Expand Up @@ -53,6 +54,19 @@ func InitProject(c *cli.Context) error {
fmt.Println("\nHarness project initialized successfully!")
color.Unset()

fmt.Print("Do you want to Proceed with the deployment? (y/n) : ")
var deployment string
_, err = fmt.Scanln(&deployment)
if err != nil {
return err
}
if deployment == "y" {
err := deployPipeline()
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -115,3 +129,14 @@ func saveProjectInfo(framework, language string) error {

return nil
}

func deployPipeline() error {
cmd := exec.Command("harness", "deploy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("error running deploy command: %v", err)
}
return nil
}
4 changes: 2 additions & 2 deletions utils/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func CheckOrgExistsAndCreate(c *cli.Context, orgName string) (bool, error) {
org := getOrganisations(c)
orgId := findOrgIdByName(org, orgName)
if len(orgId) == 0 {
fmt.Printf("Organization '%s' does not exist. Do you want to create it? (y/n): ", orgName)
fmt.Printf("\nOrganization '%s' does not exist. Do you want to create it? (y/n): ", orgName)
var createOrg string
fmt.Scanln(&createOrg)

Expand All @@ -72,7 +72,7 @@ func CheckOrgExistsAndCreate(c *cli.Context, orgName string) (bool, error) {
return false, err
}
} else {
fmt.Printf("Organization '%s' already exists. Do you want to use it? (y/n): ", orgName)
fmt.Printf("\nOrganization '%s' already exists. Do you want to use it? (y/n): ", orgName)
var useOrg string
fmt.Scanln(&useOrg)

Expand Down
19 changes: 18 additions & 1 deletion utils/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"os"
_ "os"
"strings"
"time"
)

type Pipeline struct {
Expand Down Expand Up @@ -104,8 +106,10 @@ func CreatePipeline() {
fmt.Printf("Error creating pipeline in Harness: %v\n", err)
return
}
fmt.Println()
PipelineprogressBar("Creating pipeline in Harness...")
color.Set(color.FgGreen)
fmt.Println("Pipeline created successfully!")
fmt.Println("\nPipeline created successfully!")
color.Unset()
PipelineUrl := "https://app.harness.io/ng/account/" + globals.AccountId + "/home/orgs/" + globals.OrgId + "/projects/" + globals.ProjectId + "/pipelines/" + pipeline.Pipeline.Identifier + "/pipeline-studio/"
fmt.Printf("Pipeline Url : %s\n", GetColoredText(PipelineUrl, color.FgCyan))
Expand Down Expand Up @@ -148,3 +152,16 @@ func CreateHarnessPipeline(pipelineYAML []byte) error {
}
return nil
}

func PipelineprogressBar(info string) {
barLength := 20
spinChars := []string{"|", "/", "-", "\\"}
for i := 0; i <= barLength; i++ {
spinner := spinChars[i%len(spinChars)]
progress := strings.Repeat("=", i) + strings.Repeat(" ", barLength-i)
color.Set(color.FgCyan)
fmt.Printf("\r[%s] %s %s", progress, spinner, info)
color.Unset()
time.Sleep(time.Millisecond * 100)
}
}
4 changes: 2 additions & 2 deletions utils/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func CheckProjectExistsAndCreate(c *cli.Context, orgName string, projectName str
projectId := getProjectIdByName(projects, projectName)

if len(projectId) == 0 {
fmt.Println("Project not found. Creating project...")
fmt.Println("\nProject not found. Creating project...")
url := fmt.Sprintf("%s/projects?accountIdentifier=%s&orgIdentifier=%s", GetNGBaseURL(c), globals.AccountId, orgName)
resp, err := netclient.PostNew(url, globals.ApiKey, ProjectBody{
Project: ProjectDetails{
Expand All @@ -63,7 +63,7 @@ func CheckProjectExistsAndCreate(c *cli.Context, orgName string, projectName str
}
log.Info("Project created successfully")
} else {
fmt.Printf("Project '%s' already exists. Do you want to use it? (y/n): ", projectName)
fmt.Printf("\nProject '%s' already exists. Do you want to use it? (y/n): ", projectName)
var useProject string
fmt.Scanln(&useProject)

Expand Down

0 comments on commit 1e78ded

Please sign in to comment.