diff --git a/pkg/commands/commands.go b/internal/commands/commands.go similarity index 100% rename from pkg/commands/commands.go rename to internal/commands/commands.go diff --git a/pkg/commands/init.go b/internal/commands/init.go similarity index 61% rename from pkg/commands/init.go rename to internal/commands/init.go index 302c1a4..7e007ca 100644 --- a/pkg/commands/init.go +++ b/internal/commands/init.go @@ -4,8 +4,7 @@ import ( "fmt" "path" - "github.com/Glow-Project/ppm/pkg/utility" - "github.com/fatih/color" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) @@ -19,10 +18,10 @@ func initialize(ctx *cli.Context) error { return fmt.Errorf("error creating ppm.json config-file: %w", err) } - fmt.Println(color.GreenString("new ppm.json config-file generated")) + utility.ColorPrintln("{GRN}new ppm.json config-file generated") if ok, _ := utility.DoesPathExist(path.Join(paths.Root, ".git")); ok { - fmt.Println(color.YellowString("when using ppm it is recommended to add the addons directory to your .gitignore file")) + utility.ColorPrintln("{YLW}when using ppm it is recommended to add the addons direcotry to your .gitignore file") } return nil diff --git a/pkg/commands/install.go b/internal/commands/install.go similarity index 67% rename from pkg/commands/install.go rename to internal/commands/install.go index b3cdbf3..13bb7b5 100644 --- a/pkg/commands/install.go +++ b/internal/commands/install.go @@ -1,11 +1,8 @@ package commands import ( - "fmt" - - "github.com/Glow-Project/ppm/pkg/fetch" - "github.com/Glow-Project/ppm/pkg/utility" - "github.com/fatih/color" + "github.com/Glow-Project/ppm/internal/fetch" + "github.com/Glow-Project/ppm/internal/utility" "github.com/go-git/go-git/v5" "github.com/urfave/cli/v2" ) @@ -43,29 +40,29 @@ func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) erro func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency utility.Dependency, isSubDependency bool) error { if !isSubDependency { - fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier))) + utility.ColorPrintln("\rinstalling {YLW}%s", dependency.Identifier) } else { - fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier))) + utility.ColorPrintln("\t -> installing {YLW}%s", dependency.Identifier) } loadAnim := utility.StartLoading() err := fetch.InstallDependency(dependency, paths) loadAnim.Stop() - switch err.(type) { + switch err := err.(type) { case nil: break case *fetch.InvalidVersionError: dependency.Version = nil - versionError(dependency.Identifier, err.(*fetch.InvalidVersionError).Version) + versionError(dependency.Identifier, err.Version) case *fetch.CloneError: - gitErr := err.(*fetch.CloneError).GitError + gitErr := err.GitError if gitErr == git.ErrRepositoryAlreadyExists { alreadyInstalled(dependency.Identifier) return nil } else { installError(dependency.Identifier) - return err.(*fetch.CloneError).GitError + return err.GitError } default: return err @@ -103,13 +100,13 @@ func installDependency(config *utility.PpmConfig, paths utility.Paths, dependenc } func alreadyInstalled(dependency string) { - fmt.Println(color.GreenString("\rthe plugin"), color.YellowString(dependency), color.GreenString("is already installed")) + utility.ColorPrintln("\r{GRN}the plugin {YLW}%s {GRN}is already installed", dependency) } func installError(dependency string) { - fmt.Printf(color.RedString("\rsome issues occured while trying to install %s, %s"), color.YellowString(dependency), color.RedString("are you sure you spelled it right?\n")) + utility.ColorPrintln("\r{RED}some issues occured while trying to install {YLW}%s {RED}are you sure you spelled it right?", dependency) } func versionError(dependency string, version string) { - fmt.Printf(color.RedString("\rthe version \"%s\" %s %s %s"), color.YellowString(version), color.RedString("for the dependency"), color.YellowString(dependency), color.RedString("was not found. The default version was installed\n")) + utility.ColorPrintln("\r{RED}the version {YLW}%s {RED} for the dependency {YLW}%s {RED}was not found. The default version was installed", version, dependency) } diff --git a/pkg/commands/reinstall.go b/internal/commands/reinstall.go similarity index 87% rename from pkg/commands/reinstall.go rename to internal/commands/reinstall.go index 2e91860..c583913 100644 --- a/pkg/commands/reinstall.go +++ b/internal/commands/reinstall.go @@ -1,7 +1,7 @@ package commands import ( - "github.com/Glow-Project/ppm/pkg/utility" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) diff --git a/pkg/commands/show.go b/internal/commands/show.go similarity index 81% rename from pkg/commands/show.go rename to internal/commands/show.go index 73bfb99..6ac66dc 100644 --- a/pkg/commands/show.go +++ b/internal/commands/show.go @@ -1,7 +1,7 @@ package commands import ( - "github.com/Glow-Project/ppm/pkg/utility" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) diff --git a/pkg/commands/tidy.go b/internal/commands/tidy.go similarity index 95% rename from pkg/commands/tidy.go rename to internal/commands/tidy.go index 6e98914..e8bf41c 100644 --- a/pkg/commands/tidy.go +++ b/internal/commands/tidy.go @@ -4,7 +4,7 @@ import ( "encoding/json" "os" - "github.com/Glow-Project/ppm/pkg/utility" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) diff --git a/pkg/commands/uninstall.go b/internal/commands/uninstall.go similarity index 76% rename from pkg/commands/uninstall.go rename to internal/commands/uninstall.go index f746166..9dde7c9 100644 --- a/pkg/commands/uninstall.go +++ b/internal/commands/uninstall.go @@ -1,12 +1,10 @@ package commands import ( - "fmt" "os" "path" - "github.com/Glow-Project/ppm/pkg/utility" - "github.com/fatih/color" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) @@ -27,9 +25,9 @@ func uninstall(ctx *cli.Context) error { if config.HasDependency(dep) && !config.HasSubDependency(dep) { uninstallDependency(&config, paths, dep, false) } else if config.HasSubDependency(dep) { - fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is a sub dependency and can only be uninstalled by uninstalling its parent")) + utility.ColorPrintln("{RED}the plugin {YLW}%s {RED}is a sub-dependency and can only be uninstalled by uninstalling its parent", dep.Identifier) } else { - fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is not installed")) + utility.ColorPrintln("{RED}the plugin %s is not installed", dep.Identifier) } } @@ -54,9 +52,9 @@ func uninstallAllDependencies(config *utility.PpmConfig, paths utility.Paths, ha func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency utility.Dependency, isSubDependency bool) error { if !isSubDependency { - fmt.Println("\runinstalling", color.YellowString(dependency.Identifier)) + utility.ColorPrintln("\runinstalling {YLW}%s", dependency.Identifier) } else { - fmt.Println("\t -> uninstalling", color.YellowString(dependency.Identifier)) + utility.ColorPrintln("\t -> uninstalling {YLW}%s", dependency.Identifier) } loadAnim := utility.StartLoading() diff --git a/pkg/commands/update.go b/internal/commands/update.go similarity index 94% rename from pkg/commands/update.go rename to internal/commands/update.go index f8cba96..057ee2a 100644 --- a/pkg/commands/update.go +++ b/internal/commands/update.go @@ -3,7 +3,7 @@ package commands import ( "path/filepath" - "github.com/Glow-Project/ppm/pkg/utility" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) diff --git a/pkg/fetch/errors.go b/internal/fetch/errors.go similarity index 100% rename from pkg/fetch/errors.go rename to internal/fetch/errors.go diff --git a/pkg/fetch/fetch.go b/internal/fetch/fetch.go similarity index 98% rename from pkg/fetch/fetch.go rename to internal/fetch/fetch.go index 7fe396f..3556807 100644 --- a/pkg/fetch/fetch.go +++ b/internal/fetch/fetch.go @@ -10,7 +10,7 @@ import ( "regexp" "strings" - "github.com/Glow-Project/ppm/pkg/utility" + "github.com/Glow-Project/ppm/internal/utility" "github.com/go-git/go-git/v5" ) diff --git a/pkg/fetch/fetch_test.go b/internal/fetch/fetch_test.go similarity index 100% rename from pkg/fetch/fetch_test.go rename to internal/fetch/fetch_test.go diff --git a/pkg/fetch/requester.go b/internal/fetch/requester.go similarity index 100% rename from pkg/fetch/requester.go rename to internal/fetch/requester.go diff --git a/pkg/utility/dependency.go b/internal/utility/dependency.go similarity index 100% rename from pkg/utility/dependency.go rename to internal/utility/dependency.go diff --git a/pkg/utility/dependency_test.go b/internal/utility/dependency_test.go similarity index 100% rename from pkg/utility/dependency_test.go rename to internal/utility/dependency_test.go diff --git a/pkg/utility/generics.go b/internal/utility/generics.go similarity index 100% rename from pkg/utility/generics.go rename to internal/utility/generics.go diff --git a/pkg/utility/git.go b/internal/utility/git.go similarity index 100% rename from pkg/utility/git.go rename to internal/utility/git.go diff --git a/pkg/utility/loading.go b/internal/utility/loading.go similarity index 100% rename from pkg/utility/loading.go rename to internal/utility/loading.go diff --git a/internal/utility/output.go b/internal/utility/output.go new file mode 100644 index 0000000..fdc7ed4 --- /dev/null +++ b/internal/utility/output.go @@ -0,0 +1,43 @@ +package utility + +import ( + "fmt" + "strings" +) + +var UseColor = true + +func ColorPrint(str string, args ...any) { + fmt.Printf(colorFmt(str), args...) +} + +func ColorPrintln(str string, args ...any) { + fmt.Printf(colorFmt(str)+"\n", args...) +} + +func colorFmt(str string) string { + colors := map[string]string{ + "{RED}": "\033[31m", + "{YLW}": "\033[33m", + "{BLU}": "\033[34m", + "{PUR}": "\033[35m", + "{GRN}": "\033[32m", + "{RES}": "\033[0m", + } + + if !UseColor { + for k := range colors { + colors[k] = "" + } + } + + return replaceAll(str+"{RES}", colors) +} + +func replaceAll(str string, substrings map[string]string) string { + for k, v := range substrings { + str = strings.ReplaceAll(str, k, v) + } + + return str +} diff --git a/pkg/utility/paths.go b/internal/utility/paths.go similarity index 100% rename from pkg/utility/paths.go rename to internal/utility/paths.go diff --git a/pkg/utility/paths_test.go b/internal/utility/paths_test.go similarity index 100% rename from pkg/utility/paths_test.go rename to internal/utility/paths_test.go diff --git a/pkg/utility/ppmconfig.go b/internal/utility/ppmconfig.go similarity index 100% rename from pkg/utility/ppmconfig.go rename to internal/utility/ppmconfig.go diff --git a/pkg/utility/regex.go b/internal/utility/regex.go similarity index 100% rename from pkg/utility/regex.go rename to internal/utility/regex.go diff --git a/pkg/utility/utility.go b/internal/utility/utility.go similarity index 95% rename from pkg/utility/utility.go rename to internal/utility/utility.go index 46d3159..28f4673 100644 --- a/pkg/utility/utility.go +++ b/internal/utility/utility.go @@ -2,11 +2,8 @@ package utility import ( "errors" - "fmt" "os" "strings" - - "github.com/fatih/color" ) // check wether an absolute path exists @@ -47,7 +44,7 @@ func GetVersionOrNot(dependency string) (string, string) { } func PrintDone() { - fmt.Print(color.GreenString("\rdone\n")) + ColorPrintln("{GRN}done") } func GetPluginIdentifier(name string) string { diff --git a/pkg/utility/utility_test.go b/internal/utility/utility_test.go similarity index 100% rename from pkg/utility/utility_test.go rename to internal/utility/utility_test.go diff --git a/ppm.go b/ppm.go index f6d68e6..347f0c1 100644 --- a/ppm.go +++ b/ppm.go @@ -4,7 +4,8 @@ import ( "log" "os" - "github.com/Glow-Project/ppm/pkg/commands" + "github.com/Glow-Project/ppm/internal/commands" + "github.com/Glow-Project/ppm/internal/utility" "github.com/urfave/cli/v2" ) @@ -18,6 +19,19 @@ func main() { EnableBashCompletion: true, Commands: commands.Commands(), HideVersion: false, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "no-color", + Required: false, + Action: func(ctx *cli.Context, b bool) error { + if b { + utility.UseColor = false + } + + return nil + }, + }, + }, } if err := app.Run(os.Args); err != nil { log.Fatal(err)