From 3a00b9504d50d71ef6eb4c62f09ce5a7913a34df Mon Sep 17 00:00:00 2001 From: veradec Date: Fri, 13 Mar 2026 21:56:45 +0530 Subject: [PATCH] init: init --- .gitignore | 1 + go.mod | 5 +++ go.sum | 2 ++ helpers/fileio/_copy.go | 13 +++++++ helpers/fileio/_move.go | 13 +++++++ helpers/fileio/file.go | 75 +++++++++++++++++++++++++++++++++++++++ helpers/identity/group.go | 13 +++++++ helpers/identity/user.go | 13 +++++++ helpers/pkg/pacman.go | 57 +++++++++++++++++++++++++++++ main.go | 43 ++++++++++++++++++++++ test.huml | 10 ++++++ 11 files changed, 245 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 helpers/fileio/_copy.go create mode 100644 helpers/fileio/_move.go create mode 100644 helpers/fileio/file.go create mode 100644 helpers/identity/group.go create mode 100644 helpers/identity/user.go create mode 100644 helpers/pkg/pacman.go create mode 100644 main.go create mode 100644 test.huml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..734d213 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/veradec/ensemble + +go 1.25.0 + +require github.com/huml-lang/go-huml v0.3.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4020ab8 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/huml-lang/go-huml v0.3.0 h1:73QiKH2Pvt/pdlXSYM3niKnMjLcvHaroVwHhRxDh008= +github.com/huml-lang/go-huml v0.3.0/go.mod h1:13bzEPhjk4jq3E7H/wTPkIRuXtfoTDT4xPuXfMQ8sJc= diff --git a/helpers/fileio/_copy.go b/helpers/fileio/_copy.go new file mode 100644 index 0000000..0dc4f8e --- /dev/null +++ b/helpers/fileio/_copy.go @@ -0,0 +1,13 @@ +package main +import "fmt" +func _copy(m interface{}) error { + + data, ok := m.(map[string]interface{}) + if !ok { + return fmt.Errorf("expected map[string]interface{}, got %T", m) + } + + fmt.Printf("[COPY] src: %s - dest: %s\n", data["src"], data["dest"]) + + return nil +} diff --git a/helpers/fileio/_move.go b/helpers/fileio/_move.go new file mode 100644 index 0000000..cc2f1a6 --- /dev/null +++ b/helpers/fileio/_move.go @@ -0,0 +1,13 @@ +package main +import "fmt" +func _move(m interface{}) error { + + data, ok := m.(map[string]interface{}) + if !ok { + return fmt.Errorf("expected map[string]interface{}, got %T", m) + } + + fmt.Printf("[COPY] src: %s - dest: %s\n", data["src"], data["dest"]) + + return nil +} diff --git a/helpers/fileio/file.go b/helpers/fileio/file.go new file mode 100644 index 0000000..44c3e88 --- /dev/null +++ b/helpers/fileio/file.go @@ -0,0 +1,75 @@ +package main + +import ("fmt" + "os" +) + +func checkFileExists(path string) bool { + _, err := os.Open(path) + if err != nil { + return false + } + return true +} + +func createFile(path string) bool { + f, err := os.Create(path) + if err != nil { + return false; + } + defer f.Close() + fmt.Printf("[FILE] created `%s`\n", f.Name()) + return true; +} + +func removeFile(path string) bool { + err := os.Remove(path) + if err != nil { + // panic(err) + return false; + } + fmt.Printf("[FILE] removed `%s`\n", path) + return true +} + +func file(m interface{}) error { + data, ok := m.(map[string]interface{}) + if !ok { + return fmt.Errorf("expected map[string]interface{}, got %T", m) + } + shouldPresent := false + + if data["state"] == "absent" { + shouldPresent = false + } else if data["state"] == "present"{ + shouldPresent = true + } + + + path := data["path"] + + + if checkFileExists(path.(string)){ + if shouldPresent { + // Skip + fmt.Printf("[FILE] %s exists SKIPPING\n", path) + + } else { + // Remove File + removeFile(path.(string)) + } + } else { + if shouldPresent { + // Create File + createFile(path.(string)) + } else { + // Skip + fmt.Printf("[FILE] %s doesn't exist SKIPPING\n", path) + + } + } + + + return nil + +} diff --git a/helpers/identity/group.go b/helpers/identity/group.go new file mode 100644 index 0000000..a6301e7 --- /dev/null +++ b/helpers/identity/group.go @@ -0,0 +1,13 @@ +package main +import "fmt" +func group(m interface{}) error { + + data, ok := m.(map[string]interface{}) + if !ok { + return fmt.Errorf("expected map[string]interface{}, got %T", m) + } + + fmt.Printf("[GROUP] name : %s - state: %s\n", data["name"], data["state"]) + + return nil +} diff --git a/helpers/identity/user.go b/helpers/identity/user.go new file mode 100644 index 0000000..594738e --- /dev/null +++ b/helpers/identity/user.go @@ -0,0 +1,13 @@ +package main +import "fmt" +func user(m interface{}) error { + + data, ok := m.(map[string]interface{}) + if !ok { + return fmt.Errorf("expected map[string]interface{}, got %T", m) + } + + fmt.Printf("[USER] name: %s - state: %s - gui: %v\n", data["name"], data["state"], data["uid"]) + + return nil +} diff --git a/helpers/pkg/pacman.go b/helpers/pkg/pacman.go new file mode 100644 index 0000000..a3f69fa --- /dev/null +++ b/helpers/pkg/pacman.go @@ -0,0 +1,57 @@ +package main +import ( + "fmt" + "os/exec" +) + +func checkPackageExists(pac string) bool { + command := fmt.Sprintf("pacman -Qqe %s", pac) + cmd:= exec.Command(command) + err:=cmd.Run() + + if err != nil { + return false; + } + return true; +} + +func installPackage(pac string) { + fmt.Printf("[PACMAN] Installing `%s`\n", pac) +} + +func removePackage(pac string){ + fmt.Printf("[PACMAN] Removing `%s`\n", pac) +} + + +func pacman(m interface{}) error { + data, ok := m.(map[string]interface{}) + if !ok { + return fmt.Errorf("expected map[string]interface{}, got %T", m) + } + shouldPresent := false + + if data["state"] == "present" { + shouldPresent = true + } else if data["state"] == "absent"{ + shouldPresent = false + } + + pac := data["name"] + + if checkPackageExists(pac.(string)) { + if shouldPresent { + fmt.Printf("[PACMAN] `%s` Exists Skipping\n", pac) + } else { + removePackage(pac.(string)) + } + } else { + if shouldPresent { + installPackage(pac.(string)) + } else { + fmt.Printf("[PACMAN] `%s` doesn't exist skipping\n", pac) + } + + } + return nil +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..1674df2 --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "github.com/huml-lang/go-huml" + "io/ioutil" + "log" + +) + +import "helpers" + +func main() { + // Read the content from file + data, err := ioutil.ReadFile("test.huml") + if err != nil { + log.Fatal(err) + } else { + fmt.Println("[IO] File Found") + } + + // Test Huml out + var result map[string]any + if err := huml.Unmarshal([]byte(data), &result); err != nil { + panic(err) + } + + // Iterate over Tasks + if tasks, ok := result["tasks"].(map[string]interface{}); ok { + for key, value := range tasks { + switch key { + case "pacman": + pkg.pacman(value) + case "file": + fileio.file(value) + // case "user": + // user(value) + // case "group": + // group(value) + } + } + } +} diff --git a/test.huml b/test.huml new file mode 100644 index 0000000..ff9bb09 --- /dev/null +++ b/test.huml @@ -0,0 +1,10 @@ +name: "Lewis's Dictionary" +hosts: "all" +tasks:: + pacman:: + name: "neovim" + state: "absent" + file:: + path: "/home/san/test.c" + state: "absent" + \ No newline at end of file