Skip to content

Commit

Permalink
Merge pull request #18 from srevinsaju/feature/fmt
Browse files Browse the repository at this point in the history
Formatting Feature (togomak fmt)
  • Loading branch information
srevinsaju authored Jun 21, 2023
2 parents a026b5a + 6e17159 commit 24bef6e
Show file tree
Hide file tree
Showing 23 changed files with 190 additions and 60 deletions.
32 changes: 31 additions & 1 deletion cmd/togomak/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ func main() {
Aliases: []string{"ls", "l"},
Action: list,
},
{
Name: "fmt",
Usage: "format a pipeline file",
Action: format,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "check",
Usage: "check if the file is formatted",
Aliases: []string{"c"},
},
&cli.BoolFlag{
Name: "recursive",
Usage: "format all the files named togomak.hcl in the current directory, and its children",
Aliases: []string{"r"},
},
},
},
{
Name: "cache",
Usage: "manage the cache",
Expand All @@ -52,6 +69,13 @@ func main() {
Name: "clean",
Usage: "clean the cache",
Action: cleanCache,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "recursive",
Usage: "clean the cache recursively",
Aliases: []string{"r"},
},
},
},
},
},
Expand Down Expand Up @@ -189,6 +213,7 @@ func run(ctx *cli.Context) error {
}

