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 ( import (
ownbrewconfig "github.com/foomo/ownbrew/pkg/config" ownbrewconfig "github.com/foomo/ownbrew/pkg/config"
intcmd "github.com/foomo/posh/internal/cmd"
"github.com/foomo/posh/internal/config" "github.com/foomo/posh/internal/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var ( // NewBrew represents the dependencies command
brewCmdFlagDry bool 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 plg, err := pluginProvider(l)
var brewCmd = &cobra.Command{ if err != nil {
Use: "brew", return err
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) dry, err := cmd.Flags().GetBool("dry")
if err != nil { if err != nil {
return err 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" "runtime/debug"
intenv "github.com/foomo/posh/internal/env" 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/log"
"github.com/foomo/posh/pkg/plugin" "github.com/foomo/posh/pkg/plugin"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -16,33 +15,31 @@ import (
func Init(provider plugin.Provider) { func Init(provider plugin.Provider) {
pluginProvider = provider pluginProvider = provider
cobra.OnInitialize(func() { rootCmd = NewRoot()
l = intlog.Init(flagLevel, flagNoColor) NewConfig(rootCmd)
l.Must(intenv.Init()) NewVersion(rootCmd)
})
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,
)
if provider != nil { if provider != nil {
rootCmd.AddCommand( NewBrew(rootCmd)
brewCmd, NewExecute(rootCmd)
execCmd, NewPrompt(rootCmd)
promptCmd, NewRequire(rootCmd)
requireCmd, } else {
) NewInit(rootCmd)
brewCmd.Flags().BoolVar(&brewCmdFlagDry, "dry", false, "don't execute scripts")
} }
cobra.OnInitialize(func() {
if err := intenv.Init(); err != nil {
panic(err)
}
})
} }
// Execute adds all child commands to the root command and sets flags appropriately. // 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. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
code := 0 code := 0
l = log.NewFmt() l := log.NewFmt()
// handle interrupt // handle interrupt
osInterrupt := make(chan os.Signal, 1) osInterrupt := make(chan os.Signal, 1)

View File

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

View File

@ -1,35 +1,42 @@
package cmd package cmd
import ( import (
intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config" intconfig "github.com/foomo/posh/internal/config"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// execCmd represents the exec command // NewExecute represents the exec command
var execCmd = &cobra.Command{ func NewExecute(root *cobra.Command) {
Use: "execute", cmd := &cobra.Command{
Short: "Execute a single Project Oriented Shell command", Use: "execute",
Args: cobra.ArbitraryArgs, Short: "Execute a single Project Oriented Shell command",
DisableFlagParsing: true, Args: cobra.ArbitraryArgs,
SilenceUsage: true, DisableFlagParsing: true,
SilenceErrors: true, SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error { SilenceErrors: true,
if err := intconfig.Load(l); err != nil { PreRunE: func(cmd *cobra.Command, args []string) error {
return err l := intcmd.NewLogger()
} if err := intconfig.Load(l); err != nil {
return nil return err
}, }
RunE: func(cmd *cobra.Command, args []string) error { return nil
if len(args) == 0 { },
return errors.New("missing [cmd] argument") 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) plg, err := pluginProvider(l)
if err != nil { if err != nil {
return err 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" "github.com/foomo/posh/embed"
scaffold2 "github.com/foomo/posh/integration/scaffold" 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/internal/util/git"
"github.com/foomo/posh/pkg/env" "github.com/foomo/posh/pkg/env"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var ( // NewInit represents the init command
initCmdFlagDry bool func NewInit(root *cobra.Command) {
initCmdFlagOverride bool cmd := &cobra.Command{
) Use: "init",
Short: "Initialize a Project Oriented Shell",
// initCmd represents the init command Long: `Initialize (posh init) will create a new Project Oriented Shell with the appropriate structure.
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.
Posh init must be run inside of a go module (please run "go mod init <MODNAME> first)"`, Posh init must be run inside of a go module (please run "go mod init <MODNAME> first)"`,
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
data := map[string]interface{}{} l := intcmd.NewLogger()
data := map[string]interface{}{}
// define module // define module
if value, err := git.OriginURL(); err != nil { if value, err := git.OriginURL(); err != nil {
l.Debug("failed to retrieve git origin url:", err.Error()) l.Debug("failed to retrieve git origin url:", err.Error())
data["module"] = path.Base(env.ProjectRoot()) data["module"] = path.Base(env.ProjectRoot())
} else { } else {
data["module"] = value data["module"] = value
} }
fs, err := embed.Scaffold("init") fs, err := embed.Scaffold("init")
if err != nil { if err != nil {
return err return err
} }
sc, err := scaffold2.New( dry, err := cmd.Flags().GetBool("dry")
l, if err != nil {
scaffold2.WithDry(initCmdFlagDry), return err
scaffold2.WithOverride(initCmdFlagOverride), }
scaffold2.WithDirectories(scaffold2.Directory{
Source: fs,
Target: env.ProjectRoot(),
Data: data,
}),
)
if err != nil {
return err
}
return sc.Render(cmd.Context()) override, err := cmd.Flags().GetBool("override")
}, if err != nil {
} return err
}
func init() {
rootCmd.AddCommand(initCmd) sc, err := scaffold2.New(
initCmd.Flags().BoolVar(&initCmdFlagDry, "dry", false, "don't render files") l,
initCmd.Flags().BoolVar(&initCmdFlagOverride, "override", false, "override existing files") 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 package cmd
import ( import (
"context" intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config" intconfig "github.com/foomo/posh/internal/config"
"github.com/foomo/posh/pkg/config" "github.com/foomo/posh/pkg/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// promptCmd represents the prompt command // NewPrompt represents the prompt command
var promptCmd = &cobra.Command{ func NewPrompt(root *cobra.Command) {
Use: "prompt", cmd := &cobra.Command{
Short: "Start the interactive Project Oriented Shell", Use: "prompt",
SilenceUsage: true, Short: "Start the interactive Project Oriented Shell",
SilenceErrors: true, SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error { SilenceErrors: true,
if err := intconfig.Load(l); err != nil { PreRunE: func(cmd *cobra.Command, args []string) error {
return err l := intcmd.NewLogger()
} if err := intconfig.Load(l); err != nil {
return nil return err
}, }
RunE: func(cmd *cobra.Command, args []string) error { return nil
var cfg config.Prompt },
if err := viper.UnmarshalKey("prompt", &cfg); err != nil { RunE: func(cmd *cobra.Command, args []string) error {
return err l := intcmd.NewLogger()
} var cfg config.Prompt
if err := viper.UnmarshalKey("prompt", &cfg); err != nil {
return err
}
plg, err := pluginProvider(l) plg, err := pluginProvider(l)
if err != nil { if err != nil {
return err 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 package cmd
import ( import (
intcmd "github.com/foomo/posh/internal/cmd"
intconfig "github.com/foomo/posh/internal/config" intconfig "github.com/foomo/posh/internal/config"
"github.com/foomo/posh/pkg/config" "github.com/foomo/posh/pkg/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// requireCmd represents the require command // NewRequire represents the require command
var requireCmd = &cobra.Command{ func NewRequire(root *cobra.Command) {
Use: "require", cmd := &cobra.Command{
Short: "Validate configured requirements", Use: "require",
SilenceUsage: true, Short: "Validate configured requirements",
SilenceErrors: true, SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error { SilenceErrors: true,
if err := intconfig.Load(l); err != nil { PreRunE: func(cmd *cobra.Command, args []string) error {
return err l := intcmd.NewLogger()
} if err := intconfig.Load(l); err != nil {
return nil return err
}, }
RunE: func(cmd *cobra.Command, args []string) error { return nil
var cfg config.Require },
if err := viper.UnmarshalKey("require", &cfg); err != nil { RunE: func(cmd *cobra.Command, args []string) error {
return err var cfg config.Require
} l := intcmd.NewLogger()
if err := viper.UnmarshalKey("require", &cfg); err != nil {
return err
}
plg, err := pluginProvider(l) plg, err := pluginProvider(l)
if err != nil { if err != nil {
return err 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 ( import (
intconfig "github.com/foomo/posh/internal/config" intconfig "github.com/foomo/posh/internal/config"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/plugin" "github.com/foomo/posh/pkg/plugin"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var ( var (
l log.Logger rootCmd *cobra.Command
flagLevel string
flagNoColor bool
pluginProvider plugin.Provider pluginProvider plugin.Provider
) )
// rootCmd represents the base command when called without any subcommands // NewRoot represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ func NewRoot() *cobra.Command {
Use: "posh", cmd := &cobra.Command{
Short: "Project Oriented Shell (posh)", Use: "posh",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { Short: "Project Oriented Shell (posh)",
return intconfig.Dotenv() 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" "strconv"
"time" "time"
intcmd "github.com/foomo/posh/internal/cmd"
intversion "github.com/foomo/posh/internal/version" intversion "github.com/foomo/posh/internal/version"
"github.com/foomo/posh/pkg/log" "github.com/foomo/posh/pkg/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// versionCmd represents the version command // NewVersion represents the version command
var versionCmd = &cobra.Command{ func NewVersion(root *cobra.Command) {
Use: "version", cmd := &cobra.Command{
Short: "Print the version", Use: "version",
Long: `If unsure which version of the CLI you are using, you can use this command to print the version of the CLI.`, Short: "Print the version",
Run: func(cmd *cobra.Command, args []string) { Long: `If unsure which version of the CLI you are using, you can use this command to print the version of the CLI.`,
buildTime := intversion.BuildTimestamp Run: func(cmd *cobra.Command, args []string) {
if value, err := strconv.ParseInt(intversion.BuildTimestamp, 10, 64); err == nil { l := intcmd.NewLogger()
buildTime = time.Unix(value, 0).String() buildTime := intversion.BuildTimestamp
} if value, err := strconv.ParseInt(intversion.BuildTimestamp, 10, 64); err == nil {
if l.IsLevel(log.LevelDebug) { buildTime = time.Unix(value, 0).String()
l.Printf("Version: %s\nCommit: %s\nBuildTime: %s", intversion.Version, intversion.CommitHash, buildTime) }
} else { if l.IsLevel(log.LevelDebug) {
l.Printf("%s", intversion.Version) 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" "log/slog"
"strings" "strings"
icommand "github.com/posh-test-demo/posh/internal/command"
ownbrewconfig "github.com/foomo/ownbrew/pkg/config" ownbrewconfig "github.com/foomo/ownbrew/pkg/config"
"github.com/foomo/ownbrew/pkg/ownbrew" "github.com/foomo/ownbrew/pkg/ownbrew"
"github.com/foomo/posh/pkg/command" "github.com/foomo/posh/pkg/command"
@ -17,7 +18,6 @@ import (
"github.com/foomo/posh/pkg/prompt/history" "github.com/foomo/posh/pkg/prompt/history"
"github.com/foomo/posh/pkg/readline" "github.com/foomo/posh/pkg/readline"
"github.com/foomo/posh/pkg/require" "github.com/foomo/posh/pkg/require"
icommand "github.com/posh-test-demo/posh/internal/command"
) )
type Plugin struct { type Plugin struct {
@ -53,7 +53,7 @@ func New(l log.Logger) (plugin.Plugin, error) { //nolint: ireturn
// ~ Public methods // ~ 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()), brew, err := ownbrew.New(slog.New(p.l.SlogHandler()),
ownbrew.WithDry(dry), ownbrew.WithDry(dry),
ownbrew.WithBinDir(cfg.BinDir), ownbrew.WithBinDir(cfg.BinDir),
@ -65,7 +65,7 @@ func (p *Plugin) Brew(ctx context.Context, cfg ownbrewconfig.Config, dry bool) e
if err != nil { if err != nil {
return err return err
} }
return brew.Install(ctx) return brew.Install(ctx, tags...)
} }
func (p *Plugin) Require(ctx context.Context, cfg config.Require) error { 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/c-bata/go-prompt v0.2.6
github.com/charlievieth/fastwalk v1.0.8 github.com/charlievieth/fastwalk v1.0.8
github.com/foomo/fender v1.0.1 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/go-git/go-git/v5 v5.12.0
github.com/gofrs/flock v0.12.1 github.com/gofrs/flock v0.12.1
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/kubescape/go-git-url v0.0.30 github.com/kubescape/go-git-url v0.0.30
github.com/neilotoole/slogt v1.1.0 github.com/neilotoole/slogt v1.1.0
github.com/pkg/errors v0.9.1 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/samber/lo v1.47.0
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
@ -60,7 +60,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // 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/mattn/go-tty v0.0.3 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.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/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 h1:sz+5IHWDgwAkYKau6NlgPHxa7v3kLyzScq8MrhKdybM=
github.com/foomo/fender v1.0.1/go.mod h1:Out1+dS5S8qJ4VqtxzCj/FDYi4Wtal2NK+8vhCUCrao= 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.2.0 h1:lmfEcMjQVx2c4Slb8a5pbwM8gTCk3EzmBIVjP1LVyB8=
github.com/foomo/ownbrew v0.1.2/go.mod h1:lyUFvPABflzcI/cAmQU4vif1DjH8tZ6kogc+Ee44Sag= 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 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= 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= 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.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= 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.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 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI=
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0= github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= 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.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
github.com/pterm/pterm v0.12.79 h1:lH3yrYMhdpeqX9y5Ep1u7DejyHy7NSQg9qrBjF9dFT4= 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.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.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 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 { func Init() error {
// setup env // setup env
if value := env.ProjectRoot(); value != "" { if value := env.ProjectRoot(); value != "" {
// continue return nil
} else if wd, err := os.Getwd(); err != nil { }
if wd, err := os.Getwd(); err != nil {
return errors.Wrap(err, "failed to retrieve project root") return errors.Wrap(err, "failed to retrieve project root")
} else if err := env.SetProjectRoot(wd); err != nil { } else if err := env.SetProjectRoot(wd); err != nil {
return errors.Wrap(err, "failed to set project root env") return errors.Wrap(err, "failed to set project root env")
} }
return nil 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) 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 return inst
} }

View File

@ -10,6 +10,6 @@ import (
type Plugin interface { type Plugin interface {
Prompt(ctx context.Context, cfg config.Prompt) error Prompt(ctx context.Context, cfg config.Prompt) error
Execute(ctx context.Context, args []string) 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 Require(ctx context.Context, cfg config.Require) error
} }