chore: formating
This commit is contained in:
parent
0111c08796
commit
26c57e9da8
92
datautils.go
92
datautils.go
@ -23,36 +23,36 @@ var DEBUGLEVEL int
|
|||||||
|
|
||||||
func LoadPath(path string) ([]byte,error) {
|
func LoadPath(path string) ([]byte,error) {
|
||||||
content, err := ioutil.ReadFile(path)
|
content, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
return content, nil
|
return content, nil
|
||||||
}
|
}
|
||||||
func DecoderFromFile(path string) (*yaml.Decoder, *os.File, error) {
|
func DecoderFromFile(path string) (*yaml.Decoder, *os.File, error) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// log.Fatalf("open file error: %v", err)
|
// log.Fatalf("open file error: %v", err)
|
||||||
fmt.Printf("open file error: %v\n", err)
|
fmt.Printf("open file error: %v\n", err)
|
||||||
return nil,nil, err
|
return nil,nil, err
|
||||||
}
|
}
|
||||||
return yaml.NewDecoder(file), file, nil
|
return yaml.NewDecoder(file), file, nil
|
||||||
}
|
}
|
||||||
func ExistsPathErr(path string) error {
|
func ExistsPathErr(path string) error {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
return err // errors.New("File not exists")
|
return err // errors.New("File not exists")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func ExistsPath(path string) bool {
|
func ExistsPath(path string) bool {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
return !errors.Is(err, os.ErrNotExist)
|
return !errors.Is(err, os.ErrNotExist)
|
||||||
}
|
}
|
||||||
func CheckDirPath(path string) error {
|
func CheckDirPath(path string) error {
|
||||||
if ! ExistsPath(path) {
|
if ! ExistsPath(path) {
|
||||||
os.Mkdir(path, os.FileMode(0755))
|
os.Mkdir(path, os.FileMode(0755))
|
||||||
if err := ExistsPathErr(path); err != nil {
|
if err := ExistsPathErr(path); err != nil {
|
||||||
fmt.Printf("Error creating path %s: %v\n", path,err)
|
fmt.Printf("Error creating path %s: %v\n", path,err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,20 +62,20 @@ func WriteData(data string, path string) error {
|
|||||||
//fmt.Printf("Write %s\n", path)
|
//fmt.Printf("Write %s\n", path)
|
||||||
f, err := os.Create(path)
|
f, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error create %s: %v\n", path, err)
|
fmt.Printf("Error create %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w := bufio.NewWriter(f)
|
w := bufio.NewWriter(f)
|
||||||
n4, err := w.WriteString(data)
|
n4, err := w.WriteString(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error writing %s: %v\n", path, err)
|
fmt.Printf("Error writing %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if DEBUGLEVEL > 0 {
|
if DEBUGLEVEL > 0 {
|
||||||
fmt.Printf("%s wrote %d bytes\n", path, n4)
|
fmt.Printf("%s wrote %d bytes\n", path, n4)
|
||||||
}
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func WriteAppendData(data string, path string) error {
|
func WriteAppendData(data string, path string) error {
|
||||||
if ! ExistsPath(path) {
|
if ! ExistsPath(path) {
|
||||||
@ -83,19 +83,19 @@ func WriteAppendData(data string, path string) error {
|
|||||||
}
|
}
|
||||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0644)
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error create %s: %v\n", path, err)
|
fmt.Printf("Error create %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
n4, err := file.WriteString(data)
|
n4, err := file.WriteString(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error writing %s: %v\n", path, err)
|
fmt.Printf("Error writing %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if DEBUGLEVEL > 0 {
|
if DEBUGLEVEL > 0 {
|
||||||
fmt.Printf("%s wrote %d bytes\n", path, n4)
|
fmt.Printf("%s wrote %d bytes\n", path, n4)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func ExecCommand(command string, args []string, bckg bool, quiet bool) (*exec.Cmd,string,error) {
|
func ExecCommand(command string, args []string, bckg bool, quiet bool) (*exec.Cmd,string,error) {
|
||||||
cmd := exec.Command(command, args...)
|
cmd := exec.Command(command, args...)
|
||||||
@ -106,11 +106,11 @@ func ExecCommand(command string, args []string, bckg bool, quiet bool) (*exec.Cm
|
|||||||
if bckg {
|
if bckg {
|
||||||
err = cmd.Start()
|
err = cmd.Start()
|
||||||
} else {
|
} else {
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
fmt.Printf("Error exec command %s: %#v\n", cmd,err)
|
fmt.Printf("Error exec command %s: %#v\n", cmd,err)
|
||||||
}
|
}
|
||||||
return cmd,out.String(),err
|
return cmd,out.String(),err
|
||||||
}
|
}
|
||||||
@ -131,7 +131,7 @@ func GitCommit(path string, file string, msg string, target string, bckg bool, q
|
|||||||
var err error
|
var err error
|
||||||
var out string
|
var out string
|
||||||
if cmd,out,err = ExecCommand("git",[]string{"-C",path,"add",file},bckg,quiet); err != nil {
|
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)
|
fmt.Printf("Error git add %s: %s\n %#v\n", path,out,err)
|
||||||
//return err
|
//return err
|
||||||
}
|
}
|
||||||
if _,_,err := ExecCommand("git",[]string{"-C",path,"commit","-m",msg},bckg,quiet); err != nil {
|
if _,_,err := ExecCommand("git",[]string{"-C",path,"commit","-m",msg},bckg,quiet); err != nil {
|
||||||
@ -152,11 +152,11 @@ func GitCommit(path string, file string, msg string, target string, bckg bool, q
|
|||||||
if cmd,out,err = ExecCommand("git",[]string{"-C",path,"push",target},bckg,quiet); err != nil {
|
if cmd,out,err = ExecCommand("git",[]string{"-C",path,"push",target},bckg,quiet); err != nil {
|
||||||
return cmd,out,err
|
return cmd,out,err
|
||||||
}
|
}
|
||||||
return cmd,out,nil
|
return cmd,out,nil
|
||||||
}
|
}
|
||||||
func GetFileMatch(root string, extension string) []string {
|
func GetFileMatch(root string, extension string) []string {
|
||||||
var listFiles[]string
|
var listFiles[]string
|
||||||
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return nil
|
return nil
|
||||||
@ -165,7 +165,7 @@ func GetFileMatch(root string, extension string) []string {
|
|||||||
listFiles = append(listFiles, path)
|
listFiles = append(listFiles, path)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("No files found in %s: %v\n",root, err)
|
fmt.Printf("No files found in %s: %v\n",root, err)
|
||||||
return nil
|
return nil
|
||||||
@ -176,12 +176,12 @@ func GetFileMatch(root string, extension string) []string {
|
|||||||
func gitGetWorktree(path string) (*git.Repository, *git.Worktree, error) {
|
func gitGetWorktree(path string) (*git.Repository, *git.Worktree, error) {
|
||||||
repo, err := git.PlainOpen(path)
|
repo, err := git.PlainOpen(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error path %s: %v\n", path, err)
|
fmt.Printf("Error path %s: %v\n", path, err)
|
||||||
return nil,nil,err
|
return nil,nil,err
|
||||||
}
|
}
|
||||||
worktree, err := repo.Worktree()
|
worktree, err := repo.Worktree()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error worktree %s: %v\n", path, err)
|
fmt.Printf("Error worktree %s: %v\n", path, err)
|
||||||
return nil,nil,err
|
return nil,nil,err
|
||||||
}
|
}
|
||||||
return repo,worktree,nil
|
return repo,worktree,nil
|
||||||
@ -190,27 +190,27 @@ func gitPull(path string, name string) error {
|
|||||||
fmt.Printf("Git Pull: %#v\n", path)
|
fmt.Printf("Git Pull: %#v\n", path)
|
||||||
repo,worktree,err := gitGetWorktree(path)
|
repo,worktree,err := gitGetWorktree(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error worktree %s: %v\n", path, err)
|
fmt.Printf("Error worktree %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = worktree.Pull(&git.PullOptions{RemoteName: name})
|
err = worktree.Pull(&git.PullOptions{RemoteName: name})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error pull %s: %v\n", path, err)
|
fmt.Printf("Error pull %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if DEBUGLEVEL > 0 {
|
if DEBUGLEVEL > 0 {
|
||||||
// Print the latest commit that was just pulled
|
// Print the latest commit that was just pulled
|
||||||
ref, err := repo.Head()
|
ref, err := repo.Head()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error repo Head %s: %v\n", path, err)
|
fmt.Printf("Error repo Head %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
commit, err := repo.CommitObject(ref.Hash())
|
commit, err := repo.CommitObject(ref.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error commit object %s: %v\n", path, err)
|
fmt.Printf("Error commit object %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(commit)
|
fmt.Println(commit)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -218,7 +218,7 @@ func gitCommit(path string, file string, msg string, name string, email string,
|
|||||||
// Opens an already existing repository.
|
// Opens an already existing repository.
|
||||||
repo,worktree,err := gitGetWorktree(path)
|
repo,worktree,err := gitGetWorktree(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error worktree %s: %v\n", path, err)
|
fmt.Printf("Error worktree %s: %v\n", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Info("echo \"hello world!\" > example-git-file")
|
// Info("echo \"hello world!\" > example-git-file")
|
||||||
@ -227,8 +227,8 @@ func gitCommit(path string, file string, msg string, name string, email string,
|
|||||||
// Adds the new file to the staging area.
|
// Adds the new file to the staging area.
|
||||||
_, err = worktree.Add(file)
|
_, err = worktree.Add(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error git add %s: %v\n", file, err)
|
fmt.Printf("Error git add %s: %v\n", file, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("git add %s\n",file)
|
fmt.Printf("git add %s\n",file)
|
||||||
if DEBUGLEVEL > 0 {
|
if DEBUGLEVEL > 0 {
|
||||||
@ -252,7 +252,7 @@ func gitCommit(path string, file string, msg string, name string, email string,
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error git create commit %s: %v\n", file, err)
|
fmt.Printf("Error git create commit %s: %v\n", file, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Prints the current HEAD to verify that all worked well.
|
// Prints the current HEAD to verify that all worked well.
|
||||||
|
Loading…
Reference in New Issue
Block a user