Skip to content

Commit

Permalink
add the first version of the utility
Browse files Browse the repository at this point in the history
  • Loading branch information
robotomize committed May 11, 2023
1 parent f5ae72e commit 77c4567
Show file tree
Hide file tree
Showing 10 changed files with 1,057 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## goprintenv

A command line utility that displays the environment variables used by a Go project

### Status

The project is under development, it may not find all environment variables, keep this in mind when using it.

```shell
goprintenv -p <go-project-source>
```

### Install

```shell
go install github.com/robotomize/go-printenv/cmd/goprintenv@latest
```

### Example

Currently only processes env tags

```go
type Nested struct {
Live bool `env:"LIVE,default=true"`
}

type NestedStruct struct {
LastName string `env:"LAST_NAME,default=IVANOV" json:"last_name" bson:"lastName"`
Two Nested `env:",prefix=TWO_"`
}

```

The result will be the following

```shell
LAST_NAME=IVANOV
TWO_LIVE=true
```

### @TODO
* add ci, golangci-lint, Makefile, Dockerfile
* Implement parsing of built-in structures
* Should parse os.Getenv
* Support more env tags
* Refactor, simplify the code. Fix bugs and write unit tests
12 changes: 12 additions & 0 deletions cmd/goprintenv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"os"
)

func main() {
if err := rootCmd.Execute(); err != nil {
rootCmd.Println(err)
os.Exit(1)
}
}
53 changes: 53 additions & 0 deletions cmd/goprintenv/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"github.com/spf13/cobra"
"gituhb.com/robotomize/go-printenv/internal/analysis"
"gituhb.com/robotomize/go-printenv/internal/printer"
)

var (
verboseFlag bool
goProjectPath string
)

func init() {
rootCmd.PersistentFlags().BoolVarP(
&verboseFlag,
"verbose",
"v",
false,
"more verbose",
)
rootCmd.PersistentFlags().StringVarP(
&goProjectPath,
"path",
"p",
".",
"go project path",
)
}

var rootCmd = &cobra.Command{
Use: "goprintenv [-p project path -v verbose]",
Long: "Print env variables used in go project",
RunE: func(cmd *cobra.Command, args []string) error {

ctx := cmd.Context()
var printOpts []printer.Option

a := analysis.New(
goProjectPath, func(items ...analysis.OutputEntry) analysis.Printer {
return printer.New(items, printOpts...)
},
)

if _, err := cmd.OutOrStdout().Write(a.Print()); err != nil {
return err
}

_ = ctx

return nil
},
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module gituhb.com/robotomize/go-printenv

go 1.19

require (
github.com/spf13/cobra v1.6.1
golang.org/x/tools v0.6.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 77c4567

Please sign in to comment.