Skip to content

Commit

Permalink
Merge pull request #47 from foomo/dotenv-with-root-config
Browse files Browse the repository at this point in the history
feat: add .poshrc and add root config
  • Loading branch information
franklinkim committed Dec 1, 2023
2 parents a60e70c + b681cd0 commit 22bbc72
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/brew.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var brewCmd = &cobra.Command{
Short: "Check and install required packages.",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"fmt"

intconfig "github.com/foomo/posh/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

// configCmd represents the config command
Expand All @@ -12,14 +15,18 @@ var configCmd = &cobra.Command{
Short: "Print loaded configuration",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
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 {
viper.Debug()
out, err := yaml.Marshal(viper.AllSettings())
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}
2 changes: 1 addition & 1 deletion cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var execCmd = &cobra.Command{
DisableFlagParsing: true,
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var promptCmd = &cobra.Command{
Short: "Start the interactive Project Oriented Shell",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var requireCmd = &cobra.Command{
Short: "Validate configured requirements",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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"
Expand All @@ -17,4 +18,7 @@ var (
var rootCmd = &cobra.Command{
Use: "posh",
Short: "Project Oriented Shell (posh)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return intconfig.Dotenv()
},
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/foomo/fender v1.0.1
github.com/go-git/go-git/v5 v5.8.1
github.com/gofrs/flock v0.8.1
github.com/imdario/mergo v0.3.13
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.67
github.com/samber/lo v1.38.1
Expand All @@ -20,6 +22,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/whilp/git-urls v1.0.0
golang.org/x/sync v0.3.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down Expand Up @@ -47,7 +50,6 @@ require (
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down Expand Up @@ -85,7 +87,6 @@ require (
golang.org/x/tools v0.6.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/c-bata/go-prompt v0.2.6 => github.com/franklinkim/go-prompt v0.2.7-0.20210427061716-a8f4995d7aa5
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
Expand Down
18 changes: 18 additions & 0 deletions internal/config/dotenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config

import (
"os"

"github.com/joho/godotenv"
"github.com/pkg/errors"
)

func Dotenv() error {
err := godotenv.Load(".poshrc")
if errors.Is(err, os.ErrNotExist) {
// continue
} else if err != nil {
return err
}
return nil
}
69 changes: 50 additions & 19 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,65 @@ import (

"github.com/foomo/posh/pkg/config"
"github.com/foomo/posh/pkg/log"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/spf13/viper"
)

func Load(l log.Logger) error {
var errNotFound viper.ConfigFileNotFoundError
var settings map[string]interface{}

viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName(".posh")
if err := viper.ReadInConfig(); err != nil {
return err
} else {
l.Debug("using config file:", viper.ConfigFileUsed())
if value := os.Getenv("POSH_ROOT_CONFIG_PATH"); value != "" {
c := viper.New()
c.AddConfigPath(value)
c.SetConfigType("yaml")
c.SetConfigName(".posh")
if err := c.ReadInConfig(); errors.As(err, &errNotFound) {
// continue
} else if err != nil {
return err
} else if err := mergo.Merge(&settings, c.AllSettings(), mergo.WithAppendSlice); err != nil {
return err
} else {
l.Debug("using root config file:", c.ConfigFileUsed())
}
}

override := viper.New()
override.AddConfigPath(".")
override.SetConfigType("yaml")
override.SetConfigName(".posh.override")
if err := override.ReadInConfig(); errors.As(err, &errNotFound) {
// continue
} else if err != nil {
return err
} else if err := viper.MergeConfigMap(override.AllSettings()); err != nil {
return err
} else {
l.Debug("using override config file:", override.ConfigFileUsed())
{ // load config
c := viper.New()
c.AddConfigPath(".")
c.SetConfigType("yaml")
c.SetConfigName(".posh")
if err := c.ReadInConfig(); errors.As(err, &errNotFound) {
// continue
} else if err != nil {
return err
} else if err := mergo.Merge(&settings, c.AllSettings(), mergo.WithAppendSlice); err != nil {
return err
} else {
l.Debug("using config file:", c.ConfigFileUsed())
}
}

{ // load override
c := viper.New()
c.AddConfigPath(".")
c.SetConfigType("yaml")
c.SetConfigName(".posh.override")
if err := c.ReadInConfig(); errors.As(err, &errNotFound) {
// continue
} else if err != nil {
return err
} else if err := mergo.Merge(&settings, c.AllSettings(), mergo.WithAppendSlice); err != nil {
return err
} else {
l.Debug("using override config file:", c.ConfigFileUsed())
}
}

if err := viper.MergeConfigMap(settings); err != nil {
return errors.Wrap(err, "failed to merge config map")
}

// validate version
Expand Down
5 changes: 4 additions & 1 deletion pkg/command/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"os"
"sort"
"strings"

"github.com/foomo/posh/pkg/command/tree"
Expand Down Expand Up @@ -104,7 +105,9 @@ func (c *Env) unset(ctx context.Context, r *readline.Readline) error {

func (c *Env) list(ctx context.Context, r *readline.Readline) error {
data := pterm.TableData{{"Name", "Value"}}
for _, s := range os.Environ() {
values := os.Environ()
sort.Strings(values)
for _, s := range values {
data = append(data, strings.SplitN(s, "=", 2))
}
return pterm.DefaultTable.WithHasHeader(true).WithData(data).Render()
Expand Down

0 comments on commit 22bbc72

Please sign in to comment.