Skip to content

Commit

Permalink
feat(expand): Add expand env variables in files
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Aug 3, 2021
1 parent bc357ee commit 6251311
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

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

import (
"encoding/json"
"fmt"
"io/ioutil"
"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: "file",
Short: "Expand environment variables in file",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
source, err := ioutil.ReadFile(CmdFlagSource)
if err != nil {
panic(err)
}

if rootcmd.RootCmdFlagJson {
outJson, err := json.Marshal(os.ExpandEnv(string(source)))
if err != nil {
panic(err)
}
fmt.Println(string(outJson))
} else {
fmt.Println(os.ExpandEnv(string(source)))
}
},
}

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

0 comments on commit 6251311

Please sign in to comment.