Skip to content

Commit 4ead534

Browse files
committed
added default lookup location for creditfile
1 parent 26ce193 commit 4ead534

File tree

6 files changed

+102
-17
lines changed

6 files changed

+102
-17
lines changed

.tmux/mir-serve.session.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Set a custom session root path. Default is `$HOME`.
2+
# Must be called before `initialize_session`.
3+
session_root "$PWD"
4+
5+
# Create session with specified name if it does not already exist. If no
6+
# argument is given, session name will be based on layout file name.
7+
if initialize_session "mir"; then
8+
9+
# Create a new window inline within session layout definition.
10+
#new_window "misc"
11+
12+
# Load a defined window layout.
13+
load_window "./.tmux/mir.window.sh"
14+
load_window "./.tmux/claude.window.sh"
15+
load_window "./.tmux/serve.window.sh"
16+
load_window "./.tmux/infra.window.sh"
17+
load_window "./.tmux/cfg.window.sh"
18+
load_window "./.tmux/book.window.sh"
19+
load_window "./.tmux/wiki.window.sh"
20+
21+
# Select the default active window on session creation.
22+
select_window 1
23+
24+
fi
25+
26+
# Finalize session creation and switch/attach to it.
27+
finalize_and_go_to_session

.tmux/serve.window.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Set window root path. Default is `$session_root`.
2+
# Must be called before `new_window`.
3+
window_root "$PWD/cmds/mir/"
4+
5+
# Create new window. If no argument is given, window name will be based on
6+
# layout file name.
7+
new_window "serve"
8+
9+
#run_cmd "sleep 5 && go run main.go"
10+
run_cmd 'bash -c "(sleep 5 && go run main.go serve)"'
11+
12+
13+
# Split window into panes.
14+
split_v 20
15+
#split_h 50
16+
17+
# Run commands.
18+
#run_cmd "top" # runs in active pane
19+
#run_cmd "date" 1 # runs in pane 1
20+
21+
# Paste text
22+
#send_keys "top" # paste into active pane
23+
#send_keys "date" 1 # paste into pane 1
24+
25+
# Set active pane.
26+
#select_pane 0

TASKS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [ ] device security with nats
1515
- [ ] nkeys/jwt
1616
- [x] modify sdks
17+
[ ] add device,default credential location
1718
- [x] learn how it work
1819
- [ ] integrate nsc to cli with default security for device and clients
1920
- [ ] add the nsc generator
@@ -22,6 +23,7 @@
2223
- [x] read only
2324
- [x] read and write
2425
- [ ] work on compose release, maybe its just a readme
26+
- [ ] default config has resolver.conf include commented out
2527
- [ ] adjust helm release
2628
- [ ] tls
2729
- [ ] core bug with reconnect of loading devices

