From a8534a5abc932ee3d6464975506a5a1912b96f82 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 21 Jan 2025 09:00:15 +0200 Subject: [PATCH] begin working on entry point --- go.mod | 5 +-- host/lux_host_entry.go | 88 ------------------------------------------ main.go | 45 ++++++++++++++------- 3 files changed, 33 insertions(+), 105 deletions(-) delete mode 100644 host/lux_host_entry.go diff --git a/go.mod b/go.mod index ebd3420..d77dc34 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module lux go 1.23.4 require ( - github.com/google/uuid v1.6.0 // indirect - github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect - gopkg.in/ini.v1 v1.67.0 // indirect + github.com/google/uuid v1.6.0 + github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 ) diff --git a/host/lux_host_entry.go b/host/lux_host_entry.go deleted file mode 100644 index bd1bb41..0000000 --- a/host/lux_host_entry.go +++ /dev/null @@ -1,88 +0,0 @@ -package host - -import ( - "fmt" - "log" - "lux/crypto" - "lux/proto" - - "gopkg.in/ini.v1" -) - -type luxConfigRoute struct { - Id string `ini:"id"` - Channel string `ini:"channel"` - Address string `ini:"address"` -} - -type luxConfig struct { - Host struct { - Name string `ini:"name"` - } `ini:"host"` - - KeyStore struct { - Path string `ini:"path"` - } `ini:"keystore"` - - Routes []luxConfigRoute -} - -var config luxConfig - -func LuxHostEntry(configPath string) error { - ini, err := ini.LoadSources(ini.LoadOptions{ - AllowNonUniqueSections: true, - }, configPath) - if err != nil { - return err - } - if err = ini.MapTo(&config); err != nil { - return err - } - - // parse routes - { - sections, err := ini.SectionsByName("route") - if err != nil { - log.Fatal(err) - } - - config.Routes = make([]luxConfigRoute, 0) - for _, section := range sections { - route := luxConfigRoute{} - if err = section.MapTo(&route); err != nil { - log.Fatal(err) - } - - config.Routes = append(config.Routes, route) - } - } - fmt.Println(config) - - ks := crypto.NewLuxKeyStore(config.KeyStore.Path) - - if err := ks.Load(); err != nil { - log.Fatal(err) - } - - var hostA, hostB crypto.LuxKey - if ks.Count() < 2 { - hostA, _ = crypto.NewLuxKey(proto.LuxTypeHost) - hostB, _ = crypto.NewLuxKey(proto.LuxTypeHost) - - if err := ks.Put(hostA); err != nil { - log.Fatal(err) - } - if err := ks.Put(hostB); err != nil { - log.Fatal(err) - } - } else { - keys := ks.Keys() - hostA, hostB = keys[0], keys[1] - } - - fmt.Println(hostA.String()) - fmt.Println(hostB.String()) - - return nil -} diff --git a/main.go b/main.go index ae4d4bb..e4b5a80 100644 --- a/main.go +++ b/main.go @@ -2,27 +2,44 @@ package main import ( "flag" - "log" - "lux/host" + "fmt" + "os" + "strings" ) -var configPath string -var isHost bool var isNode bool +var isHost bool +var isRpc bool +var configPath string +var rpcPath string func main() { - flag.StringVar(&configPath, "config", "", "/etc/lux/lux-host.conf or /etc/lux/lux-node.conf") - flag.BoolVar(&isHost, "host", false, "act as LUX host") - flag.BoolVar(&isNode, "node", false, "act as LUX node") + // first, we need to determine who we are: node, host or rpc. + // determine by executable name (lux binary will be symlinked to lux-node, lux-host, luc-rpc), + // or by explicit cli flag (--node, --host, --rpc) + flag.BoolVar(&isNode, "node", false, "LUX node") + flag.BoolVar(&isHost, "host", false, "LUX host") + flag.BoolVar(&isRpc, "rpc", false, "RPC tool") + flag.StringVar(&configPath, "config", "", "node or host config") + flag.StringVar(&rpcPath, "rpc-path", "", "path to RPC UNIX socket or TCP socket, must be in unix:// or tcp:// form") flag.Parse() - if isHost { - if err := host.LuxHostEntry(configPath); err != nil { - log.Fatal(err) + if !isNode && !isHost && !isRpc { + // determine by argv[0] + if strings.Contains(os.Args[0], "node") { + isNode = true + } else if strings.Contains(os.Args[0], "host") { + isHost = true + } else if strings.Contains(os.Args[0], "rpc") { + isRpc = true } - } else if isNode { - log.Fatal("not yet implemented") - } else { - log.Fatal("You must specify either --host or --node with appropriate --config") + } + + if (isNode || isHost) && configPath == "" { + fmt.Fprintln(os.Stderr, "must provide config path") + os.Exit(1) + } else if isRpc && rpcPath == "" { + fmt.Fprintln(os.Stderr, "must provide RPC socket path") + os.Exit(1) } }