feat: initial commit

This commit is contained in:
2024-11-29 00:40:42 +02:00
commit ad022ef144
16 changed files with 334 additions and 0 deletions

14
.editorconfig Normal file
View File

@@ -0,0 +1,14 @@
[*]
tab_width = 2
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[Makefile]
tab_width = 4
indent_size = 4
indent_style = tab

11
Makefile Normal file
View File

@@ -0,0 +1,11 @@
.PHONY: pklgen
pklgen:
rm -rf appconfig/
pkl-gen-go pkl/AppConfig.pkl
.PHONY: build
build: pklgen
go build
run:
./sofmani

View File

@@ -0,0 +1,39 @@
// Code generated from Pkl module `dev.casraf.sofmani.AppConfig`. DO NOT EDIT.
package appconfig
import (
"context"
"github.com/apple/pkl-go/pkl"
)
type AppConfig struct {
Debug any `pkl:"debug"`
Install []*Installer `pkl:"install"`
}
// LoadFromPath loads the pkl module at the given path and evaluates it into a AppConfig
func LoadFromPath(ctx context.Context, path string) (ret *AppConfig, err error) {
evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions)
if err != nil {
return nil, err
}
defer func() {
cerr := evaluator.Close()
if err == nil {
err = cerr
}
}()
ret, err = Load(ctx, evaluator, pkl.FileSource(path))
return ret, err
}
// Load loads the pkl module at the given source and evaluates it with the given evaluator into a AppConfig
func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*AppConfig, error) {
var ret AppConfig
if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil {
return nil, err
}
return &ret, nil
}

View File

@@ -0,0 +1,18 @@
// Code generated from Pkl module `dev.casraf.sofmani.AppConfig`. DO NOT EDIT.
package appconfig
import "github.com/chenasraf/sofmani/appconfig/installertype"
type Installer struct {
Name string `pkl:"name"`
Type installertype.InstallerType `pkl:"type"`
Platforms *Platforms `pkl:"platforms"`
Url *string `pkl:"url"`
Command *string `pkl:"command"`
Steps *[]*Installer `pkl:"steps"`
}

View File

@@ -0,0 +1,10 @@
// Code generated from Pkl module `dev.casraf.sofmani.AppConfig`. DO NOT EDIT.
package appconfig
import "github.com/chenasraf/sofmani/appconfig/platform"
type Platforms struct {
Only []platform.Platform `pkl:"only"`
Except []platform.Platform `pkl:"except"`
}

10
appconfig/init.pkl.go Normal file
View File

@@ -0,0 +1,10 @@
// Code generated from Pkl module `dev.casraf.sofmani.AppConfig`. DO NOT EDIT.
package appconfig
import "github.com/apple/pkl-go/pkl"
func init() {
pkl.RegisterMapping("dev.casraf.sofmani.AppConfig", AppConfig{})
pkl.RegisterMapping("dev.casraf.sofmani.AppConfig#Installer", Installer{})
pkl.RegisterMapping("dev.casraf.sofmani.AppConfig#Platforms", Platforms{})
}

View File

@@ -0,0 +1,43 @@
// Code generated from Pkl module `dev.casraf.sofmani.AppConfig`. DO NOT EDIT.
package installertype
import (
"encoding"
"fmt"
)
type InstallerType string
const (
Custom InstallerType = "custom"
Brew InstallerType = "brew"
Apt InstallerType = "apt"
Git InstallerType = "git"
Unzip InstallerType = "unzip"
)
// String returns the string representation of InstallerType
func (rcv InstallerType) String() string {
return string(rcv)
}
var _ encoding.BinaryUnmarshaler = new(InstallerType)
// UnmarshalBinary implements encoding.BinaryUnmarshaler for InstallerType.
func (rcv *InstallerType) UnmarshalBinary(data []byte) error {
switch str := string(data); str {
case "custom":
*rcv = Custom
case "brew":
*rcv = Brew
case "apt":
*rcv = Apt
case "git":
*rcv = Git
case "unzip":
*rcv = Unzip
default:
return fmt.Errorf(`illegal: "%s" is not a valid InstallerType`, str)
}
return nil
}

View File

