From 6afe39397fc389ecffb6cc296047861c91c8f5b3 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 11 Sep 2021 20:32:49 +0200 Subject: [PATCH] refactor: Create utils Signed-off-by: Matej Focko --- main.go | 27 --------------------------- utils.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 utils.go diff --git a/main.go b/main.go index 6635f03..2918d4d 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "bufio" "fmt" - "io" "os" "sort" "strconv" @@ -11,19 +10,6 @@ import ( "code.gitea.io/sdk/gitea" ) -type ByLineNum []*gitea.PullReviewComment - -func (a ByLineNum) Len() int { return len(a) } -func (a ByLineNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByLineNum) Less(i, j int) bool { return a[i].LineNum < a[j].LineNum } - -func ExitOnError(err error) { - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -} - func RetrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment { comments, _, err := client.ListPullReviewComments( config.Gitea.Owner, config.Gitea.Repository, prID, reviewID, @@ -49,19 +35,6 @@ func SplitByFile( return splitComments } -func BackUpSource(filepath string) { - inputFile, err := os.Open(filepath) - ExitOnError(err) - defer inputFile.Close() - - BackUpSourceFile, err := os.Create(filepath + ".bck") - ExitOnError(err) - defer BackUpSourceFile.Close() - - _, err = io.Copy(BackUpSourceFile, inputFile) - ExitOnError(err) -} - func WriteCommentsInFile( outputFile *os.File, lineNum uint64, diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..b57019d --- /dev/null +++ b/utils.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "io" + "os" + + "code.gitea.io/sdk/gitea" +) + +type ByLineNum []*gitea.PullReviewComment + +func (a ByLineNum) Len() int { return len(a) } +func (a ByLineNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByLineNum) Less(i, j int) bool { return a[i].LineNum < a[j].LineNum } + +func ExitOnError(err error) { + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func BackUpSource(filepath string) { + inputFile, err := os.Open(filepath) + ExitOnError(err) + defer inputFile.Close() + + BackUpSourceFile, err := os.Create(filepath + ".bck") + ExitOnError(err) + defer BackUpSourceFile.Close() + + _, err = io.Copy(BackUpSourceFile, inputFile) + ExitOnError(err) +}