Skip to content

Commit

Permalink
handle profile names uniformly
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 24, 2024
1 parent 9388939 commit 2d2429f
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 42 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ tidy: go.mod

clean:
rm -rf $(PLATFORMS)
# rm -f $(NAME)-*.tar
# find -type f -name "*.log" -delete
# rm -f actor.exe
rm -f $(NAME)-*.tar
find -type f -name "*.log" -delete
rm -f actor.exe
rm go-ma-actor*

distclean: clean
rm -rf releases
Expand Down
5 changes: 3 additions & 2 deletions cmd/actor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"github.com/bahner/go-ma-actor/entity/actor"
)

func initConfig(profileName string) actor.ActorConfig {
func initConfig(defaultProfileName string) actor.ActorConfig {

c := actor.Config(profileName)
config.SetDefaultProfileName(defaultProfileName)
c := actor.Config()

if config.GenerateFlag() {
config.Generate(&c)
Expand Down
4 changes: 2 additions & 2 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
log "github.com/sirupsen/logrus"
)

const profile = "actor"
const defaultProfileName = "actor"

func main() {

var err error

initConfig(profile)
initConfig(defaultProfileName)

fmt.Println("Initialising actor configuation...")
// actor.InitConfig(config.Profile())
Expand Down
5 changes: 3 additions & 2 deletions cmd/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ type NodeConfig struct {
P2P config.P2PConfig `yaml:"p2p"`
}

func Config(name string) NodeConfig {
func Config(defaultProfileName string) NodeConfig {

actor.Config(name)
config.SetDefaultProfileName(defaultProfileName)
actor.Config()

c := NodeConfig{
Actor: config.Actor(),
Expand Down
3 changes: 2 additions & 1 deletion cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui/web"
Expand All @@ -13,7 +14,7 @@ func main() {

ctx := context.Background()

Config(name)
Config(config.Profile())

// Init config and logger
// actor.InitConfig(config.Profile())
Expand Down
5 changes: 3 additions & 2 deletions cmd/pong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type PongConfig struct {
Pong PongConfigStruct `yaml:"pong"`
}

func Config(profileName string) PongConfig {
func initConfig(defaultProfileName string) PongConfig {

actor.Config(profileName)
config.SetDefaultProfileName(defaultProfileName)
actor.Config()

c := PongConfig{
Actor: config.Actor(),
Expand Down
9 changes: 2 additions & 7 deletions cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"os"

"github.com/bahner/go-ma-actor/entity/actor"

Expand All @@ -14,7 +13,7 @@ const (
defaultPongReply = "Pong!"
defaultFortuneMode = false
pong = "pong"
profile = pong
defaultProfileName = pong
)

var defaultFortuneArgs = []string{"-s"}
Expand All @@ -24,11 +23,7 @@ func main() {

ctx := context.Background()

c := Config(profile)
c.Print()
if true {
os.Exit(0)
}
initConfig(defaultProfileName)

// THese are the relay specific parts.
p, err := p2p.Init(p2p.DefaultOptions())
Expand Down
4 changes: 2 additions & 2 deletions cmd/relay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type RelayConfig struct {
P2P config.P2PConfig `yaml:"p2p"`
}

func Config(name string) RelayConfig {
func initConfig(defaultProfileName string) RelayConfig {

pflag.Parse()
config.SetProfile(name)
config.SetDefaultProfileName(defaultProfileName)
config.Init()

c := RelayConfig{
Expand Down
4 changes: 2 additions & 2 deletions cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/bahner/go-ma-actor/ui/web"
)

const name = "relay"
const defaultProfileName = "relay"

// Run the pong actor. Cancel it from outside to stop it.
func main() {

ctx := context.Background()
Config(name)
initConfig(defaultProfileName)

// FIXME. Not default here
p, err := p2p.Init(p2p.DefaultOptions())
Expand Down
9 changes: 3 additions & 6 deletions cmd/robot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
"gopkg.in/yaml.v2"
)

const (
name = "robot"
)

func init() {
pflag.String("openai-key", "", "The (paid) key to use with the OpenAI API")

Expand All @@ -37,9 +33,10 @@ type RobotConfig struct {
Robot RobotConfigStruct `yaml:"robot"`
}

func initConfig(name string) RobotConfig {
func initConfig(defaultProfileName string) RobotConfig {

actor.Config(name)
config.SetDefaultProfileName(defaultProfileName)
actor.Config()

// Create a new RobotConfig with the base config and the new key
robotConfig := RobotConfig{
Expand Down
4 changes: 3 additions & 1 deletion cmd/robot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"github.com/bahner/go-ma-actor/p2p"
)

const defaultProfileName = "robot"

// Run the pong actor. Cancel it from outside to stop it.
func main() {

ctx := context.Background()
initConfig(name)
initConfig(defaultProfileName)

// THese are the relay specific parts.

Expand Down
4 changes: 1 addition & 3 deletions config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"github.com/spf13/viper"
)

const defaultDBDirname = ".madb"

var DefaultDbPath = internal.NormalisePath(dataHome + defaultDBDirname)
var DefaultDbPath = internal.NormalisePath(dataHome + "/." + Profile())

func init() {
pflag.String("db-path", DefaultDbPath, "Directory to use for database.")
Expand Down
13 changes: 6 additions & 7 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package config

import "github.com/spf13/pflag"

// NB! This file is used early in the initialization process, so it can't depend on other packages.
const defaultProfile string = "actor"

var profile string = defaultProfile
var defaultProfile = "actor"

// Profile is the mode unless overridden by the profile flag.
func Profile() string {
Expand All @@ -15,9 +12,11 @@ func Profile() string {
return flag.Value.String()
}

return profile
return defaultProfile
}

func SetProfile(p string) {
profile = p
// Call this for special cmds like "relay", "node", etc.
// Before initConfig() like calls.
func SetDefaultProfileName(p string) {
defaultProfile = p
}
3 changes: 1 addition & 2 deletions entity/actor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ type ActorConfig struct {
// This is an all-inclusive configuration function that sets up the configuration for the actor.
// flags and everything. It is used in the main function of siple actors programmes.
// Remebmer to call check the config.GenerateFlag() and save the configuration if it is set.
func Config(name string) ActorConfig {
func Config() ActorConfig {

config.ActorFlags()
pflag.Parse()

config.SetProfile(name)
config.Init()

return ActorConfig{
Expand Down

0 comments on commit 2d2429f

Please sign in to comment.