feat: update ownbrew and refactor cmds

This commit is contained in:
Kevin Franklin Kim 2024-10-26 11:32:08 +02:00
parent 8aade5afd3
commit 7cd3072cba
No known key found for this signature in database
17 changed files with 315 additions and 233 deletions

View File

@ -2,38 +2,57 @@ package cmd
import (
ownbrewconfig "github.com/foomo/ownbrew/pkg/config"
intcmd "github.com/foomo/posh/internal/cmd"
"github.com/foomo/posh/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
brewCmdFlagDry bool
)
// NewBrew represents the dependencies command
func NewBrew(root *cobra.Command) {
cmd := &cobra.Command{
Use: "brew",
Short: "Check and install required packages.",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
if err := config.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
var cfg ownbrewconfig.Config
if err := viper.UnmarshalKey("ownbrew", &cfg); err != nil {
return err
}
// brewCmd represents the dependencies command
var brewCmd = &cobra.Command{
Use: "brew",
Short: "Check and install required packages.",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := config.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var cfg ownbrewconfig.Config
if err := viper.UnmarshalKey("ownbrew", &cfg); err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
dry, err := cmd.Flags().GetBool("dry")
if err != nil {
return err
}
return plg.Brew(cmd.Context(), cfg, brewCmdFlagDry)
},
tags, err := cmd.Flags().GetStringSlice("tags")
if err != nil {
return err
}
return plg.Brew(cmd.Context(), cfg, tags, dry)
},
}
cmd.Flags().Bool("dry", false, "print out the taps that will be installed")
_ = viper.BindPFlag("dry", cmd.Flags().Lookup("dry"))
cmd.Flags().StringSlice("tags", nil, "filter by tags (e.g. ci,-test)")
_ = viper.BindPFlag("tags", cmd.Flags().Lookup("tags"))
root.AddCommand(cmd)
}

View File