func cleanCache(ctx *cli.Context) error {
recursive := ctx.Bool("recursive")
owd, err := os.Getwd()
if err != nil {
panic(err)
Expand All @@ -197,11 +222,16 @@ func cleanCache(ctx *cli.Context) error {
if dir == "" {
dir = owd
}
cache.CleanCache(dir)
cache.CleanCache(dir, recursive)
return nil
}

func list(ctx *cli.Context) error {
cfg := newConfigFromCliContext(ctx)
return orchestra.List(cfg)
}

func format(ctx *cli.Context) error {
cfg := newConfigFromCliContext(ctx)
return orchestra.Format(cfg, ctx.Bool("check"), ctx.Bool("recursive"))
}
4 changes: 2 additions & 2 deletions examples/conditions/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ togomak {
}

data "env" "home" {
key = "HOME"
key = "HOME"
default = "@"
}

stage "example" {
if = data.env.home.value != "@"
if = data.env.home.value != "@"
name = "example"
script = "echo hello world"
}
2 changes: 1 addition & 1 deletion examples/docker/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ stage "example" {
container {
image = "ubuntu"
volume {
source = "${cwd}/diary"
source = "${cwd}/diary"
destination = "/newdiary"
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/files/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ togomak {


stage "list" {
name = "listing files"
name = "listing files"
args = ["ls", "-al"]
}
2 changes: 1 addition & 1 deletion examples/git/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ togomak {
}

data "git" "repo" {
url = "https://github.com/srevinsaju/togomak"
url = "https://github.com/srevinsaju/togomak"
files = ["togomak.hcl"]
}

Expand Down
2 changes: 1 addition & 1 deletion examples/locals/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ togomak {

locals {
nerv_headquarters = "Tokyo-3"
pilot_name = "Shinji"
pilot_name = "Shinji"
}

stage "eva01_synchronization" {
Expand Down
30 changes: 15 additions & 15 deletions examples/macros/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ togomak {
}

macro "explode" {
stage "explode" {
script = <<-EOT
stage "explode" {
script = <<-EOT
for i in $(seq 1 10); do
sleep 0.1
echo "${param.eva}: Loading $i..."
done
echo "${param.eva}: entry plug connected! pilot ${param.pilot} synchronized! 🤖"
EOT
}
}
}


stage "entry_plug_eva01" {
use {
macro = macro.explode
parameters = {
pilot = "Shinji Ikari 🙅‍♂️"
eva = "01"
}
use {
macro = macro.explode
parameters = {
pilot = "Shinji Ikari 🙅‍♂️"
eva = "01"
}
}
}

stage "entry_plug_eva02" {
use {
macro = macro.explode
parameters = {
pilot = "Asuka Langley Soryu 🙅‍♀️"
eva = "02"
}
use {
macro = macro.explode
parameters = {
pilot = "Asuka Langley Soryu 🙅‍♀️"
eva = "02"
}
}
}


4 changes: 2 additions & 2 deletions examples/output/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ stage "agent" {

stage "seele" {
depends_on = [stage.agent]
name = "seele"
script = "echo The agent from Seele reporting! ${output.AGENT}"
name = "seele"
script = "echo The agent from Seele reporting! ${output.AGENT}"
}
6 changes: 3 additions & 3 deletions examples/prompt/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ togomak {
}

data "env" "quit_if_not_shinji" {
key = "QUIT_IF_NOT_SHINJI"
key = "QUIT_IF_NOT_SHINJI"
default = "false"
}

data "prompt" "name" {
prompt = "What is your name?"
prompt = "What is your name?"
default = "Pen Pen"
}

Expand All @@ -18,7 +18,7 @@ stage "example" {
}

stage "quit" {
if = data.env.quit_if_not_shinji.value != "false"
if = data.env.quit_if_not_shinji.value != "false"
script = <<-EOT
#!/usr/bin/env bash
set -eux
Expand Down
6 changes: 3 additions & 3 deletions examples/remote-stages/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ togomak {
}

data "git" "eva01_source" {
url = "https://github.com/srevinsaju/togomak"
url = "https://github.com/srevinsaju/togomak"
files = ["togomak.hcl"]
}

macro "gendo_brain" {
files = data.git.eva01_source.files
files = data.git.eva01_source.files
}

stage "build_eva01" {
name = "Building eva unit"
name = "Building eva unit"
use {
macro = macro.gendo_brain
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/alessio/shellescape v1.4.1
github.com/bcicen/jstream v1.0.1
github.com/bmatcuk/doublestar v1.1.5
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/creack/pty v1.1.18
github.com/docker/docker v24.0.2+incompatible
github.com/fatih/color v1.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/bcicen/jstream v1.0.1 h1:BXY7Cu4rdmc0rhyTVyT3UkxAiX3bnLpKLas9btbH5ck=
github.com/bcicen/jstream v1.0.1/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYXYmRuAQ=
github.com/bmatcuk/doublestar v1.1.5 h1:2bNwBOmhyFEFcoB3tGvTD5xanq+4kyOZlB8wFYbMjkk=
github.com/bmatcuk/doublestar v1.1.5/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=
github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down
26 changes: 24 additions & 2 deletions pkg/cache/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"github.com/srevinsaju/togomak/v1/pkg/x"
"os"
"path/filepath"
"sync"
)

func CleanCache(dir string) {
func CleanCache(dir string, recursive bool) {
dirPath := filepath.Join(dir, meta.BuildDirPrefix, "pipelines", "tmp")
filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if path == dirPath {
return nil
}
Expand All @@ -20,5 +21,26 @@ func CleanCache(dir string) {
}
return nil
})
if err != nil {
panic(err)
}
var wg sync.WaitGroup
if recursive {
entries, err := os.ReadDir(dir)
if err != nil {
panic(err)
}
for _, entry := range entries {

if entry.IsDir() {
wg.Add(1)
go func(entry os.DirEntry) {
defer wg.Done()
CleanCache(filepath.Join(dir, entry.Name()), recursive)
}(entry)
}
}
}
wg.Wait()

}
65 changes: 65 additions & 0 deletions pkg/orchestra/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package orchestra

import (
"bytes"
"fmt"
"github.com/bmatcuk/doublestar"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/srevinsaju/togomak/v1/pkg/pipeline"
"os"
)

func Format(cfg Config, check bool, recursive bool) error {
t, ctx := NewContextWithTogomak(cfg)

var toFormat []string

if recursive {
matches, err := doublestar.Glob("**/*.hcl")
for _, path := range matches {
t.logger.Tracef("Found %s", path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
outSrc := hclwrite.Format(data)
if !bytes.Equal(outSrc, data) {
t.logger.Tracef("%s needs formatting", path)
toFormat = append(toFormat, path)
}
}
if err != nil {
t.logger.Fatalf("Error while globbing for **/*.hcl: %s", err)
}
} else {
fn := pipeline.ConfigFilePath(ctx)
data, err := os.ReadFile(fn)
if err != nil {
return err
}
outSrc := hclwrite.Format(data)
if !bytes.Equal(outSrc, data) {
t.logger.Tracef("%s needs formatting", fn)
toFormat = append(toFormat, fn)
}
}
for _, fn := range toFormat {
fmt.Println(fn)
if !check {
data, err := os.ReadFile(fn)
if err != nil {
panic(err)
}
outSrc := hclwrite.Format(data)
err = os.WriteFile(fn, outSrc, 0644)
if err != nil {
panic(err)
}
}
}
if check && len(toFormat) > 0 {
os.Exit(1)
}
return nil

}
9 changes: 7 additions & 2 deletions pkg/pipeline/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"path/filepath"
)

func Read(ctx context.Context, parser *hclparse.Parser) (*ci.Pipeline, hcl.Diagnostics) {

func ConfigFilePath(ctx context.Context) string {
filePath := ctx.Value(c.TogomakContextPipelineFilePath).(string)
if filePath == "" {
filePath = meta.ConfigFileName
Expand All @@ -22,6 +21,12 @@ func Read(ctx context.Context, parser *hclparse.Parser) (*ci.Pipeline, hcl.Diagn
if filepath.IsAbs(filePath) == false {
filePath = filepath.Join(owd, filePath)
}
return filePath
}

func Read(ctx context.Context, parser *hclparse.Parser) (*ci.Pipeline, hcl.Diagnostics) {
filePath := ConfigFilePath(ctx)

f, diags := parser.ParseHCLFile(filePath)

if diags.HasErrors() {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/failing/cant-run-fail/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ togomak {
version = 1
}
stage "example" {
if = this.what
if = this.what
name = "example"
script = "echo hello world"
}
4 changes: 2 additions & 2 deletions tests/tests/failing/dependency-cycles/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ togomak {

stage "example_a" {
depends_on = [stage.example_b]
script = "echo hello world"
script = "echo hello world"
}

stage "example_b" {
depends_on = [stage.example_a]
script = "echo hello again"
script = "echo hello again"
}
2 changes: 1 addition & 1 deletion tests/tests/failing/invalid-block-id/togomak.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ togomak {
}

data "env" "hello_world" {
key = "HOME"
key = "HOME"
default = "@"
}

Expand Down
Loading

0 comments on commit 24bef6e

Please sign in to comment.