Skip to content

Commit

Permalink
feat: 🎨 beautify cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
OnCloud125252 committed May 2, 2024
1 parent 9f0d9ee commit baa72cc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
6 changes: 2 additions & 4 deletions command/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func Add(ctx *cli.Context) error {
componentName := ctx.Args().Get(0)

if _, err := os.Stat(COMPONENT_DIRECTORY); errors.Is(err, os.ErrNotExist) {
color.Redln("Project isn't initialized yet. Please run 'cm init' first.")
return errors.New("1")
return errors.New("project isn't initialized yet. Please run 'cm init' first.")
}

_, err := githubapi.FindComponent(componentName)
Expand All @@ -29,8 +28,7 @@ func Add(ctx *cli.Context) error {
case "not a directory":
fallthrough
case "not a component":
color.Redln("Component " + componentName + " not found.")
return errors.New("1")
return errors.New("component " + componentName + " not found.")

default:
return err
Expand Down
20 changes: 12 additions & 8 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,45 @@ package command

import (
"errors"
"strings"

"github.com/gookit/color"
"github.com/urfave/cli/v2"

"github.com/gookit/config/v2"

"Component-Manager/module"
)

func Init(ctx *cli.Context) error {
PACKAGE_MANAGERS := config.Strings("package_managers")

_, err := module.CheckPm()
if err != nil {
switch err.Error() {
case "no package manager found":
color.Redln("No package manager found. Please install one of the following package managers: pnpm, bun, yarn, npm.")
return errors.New("no package manager found, please install one of the following package managers: " + strings.Join(PACKAGE_MANAGERS, ", ") + ".")
}
return errors.New("1")
return err
}

projectPath, err := module.GetPath(ctx.Args().Get(0))
if err != nil {
switch err.Error() {
case "path does not exist":
color.Redln("Specified path does not exist.")
return errors.New("specified path does not exist.")
case "path is not a directory":
color.Redln("Specified path is not a directory.")
return errors.New("specified path is not a directory.")
}
return errors.New("1")
return err
}

componentPath, err := module.CreateComponentDirectory(projectPath)
if err != nil {
color.Redln(err.Error())
return errors.New("1")
return err
}

color.Magentaf("Component directory:\t")
color.Magentaf("Component directory: ")
color.Cyanln(componentPath)

return nil
Expand Down
4 changes: 2 additions & 2 deletions module/CheckPm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

func CheckPm() (string, error) {
checkList := config.Strings("package_managers")
PACKAGE_MANAGERS := config.Strings("package_managers")

for _, bin := range checkList {
for _, bin := range PACKAGE_MANAGERS {
if _, err := exec.LookPath(bin); err == nil {
return bin, nil
}
Expand Down
2 changes: 1 addition & 1 deletion module/GithubApi/DownloadContents.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func DownloadFile(fileContent *github.RepositoryContent, componentPath string) e
return errors.New("failed to write file: " + filePath)
}

color.Magentap("Downloaded:\t")
color.Greenp(" + ")
color.Cyanln(filepath.Join(componentName, fileContent.GetName()))

return nil
Expand Down
9 changes: 6 additions & 3 deletions module/GithubApi/GetComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/gookit/color"
"github.com/gookit/config/v2"
Expand All @@ -15,16 +16,17 @@ import (
func GetComponent(componentName string) (string, error) {
COMPONENT_DIRECTORY := config.String("component_directory")
SOURCE_COMPONENT_DIRECTORY := config.String("source.component_directory")
PACKAGE_MANAGERS := config.Strings("package_managers")

packageManager, err := module.CheckPm()
if err != nil {
switch err.Error() {
case "no package manager found":
color.Redln("No package manager found. Please install one of the following package managers: pnpm, bun, yarn, npm.")
return "", errors.New("no package manager found, please install one of the following package managers: " + strings.Join(PACKAGE_MANAGERS, ", ") + ".")
}
return "", errors.New("1")
return "", err
}
color.Magentaf("Using:\t\t")
color.Magentaf("Using package manager: ")
color.Cyanln(packageManager)

componentPath := filepath.Join(SOURCE_COMPONENT_DIRECTORY, componentName)
Expand All @@ -34,6 +36,7 @@ func GetComponent(componentName string) (string, error) {
return "", err
}

color.Magentaln("Downloaded:")
err = DownloadComponent(githubClient, context, componentPath)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion module/LoadConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func LoadConfig(configPath string, officialConfigBytes []byte) (string, error) {
return "", errors.New("no source component directory found")
}

color.Magentap("Using custom config:\t")
color.Magentap("Using custom config: ")
color.Cyanln(configPath)

return configPath, nil
Expand Down

0 comments on commit baa72cc

Please sign in to comment.