@ -7,7 +7,6 @@ import (
"runtime/debug"
intenv "github.com/foomo/posh/internal/env"
intlog "github.com/foomo/posh/internal/log"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/plugin"
"github.com/pkg/errors"
@ -16,33 +15,31 @@ import (
func Init(provider plugin.Provider) {
pluginProvider = provider
cobra.OnInitialize(func() {
l = intlog.Init(flagLevel, flagNoColor)
l.Must(intenv.Init())
})
rootCmd.PersistentFlags().BoolVar(&flagNoColor, "no-color", false, "disabled colors (default is false)")
rootCmd.PersistentFlags().StringVar(&flagLevel, "level", "info", "set log level (default is warn)")
rootCmd.AddCommand(
configCmd,
versionCmd,
)
rootCmd = NewRoot()
NewConfig(rootCmd)
NewVersion(rootCmd)
if provider != nil {
rootCmd.AddCommand(
brewCmd,
execCmd,
promptCmd,
requireCmd,
)
brewCmd.Flags().BoolVar(&brewCmdFlagDry, "dry", false, "don't execute scripts")
NewBrew(rootCmd)
NewExecute(rootCmd)
NewPrompt(rootCmd)
NewRequire(rootCmd)
} else {
NewInit(rootCmd)
}
cobra.OnInitialize(func() {
if err := intenv.Init(); err != nil {
panic(err)
}
})
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
code := 0
l = log.NewFmt()
l := log.NewFmt()
// handle interrupt
osInterrupt := make(chan os.Signal, 1)

View File

@ -3,30 +3,37 @@ package cmd
import (
"fmt"
"github.com/foomo/ownbrew/pkg/util"
intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Print loaded configuration",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
out, err := yaml.Marshal(viper.AllSettings())
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
// NewConfig represents the config command
func NewConfig(root *cobra.Command) {
cmd := &cobra.Command{
Use: "config",
Short: "Print loaded configuration",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
out, err := yaml.Marshal(viper.AllSettings())
if err != nil {
return err
}
fmt.Println(util.Highlight(string(out), "yaml"))
return nil
},
}
root.AddCommand(cmd)
}

View File

@ -1,35 +1,42 @@
package cmd
import (
intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// execCmd represents the exec command
var execCmd = &cobra.Command{
Use: "execute",
Short: "Execute a single Project Oriented Shell command",
Args: cobra.ArbitraryArgs,
DisableFlagParsing: true,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("missing [cmd] argument")
}
// NewExecute represents the exec command
func NewExecute(root *cobra.Command) {
cmd := &cobra.Command{
Use: "execute",
Short: "Execute a single Project Oriented Shell command",
Args: cobra.ArbitraryArgs,
DisableFlagParsing: true,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
if len(args) == 0 {
return errors.New("missing [cmd] argument")
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
return plg.Execute(cmd.Context(), args)
},
return plg.Execute(cmd.Context(), args)
},
}
root.AddCommand(cmd)
}

View File

@ -5,61 +5,73 @@ import (
"github.com/foomo/posh/embed"
scaffold2 "github.com/foomo/posh/integration/scaffold"
intcmd "github.com/foomo/posh/internal/cmd"
"github.com/foomo/posh/internal/util/git"
"github.com/foomo/posh/pkg/env"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
initCmdFlagDry bool
initCmdFlagOverride bool
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a Project Oriented Shell",
Long: `Initialize (posh init) will create a new Project Oriented Shell with the appropriate structure.
// NewInit represents the init command
func NewInit(root *cobra.Command) {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize a Project Oriented Shell",
Long: `Initialize (posh init) will create a new Project Oriented Shell with the appropriate structure.
Posh init must be run inside of a go module (please run "go mod init <MODNAME> first)"`,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
data := map[string]interface{}{}
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
data := map[string]interface{}{}
// define module
if value, err := git.OriginURL(); err != nil {
l.Debug("failed to retrieve git origin url:", err.Error())
data["module"] = path.Base(env.ProjectRoot())
} else {
data["module"] = value
}
// define module
if value, err := git.OriginURL(); err != nil {
l.Debug("failed to retrieve git origin url:", err.Error())
data["module"] = path.Base(env.ProjectRoot())
} else {
data["module"] = value
}
fs, err := embed.Scaffold("init")
if err != nil {
return err
}
fs, err := embed.Scaffold("init")
if err != nil {
return err
}
sc, err := scaffold2.New(
l,
scaffold2.WithDry(initCmdFlagDry),
scaffold2.WithOverride(initCmdFlagOverride),
scaffold2.WithDirectories(scaffold2.Directory{
Source: fs,
Target: env.ProjectRoot(),
Data: data,
}),
)
if err != nil {
return err
}
dry, err := cmd.Flags().GetBool("dry")
if err != nil {
return err
}
return sc.Render(cmd.Context())
},
}
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().BoolVar(&initCmdFlagDry, "dry", false, "don't render files")
initCmd.Flags().BoolVar(&initCmdFlagOverride, "override", false, "override existing files")
override, err := cmd.Flags().GetBool("override")
if err != nil {
return err
}
sc, err := scaffold2.New(
l,
scaffold2.WithDry(dry),
scaffold2.WithOverride(override),
scaffold2.WithDirectories(scaffold2.Directory{
Source: fs,
Target: env.ProjectRoot(),
Data: data,
}),
)
if err != nil {
return err
}
return sc.Render(cmd.Context())
},
}
cmd.Flags().Bool("dry", false, "don't render files")
_ = viper.BindPFlag("dry", cmd.Flags().Lookup("dry"))
cmd.Flags().Bool("override", false, "override existing files")
_ = viper.BindPFlag("override", cmd.Flags().Lookup("override"))
root.AddCommand(cmd)
}

View File

@ -1,37 +1,42 @@
package cmd
import (
"context"
intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config"
"github.com/foomo/posh/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// promptCmd represents the prompt command
var promptCmd = &cobra.Command{
Use: "prompt",
Short: "Start the interactive Project Oriented Shell",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var cfg config.Prompt
if err := viper.UnmarshalKey("prompt", &cfg); err != nil {
return err
}
// NewPrompt represents the prompt command
func NewPrompt(root *cobra.Command) {
cmd := &cobra.Command{
Use: "prompt",
Short: "Start the interactive Project Oriented Shell",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
var cfg config.Prompt
if err := viper.UnmarshalKey("prompt", &cfg); err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
return plg.Prompt(context.TODO(), cfg)
},
return plg.Prompt(cmd.Context(), cfg)
},
}
root.AddCommand(cmd)
}

View File

@ -1,35 +1,42 @@
package cmd
import (
intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config"
"github.com/foomo/posh/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// requireCmd represents the require command
var requireCmd = &cobra.Command{
Use: "require",
Short: "Validate configured requirements",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var cfg config.Require
if err := viper.UnmarshalKey("require", &cfg); err != nil {
return err
}
// NewRequire represents the require command
func NewRequire(root *cobra.Command) {
cmd := &cobra.Command{
Use: "require",
Short: "Validate configured requirements",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
l := intcmd.NewLogger()
if err := intconfig.Load(l); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var cfg config.Require
l := intcmd.NewLogger()
if err := viper.UnmarshalKey("require", &cfg); err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
plg, err := pluginProvider(l)
if err != nil {
return err
}
return plg.Require(cmd.Context(), cfg)
},
return plg.Require(cmd.Context(), cfg)
},
}
root.AddCommand(cmd)
}

View File

@ -2,23 +2,31 @@ package cmd
import (
intconfig "github.com/foomo/posh/internal/config"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/plugin"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
l log.Logger
flagLevel string
flagNoColor bool
rootCmd *cobra.Command
pluginProvider plugin.Provider
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "posh",
Short: "Project Oriented Shell (posh)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return intconfig.Dotenv()
},
// NewRoot represents the base command when called without any subcommands
func NewRoot() *cobra.Command {
cmd := &cobra.Command{
Use: "posh",
Short: "Project Oriented Shell (posh)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return intconfig.Dotenv()
},
}
cmd.PersistentFlags().Bool("no-color", false, "disabled colors (default: false)")
_ = viper.BindPFlag("no-color", cmd.PersistentFlags().Lookup("no-color"))
cmd.PersistentFlags().String("level", "info", "set log level (default: info)")
_ = viper.BindPFlag("level", cmd.PersistentFlags().Lookup("level"))
return cmd
}

View File

@ -4,25 +4,31 @@ import (
"strconv"
"time"
intcmd "github.com/foomo/posh/internal/cmd"
intversion "github.com/foomo/posh/internal/version"
"github.com/foomo/posh/pkg/log"
"github.com/spf13/cobra"
)
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version",
Long: `If unsure which version of the CLI you are using, you can use this command to print the version of the CLI.`,
Run: func(cmd *cobra.Command, args []string) {
buildTime := intversion.BuildTimestamp
if value, err := strconv.ParseInt(intversion.BuildTimestamp, 10, 64); err == nil {
buildTime = time.Unix(value, 0).String()
}
if l.IsLevel(log.LevelDebug) {
l.Printf("Version: %s\nCommit: %s\nBuildTime: %s", intversion.Version, intversion.CommitHash, buildTime)
} else {
l.Printf("%s", intversion.Version)
}
},
// NewVersion represents the version command
func NewVersion(root *cobra.Command) {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version",
Long: `If unsure which version of the CLI you are using, you can use this command to print the version of the CLI.`,
Run: func(cmd *cobra.Command, args []string) {
l := intcmd.NewLogger()
buildTime := intversion.BuildTimestamp
if value, err := strconv.ParseInt(intversion.BuildTimestamp, 10, 64); err == nil {
buildTime = time.Unix(value, 0).String()
}
if l.IsLevel(log.LevelDebug) {
l.Printf("Version: %s\nCommit: %s\nBuildTime: %s", intversion.Version, intversion.CommitHash, buildTime)
} else {
l.Printf("%s", intversion.Version)
}
},
}
root.AddCommand(cmd)
}

View File

@ -6,6 +6,7 @@ import (
"log/slog"
"strings"
icommand "github.com/posh-test-demo/posh/internal/command"
ownbrewconfig "github.com/foomo/ownbrew/pkg/config"
"github.com/foomo/ownbrew/pkg/ownbrew"
"github.com/foomo/posh/pkg/command"
@ -17,7 +18,6 @@ import (
"github.com/foomo/posh/pkg/prompt/history"
"github.com/foomo/posh/pkg/readline"
"github.com/foomo/posh/pkg/require"
icommand "github.com/posh-test-demo/posh/internal/command"
)
type Plugin struct {
@ -53,7 +53,7 @@ func New(l log.Logger) (plugin.Plugin, error) { //nolint: ireturn
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (p *Plugin) Brew(ctx context.Context, cfg ownbrewconfig.Config, dry bool) error {
func (p *Plugin) Brew(ctx context.Context, cfg ownbrewconfig.Config, tags []string, dry bool) error {
brew, err := ownbrew.New(slog.New(p.l.SlogHandler()),
ownbrew.WithDry(dry),
ownbrew.WithBinDir(cfg.BinDir),
@ -65,7 +65,7 @@ func (p *Plugin) Brew(ctx context.Context, cfg ownbrewconfig.Config, dry bool) e
if err != nil {
return err
}
return brew.Install(ctx)
return brew.Install(ctx, tags...)
}
func (p *Plugin) Require(ctx context.Context, cfg config.Require) error {

6
go.mod
View File

@ -10,14 +10,14 @@ require (
github.com/c-bata/go-prompt v0.2.6
github.com/charlievieth/fastwalk v1.0.8
github.com/foomo/fender v1.0.1
github.com/foomo/ownbrew v0.1.2
github.com/foomo/ownbrew v0.2.0
github.com/go-git/go-git/v5 v5.12.0
github.com/gofrs/flock v0.12.1
github.com/joho/godotenv v1.5.1
github.com/kubescape/go-git-url v0.0.30
github.com/neilotoole/slogt v1.1.0
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.79
github.com/pterm/pterm v0.12.80-0.20241004171054-a6948562ecff
github.com/samber/lo v1.47.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
@ -60,7 +60,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect

8
go.sum
View File

@ -60,8 +60,8 @@ github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/foomo/fender v1.0.1 h1:sz+5IHWDgwAkYKau6NlgPHxa7v3kLyzScq8MrhKdybM=
github.com/foomo/fender v1.0.1/go.mod h1:Out1+dS5S8qJ4VqtxzCj/FDYi4Wtal2NK+8vhCUCrao=
github.com/foomo/ownbrew v0.1.2 h1:KYT1J1ztjARcPrsY8h2hhQ1A41atdEdp6YikYH5LQm0=
github.com/foomo/ownbrew v0.1.2/go.mod h1:lyUFvPABflzcI/cAmQU4vif1DjH8tZ6kogc+Ee44Sag=
github.com/foomo/ownbrew v0.2.0 h1:lmfEcMjQVx2c4Slb8a5pbwM8gTCk3EzmBIVjP1LVyB8=
github.com/foomo/ownbrew v0.2.0/go.mod h1:lyUFvPABflzcI/cAmQU4vif1DjH8tZ6kogc+Ee44Sag=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/franklinkim/go-prompt v0.2.7-0.20210427061716-a8f4995d7aa5 h1:kXNtle4AoQnngdm+gwt4ku6Llbzw3EFHgZYpL618JaI=
@ -146,6 +146,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-tty v0.0.3 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI=
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
@ -178,6 +180,8 @@ github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5b
github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
github.com/pterm/pterm v0.12.79 h1:lH3yrYMhdpeqX9y5Ep1u7DejyHy7NSQg9qrBjF9dFT4=
github.com/pterm/pterm v0.12.79/go.mod h1:1v/gzOF1N0FsjbgTHZ1wVycRkKiatFvJSJC4IGaQAAo=
github.com/pterm/pterm v0.12.80-0.20241004171054-a6948562ecff h1:50Ch3x1NIDpqRjlo4R00Xu7x9Xxdl0DS4iN61wyzpEI=
github.com/pterm/pterm v0.12.80-0.20241004171054-a6948562ecff/go.mod h1:1aCALwSx8GfmY30yYBJwdcUjGngDrhMtNlYzxicHlbM=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=

13
internal/cmd/init.go Normal file
View File

@ -0,0 +1,13 @@
package cmd
import (
"github.com/foomo/posh/pkg/log"
"github.com/spf13/viper"
)
func NewLogger() log.Logger {
return log.NewPTerm(
log.PTermWithDisableColor(viper.GetBool("no-color")),
log.PTermWithLevel(log.GetLevel(viper.GetString("level"))),
)
}

View File

@ -10,11 +10,14 @@ import (
func Init() error {
// setup env
if value := env.ProjectRoot(); value != "" {
// continue
} else if wd, err := os.Getwd(); err != nil {
return nil
}
if wd, err := os.Getwd(); err != nil {
return errors.Wrap(err, "failed to retrieve project root")
} else if err := env.SetProjectRoot(wd); err != nil {
return errors.Wrap(err, "failed to set project root env")
}
return nil
}

View File

@ -1,12 +0,0 @@
package log
import (
"github.com/foomo/posh/pkg/log"
)
func Init(level string, noColor bool) log.Logger {
return log.NewPTerm(
log.PTermWithDisableColor(noColor),
log.PTermWithLevel(log.GetLevel(level)),
)
}

View File

@ -56,6 +56,12 @@ func NewPTerm(opts ...PTermOption) *PTerm {
opt(inst)
}
}
pterm.Info = *pterm.Info.WithPrefix(pterm.Prefix{Text: "", Style: pterm.Info.Prefix.Style})
pterm.Debug = *pterm.Debug.WithPrefix(pterm.Prefix{Text: "⚒︎", Style: pterm.Debug.Prefix.Style})
pterm.Fatal = *pterm.Fatal.WithPrefix(pterm.Prefix{Text: "💀", Style: pterm.Fatal.Prefix.Style})
pterm.Error = *pterm.Error.WithPrefix(pterm.Prefix{Text: "⛌", Style: pterm.Error.Prefix.Style}) //nolint:reassign
pterm.Warning = *pterm.Info.WithPrefix(pterm.Prefix{Text: "⚠", Style: pterm.Warning.Prefix.Style})
pterm.Success = *pterm.Success.WithPrefix(pterm.Prefix{Text: "✓", Style: pterm.Success.Prefix.Style})
return inst
}

View File

@ -10,6 +10,6 @@ import (
type Plugin interface {
Prompt(ctx context.Context, cfg config.Prompt) error
Execute(ctx context.Context, args []string) error
Brew(ctx context.Context, cfg ownbrewconfig.Config, dry bool) error
Brew(ctx context.Context, cfg ownbrewconfig.Config, tags []string, dry bool) error
Require(ctx context.Context, cfg config.Require) error
}