@@ -10,6 +10,7 @@ package impl
1010import (
1111 "bufio"
1212 "bytes"
13+ "encoding/base64"
1314 "fmt"
1415 "github.com/fatih/color"
1516 ms "github.com/mitchellh/mapstructure"
@@ -107,6 +108,45 @@ func (cmdCmd *CmdCmd) runCmd(whichtype string, f func()) {
107108
108109}
109110
111+ func decodeABinaryFile (srcpath , destpath string ) {
112+ if _ , err := os .Stat (srcpath ); ! os .IsNotExist (err ) {
113+ f , _ := os .Open (srcpath )
114+ defer f .Close ()
115+ reader := bufio .NewReader (f )
116+ encoded , _ := ioutil .ReadAll (reader )
117+
118+ decoded , err := base64 .StdEncoding .DecodeString (string (encoded ))
119+ if err != nil {
120+ u .LogWarn ("base64 decode" , "base64 decoding failed" )
121+ }
122+
123+ e := ioutil .WriteFile (destpath , decoded , 0644 )
124+
125+ if e != nil {
126+ u .LogWarn ("base64 decode create file" , "create file failed" )
127+ return
128+ }
129+
130+ } else {
131+ u .LogWarn ("base64 decode binary file" , "file does not exist" )
132+ }
133+
134+ }
135+
136+ func encodeABinaryFile (filepath string ) string {
137+ if _ , err := os .Stat (filepath ); ! os .IsNotExist (err ) {
138+ f , _ := os .Open (filepath )
139+ defer f .Close ()
140+ reader := bufio .NewReader (f )
141+ content , _ := ioutil .ReadAll (reader )
142+ encoded := base64 .StdEncoding .EncodeToString (content )
143+ return encoded
144+ } else {
145+ u .LogWarn ("base64 encode binary file" , "file does not exist" )
146+ return ""
147+ }
148+ }
149+
110150func (f * CmdFuncAction ) Exec () {
111151
112152 for idx , cmdItem := range * f .Cmds {
@@ -167,6 +207,52 @@ func (f *CmdFuncAction) Exec() {
167207 TaskRuntime ().ExecbaseVars .Put (reg , filename )
168208 })
169209
210+ case "base64EncodeFile" :
211+ cmdItem .runCmd ("map" , func () {
212+ cmd := cmdItem .Cmd .(map [interface {}]interface {})
213+
214+ var raw , dest , src string
215+
216+ for k , v := range cmd {
217+ switch k .(string ) {
218+ case "dest" :
219+ raw = v .(string )
220+ dest = Render (raw , f .Vars )
221+ case "src" :
222+ raw = v .(string )
223+ src = Render (raw , f .Vars )
224+ }
225+ }
226+ if src == "" || dest == "" {
227+ u .LogWarn ("param validate" , "src and dest can not be empty" )
228+ }
229+
230+ b64Str := encodeABinaryFile (src )
231+ ioutil .WriteFile (dest , []byte (b64Str ), 0644 )
232+ })
233+
234+ case "base64DecodeFile" :
235+ cmdItem .runCmd ("map" , func () {
236+ cmd := cmdItem .Cmd .(map [interface {}]interface {})
237+
238+ var raw , dest , src string
239+
240+ for k , v := range cmd {
241+ switch k .(string ) {
242+ case "dest" :
243+ raw = v .(string )
244+ dest = Render (raw , f .Vars )
245+ case "src" :
246+ raw = v .(string )
247+ src = Render (raw , f .Vars )
248+ }
249+ }
250+ if src == "" || dest == "" {
251+ u .LogWarn ("param validate" , "src and dest can not be empty" )
252+ }
253+ decodeABinaryFile (src , dest )
254+ })
255+
170256 case "colorPrint" :
171257
172258 cmdItem .runCmd ("map" , func () {
0 commit comments