-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
305 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# s6-cli: A cli for s6-overlay | ||
|
||
todo | ||
* write tests with https://github.com/stretchr/testify | ||
* style output with color https://github.com/charmbracelet/glamour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/command/execlineb -P | ||
|
||
echo "Hello, world!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
longrun |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lint | ||
|
||
import ( | ||
"fmt" | ||
"github.com/dazz/s6-cli/internal/domain/service" | ||
"log" | ||
) | ||
|
||
type Action struct { | ||
repository service.Repository | ||
} | ||
|
||
func NewAction(repository service.Repository) *Action { | ||
return &Action{ | ||
repository: repository, | ||
} | ||
} | ||
|
||
func (a *Action) Lint() bool { | ||
// do all the fun stuff here | ||
services, err := a.repository.All() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
for _, s := range services { | ||
fmt.Println("* " + s.Id) | ||
for _, l := range s.Lints { | ||
fmt.Println(" * " + l) | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package service | ||
|
||
type Repository interface { | ||
All() ([]*Service, error) | ||
//Create(service Service) error | ||
//Remove(id Id) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package service | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
const ( | ||
TypeOneshot Type = "oneshot" | ||
TypeLongrun Type = "longrun" | ||
TypeBundle Type = "bundle" | ||
) | ||
|
||
type Id string | ||
type Type string | ||
type Lint string | ||
|
||
type Service struct { | ||
Id Id | ||
Type Type | ||
Dependencies []Id | ||
Lints []Lint | ||
Valid bool | ||
Path string | ||
} | ||
|
||
func NewService(id Id, rootPath string) *Service { | ||
return &Service{ | ||
Id: id, | ||
Path: rootPath + "/" + string(id), | ||
Valid: true, | ||
Lints: []Lint{}, | ||
} | ||
} | ||
|
||
func (s *Service) AddLint(lint string) { | ||
s.Lints = append(s.Lints, Lint(lint)) | ||
s.Valid = false | ||
} | ||
|
||
func (s *Service) DependencyDir() (string, error) { | ||
if s.Type == TypeBundle { | ||
return s.Path + "/contents.d", nil | ||
} | ||
if s.Type == TypeOneshot || s.Type == TypeLongrun { | ||
return s.Path + "/dependencies.d", nil | ||
} | ||
return "", errors.New("invalid service type") | ||
} | ||
|
||
func (s *Service) AddDependency(dependency string) { | ||
s.Dependencies = append(s.Dependencies, Id(dependency)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package persistence | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/dazz/s6-cli/internal/domain/service" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const RootService service.Id = "user" | ||
|
||
type Filesystem struct { | ||
rootPath string | ||
byId map[service.Id]*service.Service | ||
allIds []service.Id | ||
} | ||
|
||
func NewFilesystem(rootPath string) *Filesystem { | ||
return &Filesystem{ | ||
rootPath: rootPath, | ||
byId: make(map[service.Id]*service.Service), | ||
allIds: []service.Id{}, | ||
} | ||
} | ||
|
||
func (fs *Filesystem) All() ([]*service.Service, error) { | ||
err := fs.compile(RootService) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
var services []*service.Service | ||
for _, id := range fs.allIds { | ||
services = append(services, fs.byId[id]) | ||
//fmt.Println(id) | ||
} | ||
return services, nil | ||
} | ||
|
||
func (fs *Filesystem) contains(id service.Id) bool { | ||
_, ok := fs.byId[id] | ||
if ok { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// compile all folders and directories to a list of services we can work with | ||
func (fs *Filesystem) compile(id service.Id) error { | ||
if id == "" { | ||
id = RootService | ||
} | ||
|
||
// Check if the service is already in the services list | ||
if fs.contains(id) { | ||
return nil | ||
} | ||
|
||
s := service.NewService(id, fs.rootPath) | ||
|
||
// add the service to the services list | ||
fs.byId[id] = s | ||
fs.allIds = append(fs.allIds, id) | ||
|
||
// Check if the directory exists | ||
if _, err := os.Stat(s.Path); os.IsNotExist(err) { | ||
s.AddLint(fmt.Sprintf("invalid directory: path %s does not exist", s.Path)) | ||
return nil | ||
} | ||
|
||
serviceType, err := fs.serviceType(s) | ||
if err != nil { | ||
s.AddLint(err.Error()) | ||
return nil | ||
} | ||
s.Type = serviceType | ||
|
||
// check if the run file exists | ||
if serviceType == service.TypeLongrun { | ||
runFile := s.Path + "/run" | ||
if _, err := os.Stat(runFile); os.IsNotExist(err) { | ||
s.AddLint("run file for longrun does not exist") | ||
} | ||
} | ||
|
||
// check if the up file exists | ||
if serviceType == "oneshot" { | ||
upFile := s.Path + "/up" | ||
if _, err := os.Stat(upFile); os.IsNotExist(err) { | ||
s.AddLint("up file for oneshot does not exist") | ||
} | ||
} | ||
|
||
// check if the dependency directory exists | ||
dependencyDir, err := s.DependencyDir() | ||
if err != nil { | ||
s.AddLint(fmt.Sprintf("service type (%s) in type file for %s does not exist", serviceType, id)) | ||
} | ||
|
||
// get the dependencies | ||
files, err := os.ReadDir(dependencyDir) | ||
if err != nil { | ||
s.AddLint("service has no dependency directory") | ||
return nil | ||
} | ||
for _, file := range files { | ||
if file.Name() == "base" { | ||
// we don't want to check the base directory | ||
continue | ||
} | ||
s.AddDependency(file.Name()) | ||
} | ||
|
||
for _, dependency := range s.Dependencies { | ||
if fs.contains(dependency) || dependency == "base" { | ||
continue | ||
} | ||
// recursive call | ||
err := fs.compile(dependency) | ||
if err != nil { | ||
s.AddLint(fmt.Sprintf("dependency error: %s", err)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (fs *Filesystem) serviceType(s *service.Service) (service.Type, error) { | ||
// check the type file and content | ||
typeFileContent, err := os.ReadFile(s.Path + "/type") | ||
if err != nil { | ||
s.AddLint(fmt.Sprintf("type file for \"%s\" does not exist", s.Id)) | ||
return "", err | ||
} | ||
// Check if the file is empty | ||
if len(typeFileContent) == 0 { | ||
s.AddLint(fmt.Sprintf("type file for \"%s\" is empty", s.Id)) | ||
return "", err | ||
} | ||
|
||
// Check if the last character is a newline | ||
lastChar := typeFileContent[len(typeFileContent)-1] | ||
if lastChar != '\n' { | ||
s.AddLint(fmt.Sprintf("type file for \"%s\" does not end with a newline", s.Id)) | ||
} | ||
|
||
serviceType := service.Type(strings.ReplaceAll(string(typeFileContent), "\n", "")) | ||
for _, theType := range []service.Type{service.TypeOneshot, service.TypeLongrun, service.TypeBundle} { | ||
if serviceType == theType { | ||
return serviceType, nil | ||
} | ||
} | ||
|
||
return "", errors.New(fmt.Sprintf("invalid type in %s/type file specified", s.Id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters