chore: add code
This commit is contained in:
		
							parent
							
								
									99984318f4
								
							
						
					
					
						commit
						0111c08796
					
				@ -1,7 +1,5 @@
 | 
				
			|||||||
# Data Utils  Library 
 | 
					# Data Utils  Library 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<img style="margin-top: 1em;width: 300px;border: 0" width="300" alt="Fork me on GitHub" src="jesus/servcvgen/logo/servcvgen_b.svg?sanitize=true">
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Data Utils library for [ServCVgen](/jesus/servcvgen) Services for CVgen to compose and generate Curriculum Vitae from YAML data
 | 
					### Data Utils library for [ServCVgen](/jesus/servcvgen) Services for CVgen to compose and generate Curriculum Vitae from YAML data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										268
									
								
								datautils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										268
									
								
								datautils.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,268 @@
 | 
				
			|||||||
 | 
					package datautils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io/ioutil"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"os/exec"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// "io/ioutil"
 | 
				
			||||||
 | 
						// "path/filepath"
 | 
				
			||||||
 | 
						// "time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						//"github.com/go-git/go-git/v5"
 | 
				
			||||||
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var DEBUGLEVEL int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func LoadPath(path string) ([]byte,error) {
 | 
				
			||||||
 | 
						content, err := ioutil.ReadFile(path)
 | 
				
			||||||
 | 
					  if err != nil {
 | 
				
			||||||
 | 
							return nil,err
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
						return content, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func DecoderFromFile(path string) (*yaml.Decoder, *os.File, error) {
 | 
				
			||||||
 | 
						file, err := os.Open(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						 	// log.Fatalf("open file error: %v", err)
 | 
				
			||||||
 | 
						 	fmt.Printf("open file error: %v\n", err)
 | 
				
			||||||
 | 
							return nil,nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return yaml.NewDecoder(file), file, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func ExistsPathErr(path string) error {
 | 
				
			||||||
 | 
					  _, err := os.Stat(path)
 | 
				
			||||||
 | 
					  if errors.Is(err, os.ErrNotExist) {
 | 
				
			||||||
 | 
							return err // errors.New("File not exists")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func ExistsPath(path string) bool {
 | 
				
			||||||
 | 
					  _, err := os.Stat(path)
 | 
				
			||||||
 | 
					  return !errors.Is(err, os.ErrNotExist)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func CheckDirPath(path string) error {
 | 
				
			||||||
 | 
						if ! ExistsPath(path) {
 | 
				
			||||||
 | 
							os.Mkdir(path, os.FileMode(0755))
 | 
				
			||||||
 | 
							if err := ExistsPathErr(path); err != nil {
 | 
				
			||||||
 | 
						    fmt.Printf("Error creating path %s: %v\n", path,err)
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}	
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func WriteData(data string, path string) error {
 | 
				
			||||||
 | 
						//fmt.Printf("Write %s\n", path)
 | 
				
			||||||
 | 
						f, err := os.Create(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error create %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						w := bufio.NewWriter(f)
 | 
				
			||||||
 | 
					  n4, err := w.WriteString(data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error writing %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if DEBUGLEVEL > 0 {
 | 
				
			||||||
 | 
					    fmt.Printf("%s wrote %d bytes\n", path, n4)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					  w.Flush()
 | 
				
			||||||
 | 
					  return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func WriteAppendData(data string, path string) error {
 | 
				
			||||||
 | 
						if ! ExistsPath(path) {
 | 
				
			||||||
 | 
					    return WriteData(data,path)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0644)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error create %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer file.Close()
 | 
				
			||||||
 | 
					  n4, err := file.WriteString(data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error writing %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if DEBUGLEVEL > 0 {
 | 
				
			||||||
 | 
					    fmt.Printf("%s wrote %d bytes\n", path, n4)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					  return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func ExecCommand(command string, args []string, bckg bool, quiet bool) (*exec.Cmd,string,error) {
 | 
				
			||||||
 | 
						cmd := exec.Command(command, args...)
 | 
				
			||||||
 | 
						cmd.Stdin = strings.NewReader("")
 | 
				
			||||||
 | 
						var out bytes.Buffer
 | 
				
			||||||
 | 
						cmd.Stdout = &out
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
						if bckg {
 | 
				
			||||||
 | 
							err = cmd.Start()
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
						  err = cmd.Run()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  if !quiet {
 | 
				
			||||||
 | 
						 		fmt.Printf("Error exec command %s: %#v\n", cmd,err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return cmd,out.String(),err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if !quiet {
 | 
				
			||||||
 | 
							fmt.Printf("%q\n", out.String())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return cmd,out.String(),nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					// https://github.com/go-git/go-git/blob/master/_examples/commit/main.go
 | 
				
			||||||
 | 
					func GitPull(path string, name string,bckg bool, quiet bool) (*exec.Cmd, error) {
 | 
				
			||||||
 | 
						fmt.Printf("Git Pull: %#v\n", path)
 | 
				
			||||||
 | 
						cmd, _, err := ExecCommand("git",[]string{"-C",path,"pull",name},bckg,quiet)
 | 
				
			||||||
 | 
						return cmd,err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func GitCommit(path string, file string, msg string, target string, bckg bool, quiet bool) (*exec.Cmd, string, error) { 
 | 
				
			||||||
 | 
						fmt.Printf("Git Commit: %#v\n", path)
 | 
				
			||||||
 | 
						var cmd *exec.Cmd
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
						var out string
 | 
				
			||||||
 | 
						if cmd,out,err = ExecCommand("git",[]string{"-C",path,"add",file},bckg,quiet); err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error git add %s: %s\n %#v\n", path,out,err)
 | 
				
			||||||
 | 
					    //return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if _,_,err := ExecCommand("git",[]string{"-C",path,"commit","-m",msg},bckg,quiet); err != nil {
 | 
				
			||||||
 | 
						  //if len(out) != 0 {
 | 
				
			||||||
 | 
						  //fmt.Printf("Error commit %s: %s\n %#v\n", path, out,err)
 | 
				
			||||||
 | 
							//  return err
 | 
				
			||||||
 | 
							// }
 | 
				
			||||||
 | 
							return nil,"",nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// if cmd,out,err := execCommand("git",[]string{"-C",path,"diff","--stat","--cached",bckg,target},quiet); err != nil {
 | 
				
			||||||
 | 
						//   if len(out) == 0 {
 | 
				
			||||||
 | 
						// 	  return nil
 | 
				
			||||||
 | 
						// 	} else {
 | 
				
			||||||
 | 
						//     fmt.Printf("Error commit %s: %s\n %#v\n", path, out,err)
 | 
				
			||||||
 | 
						// 	  return err
 | 
				
			||||||
 | 
						// 	}
 | 
				
			||||||
 | 
						// } 
 | 
				
			||||||
 | 
						if cmd,out,err = ExecCommand("git",[]string{"-C",path,"push",target},bckg,quiet); err != nil {
 | 
				
			||||||
 | 
							return cmd,out,err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					  return cmd,out,nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func GetFileMatch(root string, extension string) []string {
 | 
				
			||||||
 | 
						var listFiles[]string
 | 
				
			||||||
 | 
					  err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Println(err)
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if !info.IsDir() && filepath.Ext(path) == extension {
 | 
				
			||||||
 | 
								listFiles = append(listFiles, path)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
					  }) 
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("No files found in %s: %v\n",root, err)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return listFiles
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					func gitGetWorktree(path string) (*git.Repository, *git.Worktree, error) {
 | 
				
			||||||
 | 
						repo, err := git.PlainOpen(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error path %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return nil,nil,err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						worktree, err := repo.Worktree()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error worktree %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return nil,nil,err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return repo,worktree,nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func gitPull(path string, name string) error {
 | 
				
			||||||
 | 
						fmt.Printf("Git Pull: %#v\n", path)
 | 
				
			||||||
 | 
						repo,worktree,err := gitGetWorktree(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error worktree %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = worktree.Pull(&git.PullOptions{RemoteName: name})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error pull %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if DEBUGLEVEL > 0 {
 | 
				
			||||||
 | 
							// Print the latest commit that was just pulled
 | 
				
			||||||
 | 
							ref, err := repo.Head()
 | 
				
			||||||
 | 
						  if err != nil {
 | 
				
			||||||
 | 
						    fmt.Printf("Error repo Head %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					      return err
 | 
				
			||||||
 | 
					  	}
 | 
				
			||||||
 | 
							commit, err := repo.CommitObject(ref.Hash())
 | 
				
			||||||
 | 
						  if err != nil {
 | 
				
			||||||
 | 
						    fmt.Printf("Error commit object %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					      return err
 | 
				
			||||||
 | 
					  	}
 | 
				
			||||||
 | 
						  fmt.Println(commit)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}	
 | 
				
			||||||
 | 
					func gitCommit(path string, file string, msg string, name string, email string, when time.Time ) error {
 | 
				
			||||||
 | 
						// Opens an already existing repository.
 | 
				
			||||||
 | 
						repo,worktree,err := gitGetWorktree(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error worktree %s: %v\n", path, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Info("echo \"hello world!\" > example-git-file")
 | 
				
			||||||
 | 
						//filename := filepath.Join(path, file)
 | 
				
			||||||
 | 
						//err = ioutil.WriteFile(filename, []byte("hello world!"), 0644)
 | 
				
			||||||
 | 
						// Adds the new file to the staging area.
 | 
				
			||||||
 | 
						_, err = worktree.Add(file)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error git add %s: %v\n", file, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("git add %s\n",file)
 | 
				
			||||||
 | 
						if DEBUGLEVEL > 0 {
 | 
				
			||||||
 | 
							status, err := worktree.Status()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("Error git status %s: %v\n", file, err)	
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							fmt.Println(status)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Commits the current staging area to the repository, with the new file
 | 
				
			||||||
 | 
						// just created. We should provide the object.Signature of Author of the
 | 
				
			||||||
 | 
						// commit Since version 5.0.1, we can omit the Author signature, being read
 | 
				
			||||||
 | 
						// from the git config files.
 | 
				
			||||||
 | 
						// Info("git commit -m \"example go-git commit\"")
 | 
				
			||||||
 | 
						commit, err := worktree.Commit(msg, &git.CommitOptions{
 | 
				
			||||||
 | 
							Author: &object.Signature{
 | 
				
			||||||
 | 
								Name:  name,
 | 
				
			||||||
 | 
								Email: email,
 | 
				
			||||||
 | 
								When:  when,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
						  fmt.Printf("Error git create commit %s: %v\n", file, err)	
 | 
				
			||||||
 | 
					    return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Prints the current HEAD to verify that all worked well.
 | 
				
			||||||
 | 
						// Info("git show -s")
 | 
				
			||||||
 | 
						obj, err := repo.CommitObject(commit)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("Error git commit %s: %v\n", file, err)	
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(obj)
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
							
								
								
									
										5
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					module datautils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.17
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 | 
				
			||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 | 
				
			||||||
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
 | 
					gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
 | 
				
			||||||
 | 
					gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user