@@ -0,0 +1,37 @@
// Code generated from Pkl module `dev.casraf.sofmani.AppConfig`. DO NOT EDIT.
package platform
import (
"encoding"
"fmt"
)
type Platform string
const (
Macos Platform = "macos"
Linux Platform = "linux"
Windows Platform = "windows"
)
// String returns the string representation of Platform
func (rcv Platform) String() string {
return string(rcv)
}
var _ encoding.BinaryUnmarshaler = new(Platform)
// UnmarshalBinary implements encoding.BinaryUnmarshaler for Platform.
func (rcv *Platform) UnmarshalBinary(data []byte) error {
switch str := string(data); str {
case "macos":
*rcv = Macos
case "linux":
*rcv = Linux
case "windows":
*rcv = Windows
default:
return fmt.Errorf(`illegal: "%s" is not a valid Platform`, str)
}
return nil
}

23
config.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"context"
"fmt"
"os"
"github.com/chenasraf/sofmani/appconfig"
)
func LoadConfig() (*appconfig.AppConfig, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
path := fmt.Sprintf("%s/%s", wd, "sofmani.pkl")
fmt.Println(fmt.Sprintf("Loading config from path: %s", path))
cfg, err := appconfig.LoadFromPath(context.Background(), path)
if err != nil {
return nil, err
}
return cfg, nil
}

10
go.mod Normal file
View File

@@ -0,0 +1,10 @@
module github.com/chenasraf/sofmani
go 1.23.0
require github.com/apple/pkl-go v0.8.1
require (
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
)

14
go.sum Normal file
View File

@@ -0,0 +1,14 @@
github.com/apple/pkl-go v0.8.1 h1:C7Z2MJAJX8bVPmk4ZL8I7tpMnuw+tLcwQ6jeR7/BYXs=
github.com/apple/pkl-go v0.8.1/go.mod h1:5Hwil5tyZGrOekh7JXLZJvIAcGHb4gT19lnv4WEiKeI=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

33
installer/brew.go Normal file
View File

@@ -0,0 +1,33 @@
package installer
import (
"bufio"
"fmt"
"os/exec"
"github.com/chenasraf/sofmani/appconfig"
)
type BrewInstaller struct {
Config *appconfig.AppConfig
Installer *appconfig.Installer
}
func (i *BrewInstaller) Install() error {
cmd := exec.Command("brew", "install", i.Installer.Name)
stderr, _ := cmd.StderrPipe()
cmd.Start()
scanner := bufio.NewScanner(stderr)
scanner.Split(bufio.ScanRunes)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
return nil
}
func NewBrewInstaller(cfg *appconfig.AppConfig, installer *appconfig.Installer) *BrewInstaller {
return &BrewInstaller{
Config: cfg,
Installer: installer,
}
}

18
installer/installer.go Normal file
View File

@@ -0,0 +1,18 @@
package installer
import (
"github.com/chenasraf/sofmani/appconfig"
)
type InstallerImpl struct {
Config *appconfig.AppConfig
}
type IInstaller interface {
Install() error
}
func RunInstaller(installer IInstaller) error {
installer.Install()
return nil
}

19
main.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"fmt"
"github.com/chenasraf/sofmani/installer"
)
func main() {
cfg, err := LoadConfig()
if err != nil {
fmt.Println(fmt.Errorf("Error loading config: %v", err))
return
}
for _, i := range cfg.Install {
installer.RunInstaller(installer.NewBrewInstaller(cfg, i))
}
}

27
pkl/AppConfig.pkl Normal file
View File

@@ -0,0 +1,27 @@
@go.Package { name = "github.com/chenasraf/sofmani/appconfig" }
module dev.casraf.sofmani.AppConfig
import "package://pkg.pkl-lang.org/pkl-go/pkl.golang@0.8.0#/go.pkl"
typealias InstallerType = "custom"|"brew"|"apt"|"git"|"unzip"
class Installer {
name: String
type: InstallerType
platforms: Platforms?
url: String?
command: String?
steps: Listing<Installer>?
}
typealias Platform = "macos"|"linux"|"windows"
class Platforms {
only: Listing<Platform>
except: Listing<Platform>
}
debug = false
install: Listing<Installer>

8
sofmani.pkl Normal file
View File

@@ -0,0 +1,8 @@
debug = true
install {
new {
name = "treelike"
type = "brew"
url = "chenasraf/tap/treelike"
}
}