Skip to content

Commit

Permalink
Refactor config handling to avoid race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 17, 2024
1 parent 41c895b commit d28c5ba
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ go-ma-*
releases
*.tar
go.work
actor
pong
relay
26 changes: 19 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
},
{
"name": "Launch pong",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/pong/",
},
{
"name": "Generate pong",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/pong/",
"args": [
"--generate",
"--publish",
"--force"
]
},
{
"name": "Launch allowAll",
"type": "go",
"request": "launch",
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ARM64=android-arm64 darwin-arm64 netbsd-arm64 openbsd-arm64
ALL = $(FETCH) $(KEYSET) $(NAME) $(DEBUG)
BINDIR = $(PREFIX)/bin
RELEASES = releases
CMDS = actor relay pong

ifneq (,$(wildcard ./.env))
include .env
Expand All @@ -39,8 +40,8 @@ local: clean tidy install
$(BINDIR):
test -d $(BINDIR)

install: $(BINDIR) $(NAME)
sudo install -m755 -T $(NAME) $(DESTDIR)$(BINDIR)/ma
install: $(BINDIR) $(CMDS)
sudo install -m755 $(CMDS) $(DESTDIR)$(BINDIR)/

$(DEBUG): BUILDFLAGS = -tags=debug
$(DEBUG): tidy
Expand Down Expand Up @@ -78,6 +79,14 @@ release: VERSION = $(shell ./.version)
release: clean $(RELEASES) $(PLATFORMS)
git tag -a $(VERSION) -m "Release $(VERSION)"

actor: tidy
$(GO) build $(BUILDFLAGS) ./cmd/actor

pong: tidy
$(GO) build $(BUILDFLAGS) ./cmd/pong

relay: tidy
$(GO) build $(BUILDFLAGS) ./cmd/relay

$(RELEASES):
mkdir -p $(RELEASES)
Expand Down
17 changes: 16 additions & 1 deletion cmd/actor/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"errors"
"os"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma/did/doc"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
Expand All @@ -20,12 +22,13 @@ func initConfig() {
// Reinit logging to STDOUT
log.SetOutput(os.Stdout)
log.Info("Generating new actor and node identity")
actor, node := config.GenerateActorIdentitiesOrPanic()
actor, node := generateActorIdentitiesOrPanic(config.Profile())
actorConfig := configTemplate(actor, node)
config.Generate(actorConfig)
os.Exit(0)
}

// At this point an actor *must* be initialized
config.InitActor()

// This flag is dependent on the actor to be initialized to make sense.
Expand All @@ -35,3 +38,15 @@ func initConfig() {
}

}

func generateActorIdentitiesOrPanic(name string) (string, string) {
actor, node, err := config.GenerateActorIdentities(name)
if err != nil {
if errors.Is(err, doc.ErrAlreadyPublished) {
log.Warnf("Actor document already published: %v", err)
} else {
log.Fatal(err)
}
}
return actor, node
}
10 changes: 5 additions & 5 deletions cmd/create_keyset/create_keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,35 @@ func main() {
flag.Parse()
_level, err := log.ParseLevel(*logLevel)
if err != nil {
panic(err)
log.Fatal(err)
}
log.SetLevel(_level)
log.Debugf("main: log level set to %v", _level)

// Create a new keyset for the entity
keyset, err := keyset.GetOrCreate(*name)
if err != nil {
panic(err)
log.Fatal(err)
}
log.Debugf("main: keyset: %v", keyset)

if *publish {
d, err := doc.NewFromKeyset(keyset)
if err != nil {
panic(err)
log.Fatal(err)
}

c, err := d.Publish()
if err != nil {
panic(err)
log.Fatal(err)
}

log.Debugf("main: published document: %v to %v", d, c)
}

packedKeyset, err := keyset.Pack()
if err != nil {
panic(err)
log.Fatal(err)
}
fmt.Println(packedKeyset)

Expand Down
2 changes: 1 addition & 1 deletion cmd/fetch_document/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
flag.Parse()
_level, err := log.ParseLevel(*logLevel)
if err != nil {
panic(err)
log.Fatal(err)
}
log.SetLevel(_level)
log.Debugf("main: log level set to %v", _level)
Expand Down
16 changes: 15 additions & 1 deletion cmd/pong/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"errors"
"os"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma/did/doc"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -54,7 +56,7 @@ func initConfig(profile string) {
// Reinit logging to STDOUT
log.SetOutput(os.Stdout)
log.Info("Generating new actor and node identity")
actor, node := config.GenerateActorIdentitiesOrPanic()
actor, node := generateActorIdentitiesOrPanic(pong)
actorConfig := configTemplate(actor, node)
config.Generate(actorConfig)
os.Exit(0)
Expand All @@ -69,3 +71,15 @@ func initConfig(profile string) {
}

}

