Skip to content

Commit

Permalink
extract
Browse files Browse the repository at this point in the history
  • Loading branch information
dazz committed Dec 18, 2023
1 parent 5aa7126 commit 4c323b0
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 113 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
S6_PATH := ./data/s6-overlay/s6-rc.d
S6_PATH := ./examples/s6-overlay/s6-rc.d
ARGS := -p $(S6_PATH)

.PHONY: build
build:
go build -o s6-cli -v ./cmd/s6-cli
@go build -o s6-cli -v ./cmd/s6cli

.PHONY: run
run:
go run ./cmd/s6-cli $(ARGS)
@go run ./cmd/s6cli $(ARGS)

.PHONY: lint
lint:
go run ./cmd/s6-cli $(ARGS) lint
@go run ./cmd/s6cli $(ARGS) lint

.PHONY: mermaid
mermaid:
go run ./cmd/s6-cli $(ARGS) mermaid
@go run ./cmd/s6cli $(ARGS) mermaid
111 changes: 111 additions & 0 deletions cmd/s6cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"fmt"
"log"
"os"
"time"

"github.com/urfave/cli/v2"

"github.com/dazz/s6-cli/pkg/s6cli"
)

func init() {
}

func main() {
app := &cli.App{
Name: "s6-cli",
Version: "0.0.1",
Compiled: time.Now(),
Authors: []*cli.Author{
&cli.Author{
Name: "Anne-Julia Seitz",
Email: "dazz@c-base",
},
},
Usage: "CLI for creating and linting files and directories",
// We'll be using the same flag for all our commands
// so we'll define it up here
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Value: "/etc/s6-overlay/s6-rc.d",
Usage: "Path to s6-rc.d directory",
},
},
Commands: []*cli.Command{
{
Name: "lint",
Aliases: []string{"l"},
Usage: "lint directories and files",
Action: func(cCtx *cli.Context) error {
path := "/etc/s6-overlay/s6-rc.d"
firstBundle := "user"

if cCtx.IsSet("path") {
path = cCtx.String("path")
}
// check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", path)
os.Exit(1)
}

// compile dependency tree
var services []s6cli.Service
var lints []s6cli.Lint
valid := s6cli.Compile(path, firstBundle, &services, &lints)

fmt.Println("*************** s6-cli Lint Report ***************")

// print lints
for _, lint := range lints {
fmt.Printf("* %s: %s\n", lint.Service, lint.Message)
}

fmt.Println("*************** s6-cli Lint Report ***************")

if valid {
return nil
}
os.Exit(1)
return nil
},
},
{
Name: "mermaid",
Aliases: []string{"m"},
Usage: "document s6 service dependencies in mermaid syntax",
Action: func(cCtx *cli.Context) error {
path := "/etc/s6-overlay/s6-rc.d"
firstBundle := "user"

if cCtx.IsSet("path") {
path = cCtx.String("path")
}
// check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", path)
os.Exit(1)
}

// compile dependency tree
var services []s6cli.Service
var lints []s6cli.Lint
s6cli.Compile(path, firstBundle, &services, &lints)

fmt.Printf(s6cli.MermaidGraph(services))

return nil
},
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
115 changes: 7 additions & 108 deletions cmd/s6-cli/main.go → pkg/s6cli/compile.go
Original file line number Diff line number Diff line change
@@ -1,113 +1,13 @@
package main
package s6cli

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/urfave/cli/v2"
)

func init() {
}

func main() {
app := &cli.App{
Name: "s6-cli",
Version: "0.0.1",
Compiled: time.Now(),
Authors: []*cli.Author{
&cli.Author{
Name: "Anne-Julia Seitz",
Email: "dazz@c-base",
},
},
Usage: "CLI for creating and linting files and directories",
// We'll be using the same flag for all our commands
// so we'll define it up here
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Value: "/etc/s6-overlay/s6-rc.d",
Usage: "Path to s6-rc.d directory",
},
},
Commands: []*cli.Command{
{
Name: "lint",
Aliases: []string{"l"},
Usage: "lint directories and files",
Action: func(cCtx *cli.Context) error {
path := "/etc/s6-overlay/s6-rc.d"
firstBundle := "user"

if cCtx.IsSet("path") {
path = cCtx.String("path")
}
// check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", path)
os.Exit(1)
}

// compile dependency tree
var services []Service
var lints []Lint
valid := compileDependencyTree(path, firstBundle, &services, &lints)

fmt.Println("*************** s6-cli Lint Report ***************")

// print lints
for _, lint := range lints {
fmt.Printf("* %s: %s\n", lint.Service, lint.Message)
}

fmt.Println("*************** s6-cli Lint Report ***************")

if valid {
return nil
}
os.Exit(1)
return nil
},
},
{
Name: "mermaid",
Aliases: []string{"m"},
Usage: "document s6 service dependencies in mermaid syntax",
Action: func(cCtx *cli.Context) error {
path := "/etc/s6-overlay/s6-rc.d"
firstBundle := "user"

if cCtx.IsSet("path") {
path = cCtx.String("path")
}
// check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", path)
os.Exit(1)
}

// compile dependency tree
var services []Service
var lints []Lint
compileDependencyTree(path, firstBundle, &services, &lints)

fmt.Printf(renderMermaidGraph(services))

return nil
},
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}

// Define a struct named Person
type Service struct {
Expand All @@ -121,7 +21,7 @@ type Lint struct {
}


func compileDependencyTree(rootPath string, currentService string, services *[]Service, lints *[]Lint) bool {
func Compile(rootPath string, currentService string, services *[]Service, lints *[]Lint) bool {
// Check if the service is already in the services list
if containsService(*services, currentService) {
return true
Expand Down Expand Up @@ -210,16 +110,15 @@ func compileDependencyTree(rootPath string, currentService string, services *[]S
}
}


// get the dependencies
files, err := ioutil.ReadDir(dependenciesDir)
if err != nil {
log.Fatal(err)
}
var dependencies []string
for _, f := range files {
dependencies = append(dependencies, f.Name())
if f.Name() == "base" {
for _, file := range files {
dependencies = append(dependencies, file.Name())
if file.Name() == "base" {
// we don't want to check the base directory
continue
}
Expand All @@ -237,13 +136,13 @@ func compileDependencyTree(rootPath string, currentService string, services *[]S
continue
}
// recursive call
compileDependencyTree(rootPath, dependency, services, lints)
Compile(rootPath, dependency, services, lints)
}

return isValid
}

func renderMermaidGraph(services []Service) string {
func MermaidGraph(services []Service) string {
var graph string
graph = "```mermaid\ngraph TD;\n"
for _, service := range services {
Expand Down

0 comments on commit 4c323b0

Please sign in to comment.