Skip to content

Commit

Permalink
feat: extended external communication via dbus #51
Browse files Browse the repository at this point in the history
  • Loading branch information
leukipp committed Apr 12, 2024
1 parent 8720467 commit 26ff3a0
Show file tree
Hide file tree
Showing 21 changed files with 1,572 additions and 685 deletions.
114 changes: 66 additions & 48 deletions common/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@ import (
"flag"
"fmt"
"os"
"path"
"strings"

"net/http"
"path/filepath"
)

var (
Build BuildInfo // Build information
Args Arguments // Parsed arguments
Args Arguments // Parsed arguments
)

type BuildInfo struct {
Name string // Build name
Version string // Build version
Commit string // Build commit
Date string // Build date
Source string // Build source
Latest string // Build latest
Summary string // Build summary
}

type Arguments struct {
Cache string // Argument for cache folder path
Config string // Argument for config file path
Expand All @@ -35,26 +21,21 @@ type Arguments struct {
VVV bool // Argument for very very verbose mode
VV bool // Argument for very verbose mode
V bool // Argument for verbose mode
Dbus struct {
Listen bool // Argument for dbus listen flag
Method string // Argument for dbus method name
Property string // Argument for dbus property name
Args []string // Argument for dbus method arguments
}
}

func InitArgs(name, version, commit, date, source string) {

// Build information
Build = BuildInfo{
Name: name,
Version: version,
Commit: Truncate(commit, 7),
Date: date,
Source: source,
Latest: version,
}
Build.Summary = fmt.Sprintf("%s v%s-%s, built on %s", Build.Name, Build.Version, Build.Commit, Build.Date)
func InitArgs(introspect map[string][]string) {

// Command line arguments
flag.StringVar(&Args.Cache, "cache", filepath.Join(CacheFolderPath(Build.Name), Build.Version), "cache folder path")
flag.StringVar(&Args.Config, "config", filepath.Join(ConfigFolderPath(Build.Name), "config.toml"), "config file path")
flag.StringVar(&Args.Lock, "lock", filepath.Join(os.TempDir(), fmt.Sprintf("%s.lock", Build.Name)), "lock file path")
flag.StringVar(&Args.Sock, "sock", filepath.Join(os.TempDir(), fmt.Sprintf("%s.sock", Build.Name)), "sock file path")
flag.StringVar(&Args.Sock, "sock", filepath.Join(os.TempDir(), fmt.Sprintf("%s.sock", Build.Name)), "sock file path (deprecated)")
flag.StringVar(&Args.Log, "log", filepath.Join(os.TempDir(), fmt.Sprintf("%s.log", Build.Name)), "log file path")
flag.BoolVar(&Args.VVV, "vvv", false, "very very verbose mode")
flag.BoolVar(&Args.VV, "vv", false, "very verbose mode")
Expand All @@ -67,32 +48,69 @@ func InitArgs(name, version, commit, date, source string) {
}
flag.Parse()

// Version checker
suspended := false
if _, err := os.Stat(filepath.Join(Args.Cache, "no-version-check")); !os.IsNotExist(err) {
suspended = true
}
if !suspended && VersionToInt(Build.Version) > 0 {
Build.Latest = Latest(source)
if VersionToInt(Build.Latest) > VersionToInt(Build.Version) {
Build.Summary = fmt.Sprintf("%s, >>> %s v%s available <<<", Build.Summary, Build.Name, Build.Latest)
// Subcommand line arguments
dbus := flag.NewFlagSet("dbus", flag.ExitOnError)
dbus.BoolVar(&Args.Dbus.Listen, "listen", false, "dbus listen mode")
dbus.StringVar(&Args.Dbus.Method, "method", "", "dbus method caller")
dbus.StringVar(&Args.Dbus.Property, "property", "", "dbus property reader")
Args.Dbus.Args = []string{}

// Subcommand line usage text
if len(os.Args) > 1 {
switch os.Args[1] {
case "dbus":
dbus.Usage = func() {
fmt.Fprintf(dbus.Output(), "%s\n\nUsage:\n", Build.Summary)
dbus.PrintDefaults()

if len(introspect) > 0 {
if methods, ok := introspect["Methods"]; ok {
fmt.Fprintf(dbus.Output(), "\nMethods:\n")
for _, method := range methods {
fmt.Fprintf(dbus.Output(), " %s dbus -method %s\n", Build.Name, method)
}
}
if properties, ok := introspect["Properties"]; ok {
fmt.Fprintf(dbus.Output(), "\nProperties:\n")
for _, property := range properties {
fmt.Fprintf(dbus.Output(), " %s dbus -property %s\n", Build.Name, property)
}
}
} else {
fmt.Fprintf(dbus.Output(), "\n>>> start %s to see further information's <<<\n", Build.Name)
}
}
Args.Dbus.Args = ParseArgs(dbus, os.Args[2:])
}

// Check number of arguments
if flag.NArg() == 1 {
dbus.Usage()
os.Exit(1)
}
}
}

func Latest(source string) string {
func ParseArgs(flags *flag.FlagSet, args []string) []string {
pargs := []string{}

// Request latest version from github
res, err := http.Get(source + "/releases/latest")
if err != nil {
return Build.Version
}
for {
// Parse named arguments
flags.Parse(args)

// Parse latest version from redirect url
version := path.Base(res.Request.URL.Path)
if !strings.HasPrefix(version, "v") {
return Build.Version
// Check named arguments
args = args[len(args)-flags.NArg():]
if len(args) == 0 {
break
}

// Check positional arguments
pargs = append(pargs, args[0])
args = args[1:]
}

return version[1:]
// Parse positional arguments
flags.Parse(pargs)

return flags.Args()
}
91 changes: 91 additions & 0 deletions common/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package common

import (
"fmt"
"os"
"path"
"runtime"
"strings"

"net/http"
"path/filepath"
)

var (
Process ProcessInfo // Process information
Build BuildInfo // Build information
)

type ProcessInfo struct {
Id int // Process id
Path string // Process path
Host string // Process host
System string // Process system
}

type BuildInfo struct {
Name string // Build name
Version string // Build version
Commit string // Build commit
Date string // Build date
Source string // Build source
Latest string // Build latest
Summary string // Build summary
}

func InitInfo(name, version, commit, date, source string) {

// Process information
Process = ProcessInfo{
Id: os.Getpid(),
System: fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH),
}
Process.Host, _ = os.Hostname()
Process.Path, _ = os.Executable()

// Build information
Build = BuildInfo{
Name: name,
Version: version,
Commit: TruncateString(commit, 7),
Date: date,
Source: source,
Latest: version,
}
Build.Summary = fmt.Sprintf("%s v%s-%s, built on %s", Build.Name, Build.Version, Build.Commit, Build.Date)

// Check latest version
if !Feature("disable-version-check") && VersionToInt(Build.Version) > 0 {
Build.Latest = Latest(source)
if VersionToInt(Build.Latest) > VersionToInt(Build.Version) {
Build.Summary = fmt.Sprintf("%s, >>> %s v%s available <<<", Build.Summary, Build.Name, Build.Latest)
}
}
}

func Latest(source string) string {

// Request latest version from github
res, err := http.Get(source + "/releases/latest")
if err != nil {
return Build.Version
}

// Parse latest version from redirect url
version := path.Base(res.Request.URL.Path)
if !strings.HasPrefix(version, "v") {
return Build.Version
}

return version[1:]
}

func Feature(name string) bool {
file := filepath.Join(Args.Cache, name)

// Check if feature file exists
if _, err := os.Stat(file); !os.IsNotExist(err) {
return true
}
return false
}
81 changes: 62 additions & 19 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,94 @@
package common

import (
"reflect"
"regexp"
"strconv"
"strings"

"crypto/sha1"
"encoding/hex"

"github.com/jezek/xgb/render"

"github.com/jezek/xgbutil/xrect"
)

func Hash(text string) string {
type Point struct {
X int // Object point x position
Y int // Object point y position
}

func CreatePoint(x int, y int) *Point {
return &Point{
X: x,
Y: y,
}
}

type Geometry struct {
X int // Object geometry x position
Y int // Object geometry y position
Width int // Object geometry width dimension
Height int // Object geometry height dimension
}

func CreateGeometry(r xrect.Rect) *Geometry {
return &Geometry{
X: r.X(),
Y: r.Y(),
Width: r.Width(),
Height: r.Height(),
}
}

func (g *Geometry) Center() Point {
return *CreatePoint(g.X+g.Width/2, g.Y+g.Height/2)
}

func (g *Geometry) Rect() xrect.Rect {
return xrect.New(g.X, g.Y, g.Width, g.Height)
}

func (g *Geometry) Pieces() (int, int, int, int) {
return g.X, g.Y, g.Width, g.Height
}

type Map = map[string]interface{} // Generic map type

func HashString(text string) string {
hash := sha1.New()
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}

func Truncate(s string, max int) string {
func TruncateString(s string, max int) string {
if max > len(s) {
return s
}
return s[:max]
}

func IsType(a interface{}, b interface{}) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}

func IsZero(items []uint) bool {
func AllZero(items []uint) bool {
mask := uint(0)
for _, s := range items {
mask |= s
for _, item := range items {
mask |= item
}
return mask == 0
}

func AllTrue(items []bool) bool {
mask := true
for _, item := range items {
mask = mask && item
}
return mask
}

func IsInsideRect(p Point, g Geometry) bool {
x, y, w, h := g.Pieces()
xInRect := int(p.X) >= x && int(p.X) <= (x+w)
yInRect := int(p.Y) >= y && int(p.Y) <= (y+h)
return xInRect && yInRect
}

func IsInList(item string, items []string) bool {
for i := 0; i < len(items); i++ {
if items[i] == item {
Expand All @@ -48,13 +98,6 @@ func IsInList(item string, items []string) bool {
return false
}

func IsInsideRect(p render.Pointfix, r xrect.Rect) bool {
x, y, w, h := r.Pieces()
xInRect := int(p.X) >= x && int(p.X) <= (x+w)
yInRect := int(p.Y) >= y && int(p.Y) <= (y+h)
return xInRect && yInRect
}

func ReverseList[T any](items []T) []T {
for i, j := 0, len(items)-1; i < j; {
items[i], items[j] = items[j], items[i]
Expand Down
Loading

0 comments on commit 26ff3a0

Please sign in to comment.