Skip to content

Commit

Permalink
feat(expand): Add "expand" (env vars expansion) functionality (string…
Browse files Browse the repository at this point in the history
…s only)
  • Loading branch information
ondrejsika committed Aug 3, 2021
1 parent 1db025d commit bc357ee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
_ "github.com/sikalabs/slut/cmd/expand"
_ "github.com/sikalabs/slut/cmd/expand/string"
_ "github.com/sikalabs/slut/cmd/mysql"
_ "github.com/sikalabs/slut/cmd/mysql/create"
_ "github.com/sikalabs/slut/cmd/mysql/drop"
Expand Down
15 changes: 15 additions & 0 deletions cmd/expand/expand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package expand

import (
"github.com/sikalabs/slut/cmd/root"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "expand",
Short: "Expand environment variables in files and strings",
}

func init() {
root.RootCmd.AddCommand(Cmd)
}
43 changes: 43 additions & 0 deletions cmd/expand/string/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package string

import (
"encoding/json"
"fmt"
"os"

expandcmd "github.com/sikalabs/slut/cmd/expand"
rootcmd "github.com/sikalabs/slut/cmd/root"

"github.com/spf13/cobra"
)

var CmdFlagSource string

var Cmd = &cobra.Command{
Use: "string",
Short: "Expand environment variables in string",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
if rootcmd.RootCmdFlagJson {
outJson, err := json.Marshal(os.ExpandEnv(CmdFlagSource))
if err != nil {
panic(err)
}
fmt.Println(string(outJson))
} else {
fmt.Println(os.ExpandEnv(CmdFlagSource))
}
},
}

func init() {
expandcmd.Cmd.AddCommand(Cmd)
Cmd.Flags().StringVarP(
&CmdFlagSource,
"source",
"s",
"",
"Source template string",
)
Cmd.MarkFlagRequired("source")
}

0 comments on commit bc357ee

Please sign in to comment.