Skip to content

Commit

Permalink
Merge pull request #1068 from neo773/env-command
Browse files Browse the repository at this point in the history
feat: cli config command
  • Loading branch information
tolgaOzen committed Feb 26, 2024
2 parents de0d427 + 7b9d92b commit 3b8a6ba
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/permify/permify.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func main() {
version := cmd.NewVersionCommand()
root.AddCommand(version)

config := cmd.NewConfigCommand()
root.AddCommand(config)

if err := root.Execute(); err != nil {
os.Exit(1)
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ require (
resenje.org/singleflight v0.4.1
)

require github.com/mattn/go-runewidth v0.0.9 // indirect

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
Expand Down Expand Up @@ -110,6 +112,7 @@ require (
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.7 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand All @@ -361,6 +363,8 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
Expand Down
98 changes: 98 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cmd

import (
"os"
"strings"

"github.com/Permify/permify/pkg/cmd/flags"

"github.com/gookit/color"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Inspect permify configuration and environment variables ",
RunE: func(cmd *cobra.Command, args []string) error {
data := prepareConfigData(cmd)
renderConfigTable(data)
return nil
},
}

flags.RegisterServeFlags(cmd)

return cmd
}

func prepareConfigData(cmd *cobra.Command) [][]string {
data := [][]string{}
for _, key := range viper.AllKeys() {
viperKey, viperValue, source := getConfigDetails(key, cmd)
data = append(data, []string{color.FgCyan.Render(viperKey), viperValue, source})
}
return data
}

func getConfigDetails(key string, cmd *cobra.Command) (string, string, string) {
envKey := strings.ToUpper(strings.ReplaceAll(key, ".", "_"))
viperKey := "PERMIFY_" + envKey
viperValue := viper.GetString(key)
envValue, envExists := os.LookupEnv(viperKey)

source := getKeyOrigin(envExists, cmd, key)
value := viperValue
if envExists {
value = envValue
}
return viperKey, obfuscateSecrets(viperKey, value), source
}

func getKeyOrigin(envExists bool, cmd *cobra.Command, key string) string {
if cmd.Flags().Changed(strings.ReplaceAll(key, ".", "-")) {
return color.FgLightGreen.Render("flag")
}
if envExists {
return color.FgLightBlue.Render("env")
}
return color.FgYellow.Render("file")
}

func renderConfigTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Key", "Value", "Source"})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
table.SetCenterSeparator("|")
for _, v := range data {
table.Append(v)
}
table.Render()
}

func obfuscateSecrets(key, value string) string {
secrets := []string{
"PERMIFY_DATABASE_URI",
}

for _, wKey := range secrets {
if key == wKey {
if len(value) < 3 {
return value
}
if lastColon := strings.LastIndex(value, ":"); lastColon != -1 {
typeEnd := strings.Index(value[lastColon:], "/")
if typeEnd != -1 {
if len(value)-3 > lastColon+typeEnd+1 {
return value[:lastColon+typeEnd+1] + "***" + value[len(value)-3:]
}
return value
}
}
return value[:len(value)-3] + "***"
}
}
return value
}

0 comments on commit 3b8a6ba

Please sign in to comment.