servcvgen/utils.go

247 lines
6.4 KiB
Go

package main
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"
)
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
}
// https://github.com/go-git/go-git/blob/master/_examples/commit/main.go
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 execCommand(command string, args []string,quiet bool) (string,error) {
cmd := exec.Command(command, args...)
cmd.Stdin = strings.NewReader("")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
if !quiet {
fmt.Printf("Error exec command %s: %#v\n", cmd,err)
}
return out.String(),err
}
if !quiet {
fmt.Printf("%q\n", out.String())
}
return out.String(),nil
}
func gitPull(path string, name string,quiet bool) error {
fmt.Printf("Git Pull: %#v\n", path)
_, err := execCommand("git",[]string{"-C",path,"pull",name},quiet)
return err
}
func gitCommit(path string, file string, msg string, target string, quiet bool) error {
fmt.Printf("Git Commit: %#v\n", path)
if out,err := execCommand("git",[]string{"-C",path,"add",file},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},quiet); err != nil {
//if len(out) != 0 {
//fmt.Printf("Error commit %s: %s\n %#v\n", path, out,err)
// return err
// }
return nil
}
// if out,err := execCommand("git",[]string{"-C",path,"diff","--stat","--cached",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 _,err := execCommand("git",[]string{"-C",path,"push",target},quiet); err != nil {
return err
}
return nil
}
func getFileMatch(root string, extension string, fullpath bool, withExtension bool) []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 {
filepath := path
if !fullpath {
filepath = strings.Replace(path,fmt.Sprintf("%s/",root),"",-1)
}
if !withExtension {
filepath = strings.Replace(filepath,fmt.Sprintf("%s",extension),"",-1)
}
listFiles = append(listFiles, filepath)
}
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
}
*/