-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwin-config
More file actions
executable file
·124 lines (103 loc) · 4.36 KB
/
twin-config
File metadata and controls
executable file
·124 lines (103 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
twin-config - Configuration manager for twin tool
Handles reading and writing JSON config files for directory pairings
"""
import sys
import os
import json
from datetime import datetime
import argparse
def get_config_filename(remote):
"""Generate config filename for a given remote"""
return f".twin.{remote}.json"
def read_config(remote, directory=None):
"""Read configuration for a remote from the current or specified directory"""
if directory:
config_path = os.path.join(directory, get_config_filename(remote))
else:
config_path = get_config_filename(remote)
if not os.path.exists(config_path):
return None
try:
with open(config_path, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error reading config: {e}", file=sys.stderr)
return None
def write_config(remote, remote_path=None, rsync_flags=None, directory=None,
git_local_name=None, git_remote_name=None, git_setup_complete=None):
"""Write or update configuration for a remote"""
if directory:
config_path = os.path.join(directory, get_config_filename(remote))
else:
config_path = get_config_filename(remote)
# Read existing config or create new
config = read_config(remote, directory) or {}
# Update fields if provided
if remote_path is not None:
config['remote_path'] = remote_path
if rsync_flags is not None:
config['rsync_flags'] = rsync_flags
# Handle git integration fields
if any([git_local_name, git_remote_name, git_setup_complete is not None]):
if 'git_integration' not in config:
config['git_integration'] = {}
if git_local_name is not None:
config['git_integration']['local_remote_name'] = git_local_name
if git_remote_name is not None:
config['git_integration']['remote_remote_name'] = git_remote_name
if git_setup_complete is not None:
config['git_integration']['setup_complete'] = git_setup_complete
# Always update last_sync timestamp
config['last_sync'] = datetime.now().isoformat()
try:
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
return True
except Exception as e:
print(f"Error writing config: {e}", file=sys.stderr)
return False
def main():
parser = argparse.ArgumentParser(description='Twin configuration manager')
parser.add_argument('action', choices=['read', 'write'], help='Action to perform')
parser.add_argument('remote', help='Remote name')
parser.add_argument('--remote-path', help='Remote path for write action')
parser.add_argument('--rsync-flags', help='Rsync flags for write action')
parser.add_argument('--directory', help='Directory to operate in')
parser.add_argument('--field', help='Specific field to read (supports nested: git_integration.setup_complete)')
parser.add_argument('--git-local-name', help='Git remote name on local machine')
parser.add_argument('--git-remote-name', help='Git remote name on remote machine')
parser.add_argument('--git-setup-complete', type=lambda x: x.lower() == 'true', help='Git setup completion status (true/false)')
args = parser.parse_args()
if args.action == 'read':
config = read_config(args.remote, args.directory)
if config:
if args.field:
# Support nested field access (e.g., git_integration.setup_complete)
value = config
for key in args.field.split('.'):
if isinstance(value, dict):
value = value.get(key, '')
else:
value = ''
break
print(value if value else '')
else:
# Return all as JSON
print(json.dumps(config, indent=2))
else:
sys.exit(1)
elif args.action == 'write':
success = write_config(
args.remote,
remote_path=args.remote_path,
rsync_flags=args.rsync_flags,
directory=args.directory,
git_local_name=args.git_local_name,
git_remote_name=args.git_remote_name,
git_setup_complete=args.git_setup_complete
)
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()