Skip to content

Commit

Permalink
add custom cloning 💥💥
Browse files Browse the repository at this point in the history
  • Loading branch information
VarthanV committed Nov 1, 2024
1 parent aea9998 commit 8d89cc5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module github.com/VarthanV/simple-ci-pipeline-runner
go 1.22.6

require (
github.com/fatih/color v1.18.0 // indirect
github.com/fatih/color v1.18.0
github.com/google/uuid v1.6.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.25.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
26 changes: 23 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@ package main

import (
"context"
"fmt"
"log"
"os"

"github.com/VarthanV/simple-ci-pipeline-runner/pkg/objects"
"github.com/VarthanV/simple-ci-pipeline-runner/pkg/pipeline"
"github.com/fatih/color"
"github.com/google/uuid"
)

func main() {
fmt.Println("hello world")
pipeline.Run(context.Background())
dirName := uuid.NewString()

defer func(dirName string) {
color.Blue("Cleaning up...")
err := os.RemoveAll(dirName)
if err != nil {
log.Println("error in removing dir ", err)
}
}(dirName)

ctx, cancel := context.WithCancel(context.Background())

defer cancel()

// FIXME: can be refactored and improve readablity with custom contexts
// will do in the upcoming iteration
ctx = context.WithValue(ctx, objects.PipelineValueDirectoryName, dirName)
pipeline.Run(ctx)
panic("stacktrace")
}
7 changes: 7 additions & 0 deletions pkg/objects/objects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package objects

type PipelineContextValue string

const (
PipelineValueDirectoryName PipelineContextValue = "pipeline-directory-name"
)
1 change: 1 addition & 0 deletions pkg/pipeline/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "errors"
var (
ErrInvalidRepoURL = errors.New("invalid repo_url")
ErrStageCloneRequired = errors.New("stage clone required")
ErrFileNameRequired = errors.New("file_name required")
)
18 changes: 16 additions & 2 deletions pkg/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"

"github.com/VarthanV/simple-ci-pipeline-runner/pkg/objects"
"github.com/fatih/color"
)

Expand Down Expand Up @@ -70,11 +71,23 @@ func clone(ctx context.Context, pipeline <-chan Pipeline) <-chan Pipeline {
continue
}

dirNameFromCtx := ctx.Value(objects.PipelineValueDirectoryName)
if dirNameFromCtx == nil {
log.Println("filename not set")
errorMsg.Error = ErrFileNameRequired
p.Err = errorMsg
outStream <- p
continue
}

dirName, _ := dirNameFromCtx.(string)

color.Green("############### Stage1: Cloning Repo ######################")
cmd := exec.Command(
"git",
"clone",
cloneStage.RepositoryURL,
dirName,
"--progress")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -114,14 +127,15 @@ func test(ctx context.Context, pipeline <-chan Pipeline) <-chan Pipeline {
_, ok = p.Tasks[TaskStageTest]
if !ok {
outStream <- p
color.Yellow("Stage2: Testing phase not found , Skipping it...")
continue
}

}
}
}()

return pipeline
return outStream
}

func Run(ctx context.Context) {
Expand All @@ -133,7 +147,7 @@ func Run(ctx context.Context) {
},
})

pipeline := clone(ctx, ch)
pipeline := test(ctx, clone(ctx, ch))

for rslt := range pipeline {
log.Println(rslt)
Expand Down

0 comments on commit 8d89cc5

Please sign in to comment.