Skip to content

Commit

Permalink
add entry files
Browse files Browse the repository at this point in the history
  • Loading branch information
dazz committed Jan 1, 2024
1 parent bdef1f0 commit d36526c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 43 deletions.
79 changes: 73 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
S6_PATH := ./examples/s6-overlay/s6-rc.d
ARGS := -p $(S6_PATH)

.DEFAULT: help

## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'




# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #

## tidy: format code and tidy modfile
.PHONY: tidy
tidy:
go fmt ./...
go mod tidy -v

## audit: run quality control checks
.PHONY: audit
audit:
go mod verify
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
go test -race -buildvcs -vet=off ./...


# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #

.PHONY: no-dirty
no-dirty:
git diff --exit-code

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

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

## test: run all tests
.PHONY: test
test:
@go test -v ./...

## nix: build binary file with nix
.PHONY: nix
nix:
@nix-shell --show-trace

# ==================================================================================== #
# RUN COMMANDS OF CLI WITH DEFAULT ARGS
# ==================================================================================== #

.PHONY: lint
lint:
Expand All @@ -17,9 +71,22 @@ lint:
mermaid:
@go run ./cmd/s6cli $(ARGS) mermaid

.PHONY: test
test:
@go test -v ./...
.PHONY: create-oneshot
create-oneshot:
go run ./cmd/s6cli $(ARGS) create o test

.PHONY: create-longrun
create-longrun:
go run ./cmd/s6cli $(ARGS) create l test

.PHONY: create-bundle
create-bundle:
go run ./cmd/s6cli $(ARGS) create b test

.PHONY: remove
remove:
go run ./cmd/s6cli $(ARGS) remove test

.PHONY: re-create
re-create: remove create-oneshot

nix:
@nix-shell --show-trace
89 changes: 52 additions & 37 deletions cmd/s6cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"fmt"
"github.com/dazz/s6-cli/internal/domain/create"
"github.com/dazz/s6-cli/internal/domain/lint"
"github.com/dazz/s6-cli/internal/domain/mermaid"
"github.com/dazz/s6-cli/internal/infrastructure/persistence"
"github.com/dazz/s6-cli/internal/domain/remove"
"github.com/dazz/s6-cli/internal/domain/service"
"github.com/dazz/s6-cli/internal/infrastructure/persistence/filesystem"
"log"
"os"
"time"

"github.com/urfave/cli/v2"

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

func main() {
Expand Down Expand Up @@ -47,10 +48,10 @@ func main() {
rootPath = cCtx.String("rootPath")
}

repo := persistence.NewFilesystem(rootPath)
action := lint.NewAction(repo)
repo := filesystem.NewFilesystem(rootPath)
command := lint.NewCommand(repo)

fmt.Println(action.Output())
fmt.Println(command.Execute())

return nil
},
Expand All @@ -64,10 +65,10 @@ func main() {
rootPath = cCtx.String("rootPath")
}

repo := persistence.NewFilesystem(rootPath)
action := mermaid.NewAction(repo)
repo := filesystem.NewFilesystem(rootPath)
command := mermaid.NewCommand(repo)

fmt.Println(action.Output())
fmt.Println(command.Execute())

return nil
},
Expand All @@ -76,42 +77,48 @@ func main() {
Name: "create",
Aliases: []string{"c"},
Usage: "create a service",
ArgsUsage: "[type: (o|l|b)] [name]",
ArgsUsage: "[type: (o|l|b)] [id]",
Flags: []cli.Flag{
&cli.BoolFlag{Value: false, Name: "overwrite", Aliases: []string{"o"}, Usage: "Ignore existing files and directories"},
},
Action: func(cCtx *cli.Context) error {
path := "/etc/s6-overlay/s6-rc.d"
if cCtx.IsSet("path") {
path = cCtx.String("path")
rootPath := "/etc/s6-overlay/s6-rc.d"
if cCtx.IsSet("rootPath") {
rootPath = cCtx.String("rootPath")
}
// check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", path)
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", rootPath)
os.Exit(1)
}

serviceType := ""
if cCtx.Args().Get(0) == "o" || cCtx.Args().Get(0) == "l" || cCtx.Args().Get(0) == "b" {
serviceType = cCtx.Args().Get(0)
} else {
var serviceType service.Type
switch t := cCtx.Args().Get(0); t {
case "o":
serviceType = service.TypeOneshot
case "l":
serviceType = service.TypeLongrun
case "b":
serviceType = service.TypeBundle
default:
fmt.Print("Arg type must not be empty and one of 'o', 'l' or 'b'\n")
os.Exit(1)
}

name := ""
if cCtx.Args().Get(1) != "" {
name = cCtx.Args().Get(1)
var id service.Id
if idArg := cCtx.Args().Get(1); idArg != "" {
id = service.Id(idArg)
} else {
fmt.Print("Arg name must not be empty\n")
fmt.Println("Arg idArg must not be empty")
os.Exit(1)
}

fmt.Printf("Create a service %s with type %s\n", name, serviceType)
fmt.Printf("Create a service %s with type %s\n", id, serviceType)

if serviceType == "o" {
s6cli.Oneshot(path, name, []string{"base"})
}
repo := filesystem.NewFilesystem(rootPath)
command := create.NewCommand(repo, id, serviceType)

fmt.Println(command.Execute())

return nil
},
Expand All @@ -122,25 +129,33 @@ func main() {
Usage: "remove a service",
ArgsUsage: "[name]",
Action: func(cCtx *cli.Context) error {
path := "/etc/s6-overlay/s6-rc.d"
if cCtx.IsSet("path") {
path = cCtx.String("path")
rootPath := "/etc/s6-overlay/s6-rc.d"
if cCtx.IsSet("rootPath") {
rootPath = cCtx.String("rootPath")
}
// check if the directory exists
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist\n", path)
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
fmt.Println("Directory does not exist: " + rootPath)
os.Exit(1)
}

name := ""
if cCtx.Args().Get(0) != "" {
name = cCtx.Args().Get(0)
var id service.Id
if idArg := cCtx.Args().Get(0); idArg != "" {
id = service.Id(idArg)
} else {
fmt.Print("Arg name must not be empty\n")
fmt.Println("Arg idArg must not be empty")
os.Exit(1)
}

s6cli.Remove(path, name)
repo := filesystem.NewFilesystem(rootPath)
command := remove.NewCommand(repo, id)

removed, err := command.Execute()
if err == nil {
fmt.Println("Successful removed service " + removed)
} else {

}

return nil
},
Expand Down

0 comments on commit d36526c

Please sign in to comment.