internal/libs/swarm/swarm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (b *devicesBuilder) Incubate() ([]*mir_apiv1.CreateDeviceResponse, error) {
171171
LogLevel(b.logLevel).
172172
Store(b.storeOpts).
173173
Target(b.s.bus.ConnectedUrl()).
174-
UserCredentials(b.credentials).
174+
UserCredentialsFile(b.credentials).
175175
Schema(b.sch...).Build()
176176
if err != nil {
177177
errs = errors.Join(err)
@@ -194,7 +194,7 @@ func (b *devicesBuilder) Incubate() ([]*mir_apiv1.CreateDeviceResponse, error) {
194194
LogWriters(b.logWriters).
195195
Store(b.storeOpts).
196196
Target(b.s.bus.ConnectedUrl()).
197-
UserCredentials(b.credentials).
197+
UserCredentialsFile(b.credentials).
198198
Schema(b.sch...).Build()
199199
if err != nil {
200200
errs = errors.Join(err)
@@ -273,7 +273,7 @@ func (b *deviceBuilder) Incubate() (*mir_apiv1.CreateDeviceResponse, error) {
273273
LogWriters(b.logWriters).
274274
Store(b.storeOpts).
275275
Target(b.s.bus.ConnectedUrl()).
276-
UserCredentials(b.credentials).
276+
UserCredentialsFile(b.credentials).
277277
Schema(b.sch...).Build()
278278
if err != nil {
279279
return nil, err

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ install-scp host arch="arm64" os="linux":
5858
tx:
5959
tmuxifier s ./.tmux/mir.session.sh
6060

61+
# Start tmux layouts with mir in docker and serve using CLI
62+
tx-serve:
63+
tmuxifier s ./.tmux/mir-serve.session.sh
64+
6165
# Start tmux layouts with mir in docker
6266
tx-full:
6367
tmuxifier s ./.tmux/mir-full.session.sh

pkgs/device/mir/mir_builder.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
type builder struct {
2424
fileOpts []func(*mir_config.MirConfig)
25-
credentials *string
25+
credentialsFiles []string
2626
deviceId *string
2727
deviceIdGenerator *IdGenerator
2828
deviceIdPrefix *IdPrefix
@@ -82,8 +82,28 @@ func (b builder) DeviceIdPrefix(p IdPrefix) builder {
8282
return b
8383
}
8484

85-
func (b builder) UserCredentials(fullPath string) builder {
86-
b.credentials = &fullPath
85+
func (b builder) UserCredentialsFile(fullPath string) builder {
86+
b.credentialsFiles = append(b.credentialsFiles, fullPath)
87+
return b
88+
}
89+
90+
// Look for a user credentials file at those locations in order to authenticate:
91+
// - ./device.creds
92+
// - ~/.config/mir/device.creds
93+
// - /etc/mir/device.creds
94+
func (b builder) DefaultUserCredentialsFile() builder {
95+
userHomeDir, err := os.UserHomeDir()
96+
if err != nil {
97+
fmt.Println("$HOME is not defined")
98+
userHomeDir = "./"
99+
}
100+
xdgConfigHome := filepath.Join(userHomeDir, ".config", "mir", "device.creds")
101+
102+
b.credentialsFiles = append(b.credentialsFiles,
103+
"/etc/mir/device.creds",
104+
xdgConfigHome,
105+
"./device.creds",
106+
)
87107
return b
88108
}
89109

@@ -94,15 +114,12 @@ func (b builder) Target(t string) builder {
94114
return b
95115
}
96116

97-
// Use a configuration file to load the
98-
// device_id and the target. Specifying those configs
99-
// in the builder pattern have greater priority
100-
// then loading from the config file.
101-
// The file is loaded in folders:
102-
// - ./device.yaml
103-
// - ~/.config/mir/device.yaml
104-
// - /etc/mir/device.yaml
105-
// $HOME/.config/mir/device.[json|yaml]
117+
// Use a configuration file to load device configs.
118+
// Specifying those configs in the builder pattern have greater priority
119+
// then loading from the config file. The file is loaded in folders:
120+
// - ./device.yaml
121+
// - ~/.config/mir/device.yaml
122+
// - /etc/mir/device.yaml
106123
func (b builder) DefaultConfigFile() builder {
107124
format := mir_config.Yaml
108125
fileName := "device.yaml"
@@ -247,8 +264,17 @@ func (b builder) build(extraCfg any) (*Mir, error) {
247264
} else if cfg.Mir.Target == "" {
248265
cfg.Mir.Target = "nats://127.0.0.1:4222"
249266
}
250-
if b.credentials != nil {
251-
cfg.Mir.Credentials = *b.credentials
267+
// Add credential file path
268+
if cfg.Mir.Credentials != "" {
269+
b.credentialsFiles = append(b.credentialsFiles, cfg.Mir.Credentials)
270+
}
271+
// Look for first file to exist
272+
for i := len(b.credentialsFiles) - 1; i >= 0; i-- {
273+
c := b.credentialsFiles[i]
274+
if _, err := os.Stat(c); err == nil {
275+
cfg.Mir.Credentials = c
276+
break
277+
}
252278
}
253279
if b.logLevel != nil {
254280
cfg.Mir.LogLevel = b.logLevel.String()

0 commit comments

Comments
 (0)