func generateActorIdentitiesOrPanic(name string) (string, string) {
actor, node, err := config.GenerateActorIdentities(name)
if err != nil {
if errors.Is(err, doc.ErrAlreadyPublished) {
log.Warnf("Actor document already published: %v", err)
} else {
log.Fatal(err)
}
}
return actor, node
}
5 changes: 3 additions & 2 deletions cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ func main() {
ctx := context.Background()
initConfig(pong)

a := initActorOrPanic()

p, err := initP2P()
if err != nil {
fmt.Printf("Failed to initialize p2p: %v\n", err)
return
}

// Init of actor requires P2P to be initialized
a := initActorOrPanic()

fmt.Printf("Starting pong mode as %s\n", a.Entity.DID.Id)
go p.StartDiscoveryLoop(ctx)
fmt.Println("Discovery loop started.")
Expand Down
19 changes: 13 additions & 6 deletions config/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
)

var (
defaultNick string = os.Getenv("USER")
keyset set.Keyset
ErrEmptyIdentity = fmt.Errorf("identity is empty")
ErrFakeIdentity = fmt.Errorf("your identity is fake. You need to define actorKeyset or generate a new one")
Expand All @@ -33,10 +32,20 @@ func InitActorFlags() {
viper.BindPFlag("actor.location", pflag.Lookup("location"))

viper.SetDefault("actor.location", defaultLocation)
viper.SetDefault("actor.nick", defaultNick)
viper.SetDefault("actor.nick", defaultNick())

}

// Set the default nick to the user's username, unless a profile is set.
func defaultNick() string {

if Profile() == defaultProfile {
return os.Getenv("USER")
}

return Profile()
}

// Load a keyset from string and initiate an Actor.
// This is optional, but if you want to use the actor package, you need to call this.
func InitActor() {
Expand All @@ -53,7 +62,7 @@ func InitActor() {
}

// This function fails fatally, so no return value
initActorKeyset()
initActorKeyset(keyset_string)

if PublishFlag() && keyset_string != "" {
fmt.Println("Publishing identity to IPFS...")
Expand Down Expand Up @@ -89,9 +98,7 @@ func actorIdentity() string {

}

func initActorKeyset() {

keyset_string := viper.GetString("actor.identity")
func initActorKeyset(keyset_string string) {

log.Debugf("config.initActor: %s", keyset_string)
// Create the actor keyset
Expand Down
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Init() error {
// Make sure the XDG directories exist before we start writing to them.
err = createXDGDirectories()
if err != nil {
panic(err)
log.Fatal(err)
}

return nil
Expand All @@ -95,7 +95,7 @@ func Print() (int, error) {

configYAML, err := yaml.Marshal(configMap)
if err != nil {
panic(err)
log.Fatal(err)
}

fmt.Println("# " + ActorKeyset().DID.Id)
Expand Down Expand Up @@ -129,14 +129,14 @@ func File() string {

config, err := pflag.CommandLine.GetString("config")
if err != nil {
panic(err)
log.Fatal(err)
}

// Prefer explicitly requested config. If not, use the name of the profile name.
if config != defaultConfigFile && config != "" {
filename, err = homedir.Expand(config)
if err != nil {
panic(err)
log.Fatal(err)
}
} else {
filename = configHome + Profile() + ".yaml"
Expand Down
Loading

0 comments on commit d28c5ba

Please sign in to comment.